webpack2 环境搭建

前言

由于demos/project 前端渲染是通过react.js渲染的,这就需要webpack2 对react.js及其相关JSX,ES6/7代码进行编译和混淆压缩。

webpack2

安装和文档

可访问网https://webpack.js.org/

配置webpack2编译react.js + less + sass + antd 环境

文件目录

  1. └── static # 项目静态文件目录
  2. ├── build
  3. ├── webpack.base.config.js # 基础编译脚本
  4. ├── webpack.dev.config.js # 开发环境编译脚本
  5. └── webpack.prod.config.js # 生产环境编译脚本
  6. ├── output # 编译后输出目录
  7. ├── asset
  8. ├── dist
  9. └── upload
  10. └── src # 待编译的ES6/7、JSX源代码
  11. ├── api
  12. ├── apps
  13. ├── components
  14. ├── pages
  15. ├── texts
  16. └── utils

webpack2 编译基础配置

webpack.base.config.js

  1. const webpack = require('webpack');
  2. const ExtractTextPlugin = require('extract-text-webpack-plugin');
  3. const path = require('path');
  4. const sourcePath = path.join(__dirname, './static/src');
  5. const outputPath = path.join(__dirname, './../output/dist/');
  6. module.exports = {
  7. // 入口文件
  8. entry: {
  9. 'admin' : './static/src/pages/admin.js',
  10. 'work' : './static/src/pages/work.js',
  11. 'index' : './static/src/pages/index.js',
  12. 'error' : './static/src/pages/error.js',
  13. vendor: ['react', 'react-dom', 'whatwg-fetch'],
  14. },
  15. // 出口文件
  16. output: {
  17. path: outputPath,
  18. publicPath: '/static/output/dist/',
  19. filename: 'js/[name].js',
  20. },
  21. module: {
  22. // 配置编译打包规则
  23. rules: [
  24. {
  25. test: /\.(js|jsx)$/,
  26. exclude: /node_modules/,
  27. use: [
  28. {
  29. loader: 'babel-loader',
  30. query: {
  31. // presets: ['es2015', 'react'],
  32. cacheDirectory: true
  33. }
  34. }
  35. ]
  36. },
  37. {
  38. test: /\.css$/,
  39. use: ExtractTextPlugin.extract({
  40. fallback: "style-loader",
  41. use: ['css-loader']
  42. }),
  43. },
  44. {
  45. test: /\.scss$/,
  46. use: ExtractTextPlugin.extract({
  47. fallback: "style-loader",
  48. use: ['css-loader', 'sass-loader']
  49. })
  50. },
  51. {
  52. test: /\.less$/,
  53. use: ExtractTextPlugin.extract({
  54. fallback: "style-loader",
  55. use: ['css-loader', 'less-loader']
  56. })
  57. },
  58. ]
  59. },
  60. resolve: {
  61. extensions: ['.js', '.jsx'],
  62. modules: [
  63. sourcePath,
  64. 'node_modules'
  65. ]
  66. },
  67. plugins: [
  68. new ExtractTextPlugin('css/[name].css'),
  69. new webpack.optimize.CommonsChunkPlugin({
  70. names: ['vendor'],
  71. minChunks: Infinity,
  72. filename: 'js/[name].js'
  73. }),
  74. ]
  75. };

配置开发&生产环境webpack2 编译设置

为了方便编译基本配置代码统一管理,开发环境(wepack.dev.config.js)和生产环境(webpack.prod.config.js)的编译配置都是继承了基本配置(wepack.base.config.js)的代码

开发环境配置 wepack.dev.config.js

  1. var merge = require('webpack-merge')
  2. var webpack = require('webpack')
  3. var baseWebpackConfig = require('./webpack.base.config');
  4. module.exports = merge(baseWebpackConfig, {
  5. devtool: 'source-map',
  6. plugins: [
  7. new webpack.DefinePlugin({
  8. 'process.env': {
  9. NODE_ENV: JSON.stringify('development')
  10. }
  11. }),
  12. ]
  13. })

编译环境配置 wepack.prod.config.js

  1. var webpack = require('webpack');
  2. var merge = require('webpack-merge');
  3. var baseWebpackConfig = require('./webpack.base.config');
  4. module.exports = merge(baseWebpackConfig, {
  5. // eval-source-map is faster for development
  6. plugins: [
  7. new webpack.DefinePlugin({
  8. 'process.env': {
  9. NODE_ENV: JSON.stringify('production')
  10. }
  11. }),
  12. new webpack.optimize.UglifyJsPlugin({
  13. minimize: true,
  14. compress: {
  15. warnings: false,
  16. }
  17. })
  18. ]
  19. })