需安装插件:thread-loader

多进程打包:
进程启动大概为600ms,进程通信也有开销
只用工作消耗时间比较长,才需要多进程打包

webpack.config.js

  1. const { resolve } = require('path')
  2. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  3. const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');
  4. const HtmlWebpackPlugin = require('html-webpack-plugin');
  5. const WorkboxWebpackPlugin = require('workbox-webpack-plugin')
  6. /*
  7. PWA:渐进式网络开发应用程序(离线可访问)
  8. workbox --> workbox-webpack-plugin
  9. */
  10. //定义nodejs环境变量:决定使用browserslist的哪个环境
  11. process.env.NODE_ENV = 'production'
  12. //复用loader
  13. const commonCssLoader = [
  14. MiniCssExtractPlugin.loader,
  15. 'css-loader',
  16. {
  17. //还需要在package.json中定义browserslist
  18. loader: 'postcss-loader',
  19. options: {
  20. ident: 'postcss',
  21. plugins: () => [
  22. require('postcss-preset-env')()
  23. ],
  24. }
  25. }
  26. ]
  27. module.exports = {
  28. entry: './src/js/index.js',
  29. output: {
  30. filename: 'js/built.[contenthash:10].js',
  31. path: resolve(__dirname, 'build'),
  32. // publicPath: 'build'
  33. },
  34. module: {
  35. rules: [
  36. {
  37. //在package.json中eslintConfig --> airbnb
  38. test: /\.js$/,
  39. exclude: /node_modules/,
  40. //优先执行
  41. enforce: 'pre',
  42. loader: 'eslint-loader',
  43. options: {
  44. fix: true
  45. }
  46. },
  47. {
  48. //以下loader只会匹配一个
  49. //注意:不能有两个配置 处理同一个文件
  50. oneOf: [
  51. {
  52. test: /\.css$/,
  53. use: [...commonCssLoader],
  54. },
  55. {
  56. test: /\.less$/,
  57. use: [...commonCssLoader, 'less-loader']
  58. },
  59. /**
  60. * 正常来讲,一个文件只能被一个loader处理
  61. * 当一个文件要被多个loader处理,那么一定要指定loader执行的先后顺序:
  62. * 先执行eslint 再执行babel
  63. */
  64. {
  65. test: /\.js$/,
  66. exclude: /node_modules/,
  67. use:[
  68. /*
  69. 多进程打包
  70. 进程启动大概为600ms,进程通信也有开销
  71. 只用工作消耗时间比较长,才需要多进程打包
  72. */
  73. {
  74. loader:'thread-loader',
  75. options:{
  76. workers:2 //进程两个
  77. }
  78. },
  79. {
  80. loader: 'babel-loader',
  81. options: {
  82. presets: [
  83. [
  84. '@babel/preset-env',
  85. {
  86. //按需加载
  87. useBuiltIns: 'usage',
  88. //指定core-js版本
  89. corejs: {
  90. version: 3
  91. },
  92. targets: {
  93. chrome: '60',
  94. firefox: '60',
  95. ie: '9',
  96. safari: '10',
  97. edge: '17'
  98. }
  99. }
  100. ],
  101. ],
  102. //开启babel缓存
  103. //第二次构建时,会读取之前的缓存
  104. cacheDirectory:true
  105. }
  106. }
  107. ],
  108. },
  109. {
  110. test: /\.(jpg|png|gif)/,
  111. loader: 'url-loader',
  112. options: {
  113. limit: 8 * 1024,
  114. name: '[hash:10].[ext]',
  115. outputPath: '../imgs',
  116. esModule: false,
  117. }
  118. },
  119. {
  120. test: /\.html$/,
  121. loader: 'html-loader'
  122. },
  123. {
  124. exclude: /\.(js|css|less|html|jpg|png|gif)/,
  125. loader: 'file-loader',
  126. options: {
  127. outputPath: 'media',
  128. }
  129. }
  130. ]
  131. }
  132. ]
  133. },
  134. plugins: [
  135. new MiniCssExtractPlugin({
  136. filename: 'css/built.[contenthash:10].css'
  137. }),
  138. new OptimizeCssAssetsWebpackPlugin(),
  139. new HtmlWebpackPlugin({
  140. template: './src/index.html',
  141. minify: {
  142. collapseWhitespace: true,
  143. removeComments: true
  144. }
  145. }),
  146. new WorkboxWebpackPlugin.GenerateSW({
  147. /**
  148. * 1.帮助serviceworker快速启动
  149. * 2.删除旧的serviceworker
  150. *
  151. * 生成serviceworker的配置文件
  152. */
  153. clientsClaim:true,
  154. skipWaiting:true
  155. })
  156. ],
  157. mode: 'production',
  158. devtool:'source-map'
  159. }