ä» webpack 4 å¼å§ï¼ä¼æ ¹æ®ä½ éæ©ç mode
æ¥æ§è¡ä¸åçä¼åï¼
ä¸è¿ææçä¼åè¿æ¯å¯ä»¥æå¨é
ç½®åéåã
boolean = false
string: 'natural' | 'named' | 'size' | 'total-size' | 'deterministic'
åç¥ webpack å½éæ©æ¨¡å id æ¶éè¦ä½¿ç¨åªç§ç®æ³ãå° optimization.chunkIds
设置为 false
ä¼åç¥ webpack 没æä»»ä½å
ç½®çç®æ³ä¼è¢«ä½¿ç¨ï¼ä½èªå®ä¹çç®æ³ä¼ç±æ件æä¾ãoptimization.chunkIds
çé»è®¤å¼æ¯ false
ï¼
optimization.chunkIds
ä¼è¢«è®¾ç½®æ 'named'
ï¼ä½å½å¨ç产ç¯å¢ä¸æ¶ï¼å®ä¼è¢«è®¾ç½®æ 'deterministic'
optimization.chunkIds
ä¼è¢«é»è®¤è®¾ç½®ä¸º 'natural'
ä¸è¿°é项å符串å¼å为被æ¯æï¼
éé¡¹å¼ | æè¿° |
---|---|
'natural' | æ使ç¨é¡ºåºçæ°å idã |
'named' | 对è°è¯æ´å好çå¯è¯»ç idã |
'deterministic' | å¨ä¸åçç¼è¯ä¸ä¸åççæ°å idãæçäºé¿æç¼åãå¨ç产模å¼ä¸ä¼é»è®¤å¼å¯ã |
'size' | ä¸æ³¨äºè®©åå§ä¸è½½å 大å°æ´å°çæ°å idã |
'total-size' | ä¸æ³¨äºè®©æ»ä¸è½½å 大å°æ´å°çæ°å idã |
webpack.config.js
module.exports = {
//...
optimization: {
chunkIds: 'named',
},
};
é»è®¤å°ï¼å½ optimization.chunkIds
被设置为 'deterministic'
æ¶ï¼æå° 3 ä½æ°åä¼è¢«ä½¿ç¨ãè¦è¦çé»è®¤çè¡ä¸ºï¼è¦å° optimization.chunkIds
设置为 false
ï¼åæ¶è¦ä½¿ç¨ webpack.ids.DeterministicChunkIdsPlugin
ã
webpack.config.js
module.exports = {
//...
optimization: {
chunkIds: false,
},
plugins: [
new webpack.ids.DeterministicChunkIdsPlugin({
maxLength: 5,
}),
],
};
boolean
åè¯ webpack å¨æ¨¡åå¾ä¸æ¥æ¾å¯ä»¥å®å
¨è¿æ¥å°å个模åçç段ãæ¤ä¼åé¦å
éè¦ optimization.providedExports
ä¸ optimization.usedExports
设置为å¯ç¨ã
é»è®¤æ
åµä¸ï¼optimization.concatenateModules
å¨ production
æ¨¡å¼ ä¸å¯ç¨ï¼èå¨å
¶å®æ
åµä¸è¢«ç¦ç¨ã
webpack.config.js
module.exports = {
//...
optimization: {
concatenateModules: true,
},
};
boolean = false
ä½¿ç¨ optimization.emitOnErrors
å¨ç¼è¯æ¶æ¯å½æé误æ¶ï¼å°±ä¼åééæèµæºãè¿æ ·å¯ä»¥ç¡®ä¿åºéçéæèµæºè¢«åéåºæ¥ãå
³é®é误ä¼è¢«åéå°çæç代ç ä¸ï¼å¹¶ä¼å¨è¿è¡æ¶æ¥éã
webpack.config.js
module.exports = {
//...
optimization: {
emitOnErrors: true,
},
};
boolean
åç¥ webpack ç¡®å®åæ è®°åºä½ä¸ºå
¶ä» chunk åéçé£äº chunkï¼å
¶æ¹å¼æ¯å¨å·²ç»å è½½è¿è¾å¤§ç chunk ä¹åï¼å°±ä¸åå»å è½½è¿äº chunk åéãoptimization.flagIncludedChunks
é»è®¤ä¼å¨ production
æ¨¡å¼ ä¸å¯ç¨ï¼å
¶ä»æ
åµç¦ç¨ã
webpack.config.js
module.exports = {
//...
optimization: {
flagIncludedChunks: true,
},
};
boolean = true
optimization.innerGraph
åè¯ webpack æ¯å¦å¯¹æªä½¿ç¨ç导åºå
容å®æ½å
é¨å¾å½¢åæã
webpack.config.js
module.exports = {
//...
optimization: {
innerGraph: false,
},
};
boolean
string: 'deterministic' | 'size'
optimization.mangleExports
å
许æ§å¶å¯¼åºå¤ç(export mangling)ã
é»è®¤ optimization.mangleExports: 'deterministic'
ä¼å¨ production
模å¼ä¸ å¯ç¨èå
¶å®æ
åµä¼è¢«ç¦ç¨ã
æ¤é项æ¯æ以ä¸é项ï¼
é项 | æè¿° |
---|---|
'size' | ç®åå½¢å¼ â é常åªæä¸ä¸ªå符 â ä¸æ³¨äºæå°çä¸è½½ sizeã |
'deterministic' | ç®åå½¢å¼ - é常两个å符 â å¨æ·»å æç§»é¤ export æ¶ä¸ä¼æ¹åãéç¨äºé¿æç¼åã |
true | çä»·äº 'deterministic' |
false | ä¿çååï¼æå©äºé 读åè°è¯ã |
webpack.config.js
module.exports = {
//...
optimization: {
mangleExports: true,
},
};
boolean = false
å¨è®¾ç½®ä¸º true
æ¶ï¼åç¥ webpack éè¿å°å¯¼å
¥ä¿®æ¹ä¸ºæ´ççå符串ï¼æ¥åå° WASM 大å°ãè¿ä¼ç ´å模åå导åºå称ã
webpack.config.js
module.exports = {
//...
optimization: {
mangleWasmImports: true,
},
};
boolean = true
åç¥ webpack å并å«æç¸å模åç chunkãå° optimization.mergeDuplicateChunks
设置为 false
以ç¦ç¨è¿é¡¹ä¼åã
webpack.config.js
module.exports = {
//...
optimization: {
mergeDuplicateChunks: false,
},
};
boolean = true
åç¥ webpack ä½¿ç¨ TerserPlugin æå
¶å®å¨ optimization.minimizer
å®ä¹çæ件å缩 bundleã
webpack.config.js
module.exports = {
//...
optimization: {
minimize: false,
},
};
[TerserPlugin]
æ [function (compiler)]
å è®¸ä½ éè¿æä¾ä¸ä¸ªæå¤ä¸ªå®å¶è¿ç TerserPlugin å®ä¾ï¼è¦çé»è®¤åç¼©å·¥å ·(minimizer)ã
webpack.config.js
const TerserPlugin = require('terser-webpack-plugin');
module.exports = {
optimization: {
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
},
}),
],
},
};
æï¼ä½¿ç¨å½æ°çå½¢å¼ï¼
module.exports = {
optimization: {
minimizer: [
(compiler) => {
const TerserPlugin = require('terser-webpack-plugin');
new TerserPlugin({
/* ä½ çé
ç½® */
}).apply(compiler);
},
],
},
};
å¨ optimization.minimizer
ä¸å¯ä»¥ä½¿ç¨ '...'
æ¥è®¿é®é»è®¤å¼ã
module.exports = {
optimization: {
minimizer: [new CssMinimizer(), '...'],
},
};
boolean: false
string: 'natural' | 'named' | 'deterministic' | 'size'
åç¥ webpack å½éæ©æ¨¡å id æ¶éè¦ä½¿ç¨åªç§ç®æ³ãå° optimization.moduleIds
设置为 false
ä¼åç¥ webpack 没æä»»ä½å
ç½®çç®æ³ä¼è¢«ä½¿ç¨ï¼ä½èªå®ä¹çç®æ³ä¼ç±æ件æä¾ã
ä¸è¿°é项å符串å¼å为被æ¯æ:
éèå¼ | æè¿° |
---|---|
natural | æ使ç¨é¡ºåºçæ°å idã |
named | 对è°è¯æ´å好çå¯è¯»ç idã |
deterministic | 被åå¸è½¬åæçå°ä½æ°å¼æ¨¡ååã |
size | ä¸æ³¨äºè®©åå§ä¸è½½å 大å°æ´å°çæ°å idã |
webpack.config.js
module.exports = {
//...
optimization: {
moduleIds: 'deterministic',
},
};
deterministic
é项æçäºé¿æç¼åï¼ä½å¯¹æ¯äº hashed
æ¥è¯´ï¼å®ä¼å¯¼è´æ´å°çæ件 bundlesãæ°åå¼çé¿åº¦ä¼è¢«éä½ç¨äºå¡«æ»¡æå¤ 80%ç id 空é´ãå½ optimization.moduleIds
被设置æ deterministic
ï¼é»è®¤æå° 3 ä½æ°åä¼è¢«ä½¿ç¨ãè¦è¦çé»è®¤è¡ä¸ºï¼è¦å° optimization.moduleIds
设置æ false
ï¼å¹¶ä¸è¦ä½¿ç¨ webpack.ids.DeterministicModuleIdsPlugin
ã
webpack.config.js
module.exports = {
//...
optimization: {
moduleIds: false,
},
plugins: [
new webpack.ids.DeterministicModuleIdsPlugin({
maxLength: 5,
}),
],
};
boolean = false
string
åç¥ webpack å° process.env.NODE_ENV
设置为ä¸ä¸ªç»å®å符串ãå¦æ optimization.nodeEnv
ä¸æ¯ false
ï¼åä¼ä½¿ç¨ DefinePluginï¼optimization.nodeEnv
é»è®¤å¼åå³äº modeï¼å¦æ为 false å¼ï¼åä¼åéå° "production"
ã
å¯è½çå¼æï¼
process.env.NODE_ENV
çå¼ãprocess.env.NODE_ENV
çå¼ãwebpack.config.js
module.exports = {
//...
optimization: {
nodeEnv: 'production',
},
};
boolean
optimization.portableRecords
åç¥ webpack çæ带æç¸å¯¹è·¯å¾çè®°å½(records)使å¾å¯ä»¥ç§»å¨ä¸ä¸æç®å½ã
é»è®¤ optimization.portableRecords
被ç¦ç¨ãå¦æä¸åè³å°ä¸ä¸ªéé¡¹å¨ webpack ä¸è¢«è®¾ç½®ï¼è¯¥é项ä¹ä¼èªå¨å¯ç¨ï¼recordsPath
, recordsInputPath
ï¼recordsOutputPath
ã
webpack.config.js
module.exports = {
//...
optimization: {
portableRecords: true,
},
};
boolean
åç¥ webpack å»ç¡®å®é£äºç±æ¨¡åæä¾ç导åºå
容ï¼ä¸º export * from ...
çææ´å¤é«æç代ç ãé»è®¤ optimization.providedExports
ä¼è¢«å¯ç¨ã
webpack.config.js
module.exports = {
//...
optimization: {
providedExports: false,
},
};
boolean = true
å¨å¤çéæèµæºåæ·»å é¢å¤çåå¸ç¼è¯ï¼ä»¥è·å¾æ£ç¡®çéæèµæºå
容åå¸ãå¦æ realContentHash
设置为 false
ï¼å
é¨æ°æ®ç¨äºè®¡ç®åå¸å¼ï¼å½éæèµæºç¸åæ¶ï¼å®å¯ä»¥æ¹åã
webpack.config.js
module.exports = {
//...
optimization: {
realContentHash: false,
},
};
boolean = false
å¦æ模åå·²ç»å
å«å¨ææç¶çº§æ¨¡åä¸ï¼åç¥ webpack ä» chunk ä¸æ£æµåºè¿äºæ¨¡åï¼æ移é¤è¿äºæ¨¡åãå° optimization.removeAvailableModules
设置为 true
以å¯ç¨è¿é¡¹ä¼åãå¨ production
æ¨¡å¼ ä¸é»è®¤ä¼è¢«å¼å¯ã
webpack.config.js
module.exports = {
//...
optimization: {
removeAvailableModules: true,
},
};
boolean = true
å¦æ chunk 为空ï¼åç¥ webpack æ£æµæ移é¤è¿äº chunkãå° optimization.removeEmptyChunks
设置为 false
以ç¦ç¨è¿é¡¹ä¼åã
webpack.config.js
module.exports = {
//...
optimization: {
removeEmptyChunks: false,
},
};
object
string
boolean
å° optimization.runtimeChunk
设置为 true
æ 'multiple'
ï¼ä¼ä¸ºæ¯ä¸ªå
¥å£æ·»å ä¸ä¸ªåªå«æ runtime çé¢å¤ chunkãæ¤é
ç½®çå«åå¦ä¸ï¼
webpack.config.js
module.exports = {
//...
optimization: {
runtimeChunk: {
name: (entrypoint) => `runtime~${entrypoint.name}`,
},
},
};
å¼ "single"
ä¼å建ä¸ä¸ªå¨ææçæ chunk ä¹é´å
±äº«çè¿è¡æ¶æ件ãæ¤è®¾ç½®æ¯å¦ä¸è®¾ç½®çå«åï¼
webpack.config.js
module.exports = {
//...
optimization: {
runtimeChunk: {
name: 'runtime',
},
},
};
éè¿å° optimization.runtimeChunk
设置为 object
ï¼å¯¹è±¡ä¸å¯ä»¥è®¾ç½®åªæ name
å±æ§ï¼å
¶ä¸å±æ§å¼å¯ä»¥æ¯å称æè
è¿åå称çå½æ°ï¼ç¨äºä¸º runtime chunks å½åã
é»è®¤å¼æ¯ false
ï¼æ¯ä¸ªå
¥å£ chunk ä¸ç´æ¥åµå
¥ runtimeã
webpack.config.js
module.exports = {
//...
optimization: {
runtimeChunk: {
name: (entrypoint) => `runtimechunk~${entrypoint.name}`,
},
},
};
boolean = true
string: 'flag'
åè¯ webpack å»è¾¨è¯ package.json
ä¸ç sideEffects
æ è®°æè§åï¼ä»¥è·³è¿é£äºå½å¯¼åºä¸è¢«ä½¿ç¨ä¸è¢«æ 记为ä¸å
å«å¯ä½ç¨ç模åã
package.json
{
"name": "awesome npm module",
"version": "1.0.0",
"sideEffects": false
}
optimization.sideEffects
é¦å
ä¾èµäº optimization.providedExports
被设置为å¯ç¨ãè¿ä¸ªä¾èµä¼ææ建æ¶é´çæèï¼ä½ç§»é¤æ¨¡åå°çææ´å°ç代ç ï¼å æ¤æå©äºæ§è½çæåã该ä¼åçææåå³äºä»£ç åºï¼
å¯ä»¥å°è¯æ¤ç¹æ§ä»¥è·åä¸äºå¯è½çæ§è½ä¼åã
webpack.config.js
module.exports = {
//...
optimization: {
sideEffects: true,
},
};
å¦æä¸æ³è¦åææºç ï¼é£ä¹å¯ä»¥å°å
¶æå®ä¸º flag
ï¼
module.exports = {
//...
optimization: {
sideEffects: 'flag',
},
};
æ¤å¤ç 'flag'
å¼å¨éç产ç¯å¢ä¸é»è®¤ä½¿ç¨ã
object
对äºå¨æå¯¼å ¥æ¨¡åï¼é»è®¤ä½¿ç¨ webpack v4+ æä¾çå ¨æ°çéç¨ååçç¥(common chunk strategy)ãè¯·å¨ SplitChunksPlugin 页é¢ä¸æ¥çé ç½®å ¶è¡ä¸ºçå¯ç¨é项ã
boolean = true
string: 'global'
åè¯ webpack å»å³å®æ¯ä¸ªæ¨¡åç导åºå
容æ¯å¦è¢«ä½¿ç¨ãè¿é¦å
åå³äº optimization.providedExports
é项æ¯å¦è¢«å¯ç¨ãç± optimization.usedExports
æ¶éçä¿¡æ¯ä¼è¢«å
¶å®ä¼åæ段æè
代ç çæ使ç¨ï¼æ¯å¦æªä½¿ç¨ç导åºå
容ä¸ä¼è¢«çæï¼å½ææç使ç¨é½éé
ï¼å¯¼åºå称ä¼è¢«å¤çåå个æ è®°å符ã
å缩工å
·æ§è¡æ¸
é¤æ»ä»£ç æ¶ä¼åçäºè¯¥é项ï¼èä¸è½å¤å»é¤æªä½¿ç¨ç导åºå
容ã
webpack.config.js
module.exports = {
//...
optimization: {
usedExports: false,
},
};
éæ©éåºæ¯æ¬¡è¿è¡æ¶ä½¿ç¨ export å享ï¼
module.exports = {
//...
optimization: {
usedExports: 'global',
},
};