|
1 | 1 | const commonConfig = require('./webpack.base.config.js')
|
2 | 2 | const resolve = dir => path.join(__dirname, '..', dir)
|
3 | 3 | const path = require('path')
|
4 |
| -const merge = require('webpack-merge') |
| 4 | +const { merge } = require('webpack-merge') |
5 | 5 | const { CleanWebpackPlugin } = require('clean-webpack-plugin')
|
6 | 6 | const MiniCssExtractPlugin = require('mini-css-extract-plugin')
|
7 | 7 |
|
@@ -35,34 +35,6 @@ module.exports = merge(commonConfig, {
|
35 | 35 | 'postcss-loader'
|
36 | 36 | ]
|
37 | 37 | },
|
38 |
| - { |
39 |
| - test: /\.less$/, |
40 |
| - // include: resolve('src'), |
41 |
| - use: [ |
42 |
| - { |
43 |
| - loader: MiniCssExtractPlugin.loader, |
44 |
| - options: { |
45 |
| - publicPath: (resourcePath, context) => { |
46 |
| - // publicPath is the relative path of the resource to the context |
47 |
| - // e.g. for ./css/admin/main.css the publicPath will be ../../ |
48 |
| - // while for ./css/main.css the publicPath will be ../ |
49 |
| - return path.relative(path.dirname(resourcePath), context) + '/' |
50 |
| - } |
51 |
| - } |
52 |
| - }, |
53 |
| - { |
54 |
| - loader: 'css-loader', |
55 |
| - options: { |
56 |
| - odules: { |
57 |
| - localIdentName: '[local]--[hash:base64]' |
58 |
| - } |
59 |
| - } |
60 |
| - }, // 将 CSS 转化成 CommonJS 模块 |
61 |
| - 'postcss-loader', |
62 |
| - 'less-loader' // 将 Less 编译为 CSS |
63 |
| - ] |
64 |
| - }, |
65 |
| - |
66 | 38 | {
|
67 | 39 | test: /\.scss$/,
|
68 | 40 | use: [
|
@@ -100,36 +72,57 @@ module.exports = merge(commonConfig, {
|
100 | 72 | filename: '[name][hash].js', // 生成的js文件的名字
|
101 | 73 | path: path.resolve(__dirname, '..', 'dist/708') // 生成的js存放目录
|
102 | 74 | },
|
103 |
| - // optimization: { |
104 |
| - // splitChunks: { |
105 |
| - // chunks: "all", //可填 async, initial, all. 顾名思义,async针对异步加载的chunk做切割 |
106 |
| - // minSize: 30000, //我们切割完要生成的新chunk要>30kb,否则不生成新chunk。 |
107 |
| - // minChunks: 2, //共享该module的最小chunk数 |
108 |
| - // maxAsyncRequests: 5, //最多有5个异步加载请求该module |
109 |
| - // maxInitialRequests: 3, //初始化的时候最多有3个请求该module |
110 |
| - // automaticNameDelimiter: '~', //名字中间的间隔符 |
111 |
| - // name: true, //chunk的名字,如果设成true,会根据被提取的chunk自动生成。 |
112 |
| - // cacheGroups: { //要切割成的每一个新chunk就是一个cache group。 |
113 |
| - // vendors: { |
114 |
| - // test: /[\\/]node_modules[\\/]/, //和CommonsChunkPlugin里的minChunks非常像,用来决定提取哪些module,可以接受字符串,正则表达式,或者函数,函数的一个参数为module,第二个参数为引用这个module的chunk(数组). |
115 |
| - // priority: -10 |
116 |
| - // }, |
117 |
| - // default: { |
118 |
| - // minChunks: 2, |
119 |
| - // priority: -20, //优先级高的chunk为被优先选择(说出来感觉好蠢),优先级一样的话,size大的优先被选择 |
120 |
| - // reuseExistingChunk: true //当module未变时,是否可以使用之前的chunk. |
121 |
| - // } |
122 |
| - // } |
123 |
| - // } |
124 |
| - // }, |
| 75 | + optimization: { |
| 76 | + minimize: true, |
| 77 | + // 告诉webpack使用TerserPlugin最小化捆绑包 |
| 78 | + minimizer: [ |
| 79 | + new TerserPlugin({ |
| 80 | + parallel: 4, |
| 81 | + sourceMap: false, |
| 82 | + }), |
| 83 | + new OptimizeCSSAssetsPlugin({}) |
| 84 | + ], |
| 85 | + //抽取公共的dm |
| 86 | + splitChunks: { |
| 87 | + name: "commons",//chunk的名字,如果设成true,会根据被提取的chunk自动生成。 |
| 88 | + chunks: 'all',//可填 async, initial, all. 顾名思义,async针对异步加载的chunk做切割 |
| 89 | + minSize: 30000, //我们切割完要生成的新chunk要>30kb,否则不生成新chunk。 |
| 90 | + // minChunks: 2, //共享该module的最小chunk数 |
| 91 | + // maxAsyncRequests: 5, //最多有5个异步加载请求该module |
| 92 | + // maxInitialRequests: 3, //初始化的时候最多有3个请求该module |
| 93 | + // automaticNameDelimiter: '~', //名字中间的间隔符 |
| 94 | + cacheGroups: {//要切割成的每一个新chunk就是一个cache group。 |
| 95 | + styles: { |
| 96 | + name: 'styles', |
| 97 | + test: /\.css$/, |
| 98 | + chunks: 'all', |
| 99 | + enforce: true, |
| 100 | + }, |
| 101 | + // vendors: { |
| 102 | + // test: /[\\/]node_modules[\\/]/, //和CommonsChunkPlugin里的minChunks非常像,用来决定提取哪些module,可以接受字符串,正则表达式,或者函数,函数的一个参数为module,第二个参数为引用这个module的chunk(数组). |
| 103 | + // priority: -10 |
| 104 | + // }, |
| 105 | + // default: { |
| 106 | + // minChunks: 2, |
| 107 | + // priority: -20, //优先级高的chunk为被优先选择(说出来感觉好蠢),优先级一样的话,size大的优先被选择 |
| 108 | + // reuseExistingChunk: true //当module未变时,是否可以使用之前的chunk. |
| 109 | + // } |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
125 | 113 | plugins: [
|
| 114 | + new webpack.HashedModuleIdsPlugin(), |
126 | 115 | new CleanWebpackPlugin(),
|
127 | 116 | new MiniCssExtractPlugin({
|
128 |
| - // Options similar to the same options in webpackOptions.output |
129 |
| - // all options are optional |
130 |
| - filename: '[name].css', |
131 |
| - chunkFilename: '[id].css', |
132 |
| - ignoreOrder: false // Enable to remove warnings about conflicting order |
133 |
| - }) |
| 117 | + filename: 'asset/css/[name].[hash].css', |
| 118 | + chunkFilename: 'asset/css/[id].[chunkhash].css', |
| 119 | + }), |
| 120 | + // new MiniCssExtractPlugin({ |
| 121 | + // // Options similar to the same options in webpackOptions.output |
| 122 | + // // all options are optional |
| 123 | + // filename: '[name].css', |
| 124 | + // chunkFilename: '[id].css', |
| 125 | + // ignoreOrder: false // Enable to remove warnings about conflicting order |
| 126 | + // }) |
134 | 127 | ]
|
135 | 128 | })
|
0 commit comments