webpack4 环境搭建

前言

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

webpack4

安装和文档

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

配置webpack4编译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

webpack4 编译基础配置

babel@7 配置

  1. const babelConfig = {
  2. presets: [
  3. '@babel/env',
  4. // [
  5. // '@babel/env',
  6. // {
  7. // targets: {
  8. // edge: '17',
  9. // firefox: '60',
  10. // chrome: '67',
  11. // safari: '11.1'
  12. // },
  13. // useBuiltIns: 'usage'
  14. // }
  15. // ],
  16. '@babel/preset-react'
  17. ],
  18. 'plugins': [
  19. [
  20. 'import',
  21. { 'libraryName': 'antd', 'libraryDirectory': 'lib' },
  22. 'ant'
  23. ],
  24. [
  25. 'import',
  26. { 'libraryName': 'antd-mobile', 'libraryDirectory': 'lib' },
  27. 'antd-mobile'
  28. ],
  29. '@babel/plugin-proposal-class-properties'
  30. ]
  31. };
  32. module.exports = babelConfig;

webpack.base.config.js

  1. const path = require('path');
  2. const webpack = require('webpack');
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  4. const babelConfig = require('./babel.config');
  5. // const prodMode = process.env.NODE_ENV === 'production';
  6. const srcResolve = function (file) {
  7. return path.join(__dirname, '..', 'src', file);
  8. };
  9. const distResolve = function (file) {
  10. return path.join(__dirname, '..', 'output', 'dist', file);
  11. };
  12. module.exports = {
  13. entry: {
  14. 'index': srcResolve('js/index'),
  15. 'admin' : srcResolve('pages/admin.js'),
  16. 'work' : srcResolve('pages/work.js'),
  17. 'index' : srcResolve('pages/index.js'),
  18. 'error' : srcResolve('pages/error.js'),
  19. },
  20. output: {
  21. path: distResolve(''),
  22. filename: 'vendorjs/[name].js'
  23. },
  24. module: {
  25. rules: [
  26. {
  27. test: /\.(js|jsx)$/,
  28. use: {
  29. loader: 'babel-loader',
  30. options: babelConfig
  31. }
  32. },
  33. {
  34. test: /\.(css|less)$/,
  35. use: [
  36. // devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
  37. // 'style-loader',
  38. MiniCssExtractPlugin.loader,
  39. 'css-loader',
  40. // 'postcss-loader',
  41. {
  42. loader: 'postcss-loader',
  43. options: {
  44. plugins: () => {
  45. return [];
  46. }
  47. }
  48. },
  49. 'less-loader'
  50. ]
  51. }
  52. ]
  53. },
  54. plugins: [
  55. new MiniCssExtractPlugin({
  56. filename: 'css/[name].css'
  57. })
  58. ],
  59. optimization: {
  60. splitChunks: {
  61. cacheGroups: {
  62. commons: {
  63. test: /[\\/]node_modules[\\/]/,
  64. name: 'vendor',
  65. chunks: 'all'
  66. }
  67. }
  68. }
  69. }
  70. };

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

为了方便编译基本配置代码统一管理,开发环境(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. process.env.NODE_ENV = 'production';
  2. const merge = require('webpack-merge');
  3. const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
  4. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  5. const config = require('./webpack.base.config');
  6. module.exports = merge(config, {
  7. mode: 'production',
  8. // plugins: [
  9. // new UglifyJsPlugin()
  10. // ]
  11. optimization: {
  12. minimizer: [
  13. new UglifyJsPlugin({
  14. cache: true,
  15. parallel: true
  16. }),
  17. new OptimizeCSSAssetsPlugin({})
  18. ]
  19. }
  20. });