è¿éæ¯ webpack æ¯æçå ¶å®é项ã
object
boolean: false
设置 require.amd
æ define.amd
çå¼ã设置 amd
为 false
ä¼ç¦ç¨ webpack ç AMD æ¯æã
webpack.config.js
module.exports = {
//...
amd: {
jQuery: true,
},
};
æäºæµè¡ç模åæ¯æç § AMD è§èç¼åçï¼æå¼äººç©ç®ç jQuery çæ¬å¨ 1.7.0 å° 1.9.1ï¼å¦æ loader æ示å®å¯¹é¡µé¢å å«çå¤ä¸ªçæ¬éåäºç¹æ®è®¸å¯æ¶ï¼æä¼æ³¨å为 AMD 模åã
许å¯æéæ¯å ·æãéå¶æå®çæ¬æ³¨åãæãæ¯ææä¸åå®ä¹æ¨¡åçä¸åæ²çãçè½åã
æ¤é项å 许å°æ¨¡åæ¥æ¾çé®(key)设置为çå¼(truthy value)ã åçè¿ç§æ åµæ¶ï¼webpack ä¸ç AMD æ¯æå°å¿½ç¥å®ä¹çå称ã
boolean = false
å¨ç¬¬ä¸ä¸ªé误åºç°æ¶æåºå¤±è´¥ç»æï¼èä¸æ¯å®¹å¿å®ãé»è®¤æ åµä¸ï¼å½ä½¿ç¨ HMR æ¶ï¼webpack ä¼å°å¨ç»ç«¯ä»¥åæµè§å¨æ§å¶å°ä¸ï¼ä»¥çº¢è²æåè®°å½è¿äºé误ï¼ä½ä»ç¶ç»§ç»è¿è¡æå ãè¦å¯ç¨å®ï¼
webpack.config.js
module.exports = {
//...
bail: true,
};
è¿å°è¿«ä½¿ webpack éåºå ¶æå è¿ç¨ã
[string]
ä¸ä¸ª name
å表ï¼å®ä¹å®æä¾èµçææå
å¼ï¼siblingï¼é
ç½®ãéè¦é¦å
ç¼è¯ä¾èµçé
ç½®ã
å¨ watch 模å¼ä¸ï¼å½åºç°ä»¥ä¸æ åµæ¶ï¼ä¾èµé¡¹å°ä½¿ç¼è¯å¨å¤±æï¼
请记ä½ï¼å¨å®æä¾èµé¡¹ç¼è¯ä¹åï¼ä¸ä¼ç¼è¯æ¤é ç½®ã
webpack.config.js
module.exports = [
{
name: 'client',
target: 'web',
// â¦
},
{
name: 'server',
target: 'node',
dependencies: ['client'],
},
];
[RegExp, function (WebpackError, Compilation) => boolean, {module?: RegExp, file?: RegExp, message?: RegExp}]
åè¯ webpack 忽ç¥æç¹å®çè¦åãç±»åå¯ä»¥æ¯ RegExp
ï¼å¯ä»¥æ¯èªå®ä¹ function
ãå¦æç±»å为å½æ°ï¼å¯åºäºåå§ warning æ¥éæ©æ§å±ç¤ºè¦åï¼å
¶åæ°åå«ä¸º WebpackError
å Compilation
ï¼ä¸è¿åå¼ä¸º boolean
ãè¿å¯ä»¥å
å«ä»¥ä¸å±æ§ç object
ï¼
file
ï¼ ç±»å为 RegExp
ï¼ç¨äºéæ©åºç°è¦åçæºæ件ãmessage
ï¼ ç±»å为 RegExp
ï¼ç¨äºéæ©è¦åçå
容ãmodule
ï¼ ç±»å为 RegExp
ï¼ç¨äºéæ©è¦åæ¥æºç模åãignoreWarnings
å¿
é¡»æ¯ä¸è¿°ä»»ææææç±»åç»æç array
ã
module.exports = {
//...
ignoreWarnings: [
{
module: /module2\.js\?[34]/, // A RegExp
},
{
module: /[13]/,
message: /homepage/,
},
/warning from compiler/,
(warning) => true,
],
};
ç¨äºåºç¡è®¾æ½æ°´å¹³çæ¥å¿é项ã
object = {}
boolean
å°å
容追å å°ç°æè¾åºä¸ï¼èéæ´æ°ç°æè¾åºï¼è¿å¯¹äºå±ç¤ºç¶æä¿¡æ¯æ¥è¯´é常æç¨ãæ¤é项ä»
å¨æªæä¾èªå®ä¹ console
çæ
åµä¸ä½¿ç¨ã
webpack.config.js
module.exports = {
//...
infrastructureLogging: {
appendOnly: true,
level: 'verbose',
},
plugins: [
(compiler) => {
const logger = compiler.getInfrastructureLogger('MyPlugin');
logger.status('first output'); // this line won't be overridden with `appendOnly` enabled
logger.status('second output');
},
],
};
boolean
为åºç¡è®¾æ½æ¥å¿å¯ç¨å¸¦æé¢è²çè¾åºãæ¤é项ä»
å¨æªæä¾èªå®ä¹ console
çæ
åµä¸ä½¿ç¨ã
webpack.config.js
module.exports = {
//...
infrastructureLogging: {
colors: true,
level: 'verbose',
},
plugins: [
(compiler) => {
const logger = compiler.getInfrastructureLogger('MyPlugin');
logger.log('this output will be colorful');
},
],
};
Console
为åºç¡è®¾æ½æ¥å¿æä¾èªå®ä¹æ¹æ¡ã
webpack.config.js
module.exports = {
//...
infrastructureLogging: {
console: yourCustomConsole(),
},
};
string
boolean = false
RegExp
function(name) => boolean
[string, RegExp, function(name) => boolean]
å¼å¯ç¹å®æ¥å¿æ¯å¦æ件(plugins)åå è½½å¨(loaders)çè°è¯ä¿¡æ¯ã ä¸ stats.loggingDebug
é项类似ä½ä»
ä»
对äºåºç¡è®¾æ½èè¨ãé»è®¤ä¸º false
ã
webpack.config.js
module.exports = {
//...
infrastructureLogging: {
level: 'info',
debug: ['MyPlugin', /MyPlugin/, (name) => name.contains('MyPlugin')],
},
};
string = 'info' : 'none' | 'error' | 'warn' | 'info' | 'log' | 'verbose'
å¼å¯åºç¡è®¾æ½æ¥å¿è¾åºãä¸ stats.logging
é项类似ä½ä»
ä»
æ¯å¯¹äºåºç¡è®¾æ½èè¨ãé»è®¤å¼ä¸º 'info'
ã
å¯è½çåå¼ï¼
'none'
- ç¦ç¨æ¥å¿'error'
- ä»
ä»
æ¾ç¤ºé误'warn'
- ä»
ä»
æ¾ç¤ºé误ä¸åè¦'info'
- æ¾ç¤ºé误ãåè¦ä¸ä¿¡æ¯'log'
- æ¾ç¤ºé误ãåè¦ï¼ä¿¡æ¯ï¼æ¥å¿ä¿¡æ¯ï¼ç»å«ï¼æ¸
æ¥ã æ¶ç¼©çç»å«ä¼å¨æ¶ç¼©çç¶æä¸è¢«æ¾ç¤ºã'verbose'
- è¾åºæææ¥å¿é¤äºè°è¯ä¸è¿½è¸ªãæ¶ç¼©çç»å«ä¼å¨æ©å±çç¶æä¸è¢«æ¾ç¤ºãwebpack.config.js
module.exports = {
//...
infrastructureLogging: {
level: 'info',
},
};
NodeJS.WritableStream = process.stderr
ç¨äºæ¥å¿è¾åºçæµãé»è®¤ä¸º process.stderr
ãæ¤é项ä»
å¨æªæä¾èªå®ä¹ console
çæ
åµä¸ä½¿ç¨ã
webpack.config.js
module.exports = {
//...
infrastructureLogging: {
stream: process.stderr,
},
};
object
å¨ loader ä¸ä¸æ ä¸æ´é²èªå®ä¹å¼ã
ä¾å¦ï¼ä½ å¯ä»¥å¨ loader ä¸ä¸æä¸å®ä¹ä¸ä¸ªæ°åéï¼
webpack.config.js
module.exports = {
// ...
loader: {
answer: 42,
},
};
ç¶åä½¿ç¨ this.answer
å¨ loader ä¸è·å该å¼ï¼
custom-loader.js
module.exports = function (source) {
// ...
console.log(this.answer); // will log `42` here
return source;
};
string
é ç½®çå称ãå½å è½½ä¸åçé ç½®æ¶ä¼è¢«ä½¿ç¨ã
webpack.config.js
module.exports = {
//...
name: 'admin-app',
};
number = 100
éå¶å¹¶è¡å¤çç模åæ°éãå¯ä»¥ç¨äºè°ä¼æ§è½æè·åæ´å¯é çæ§è½åæç»æã
boolean
æè·ä¸ä¸ªåºç¨ç¨åº"é ç½®æ件"ï¼å æ¬ç»è®¡åæ示ï¼ç¶åå¯ä»¥ä½¿ç¨ Analyze åæå·¥å ·è¿è¡è¯¦ç»åæã
string
æå®è¯»åæåä¸æ¡è®°å½çæ件çå称ãè¿å¯ä»¥ç¨æ¥éå½åä¸ä¸ªè®°å½æ件ï¼å¯ä»¥æ¥çä¸é¢çå®ä¾ï¼
string
æå®è®°å½è¦åå
¥çä½ç½®ã以ä¸ç¤ºä¾æè¿°äºå¦ä½ç¨è¿ä¸ªé项å recordsInptuPaht
æ¥éå½åä¸ä¸ªè®°å½æ件ï¼
webpack.config.js
const path = require('path');
module.exports = {
//...
recordsInputPath: path.join(__dirname, 'records.json'),
recordsOutputPath: path.join(__dirname, 'newRecords.json'),
};
string
å¼å¯è¿ä¸ªé项å¯ä»¥çæä¸ä¸ª JSON æ件ï¼å ¶ä¸å«æ webpack ç "records" è®°å½ - å³ãç¨äºåå¨è·¨å¤æ¬¡æ建(across multiple builds)ç模åæ è¯ç¬¦ãçæ°æ®ç段ãå¯ä»¥ä½¿ç¨æ¤æ件æ¥è·è¸ªå¨æ¯æ¬¡æ建ä¹é´ç模åååãåªè¦ç®åç设置ä¸ä¸è·¯å¾,å°±å¯ä»¥çæè¿ä¸ª JSON æ件ï¼
webpack.config.js
const path = require('path');
module.exports = {
//...
recordsPath: path.join(__dirname, 'records.json'),
};
å¦æä½ ä½¿ç¨äºä»£ç å离(code splittnig)è¿æ ·çå¤æé ç½®ï¼records ä¼ç¹å«æç¨ãè¿äºæ°æ®ç¨äºç¡®ä¿æå bundleï¼ä»¥ä¾¿å®ç°ä½ éè¦çç¼å(caching)è¡ä¸ºã
object
snapshot
é
置项å³å®æ件系ç»æ¯å¦ä½å建åæ æå¿«ç
§ã
webpack.config.js
const path = require('path');
module.exports = {
// ...
snapshot: {
managedPaths: [path.resolve(__dirname, '../node_modules')],
immutablePaths: [],
buildDependencies: {
hash: true,
timestamp: true,
},
module: {
timestamp: true,
},
resolve: {
timestamp: true,
},
resolveBuildDependencies: {
hash: true,
timestamp: true,
},
},
};
object = { hash boolean = true, timestamp boolean = true }
使ç¨æä¹ åç¼åæ¶çä¾èµæå»ºå ³ç³»å¿«ç §ã
hash
ï¼æ¯è¾å
容åå¸ä»¥ç¡®å®æ æï¼æ¯ timestamp
æ´æè´µï¼ä½æ´æ¹çé¢çè¾ä½ï¼ãtimestamp
ï¼æ¯è¾ timestamps 以确å®æ æãhash
ä¸ timestamp
é½æ¯å¯éé
ç½®ã
{ hash: true }
ï¼å¯¹ CI ç¼åå¾æ帮å©ï¼ä½¿ç¨æ°ç checkoutï¼ä¸éè¦ä¿åæ¶é´æ³ï¼å¹¶ä¸ä½¿ç¨åå¸ã{ timestamp: true }
ï¼å¯¹åºæ¬å°å¼åç¼åå¾ç¨å¸®å©ã{ timestamp: true, hash: true }
ï¼å¯¹äºä»¥ä¸æå°ç两è
é½å¾æ帮å©ãé¦å
æ¯è¾æ¶é´æ³ï¼è¿ä»£ä»·å¾ä½ï¼å 为 webpack ä¸éè¦è¯»åæ件æ¥è®¡ç®å®ä»¬çåå¸å¼ãä»
å½æ¶é´æ³ç¸åæ¶æä¼æ¯è¾å
容åå¸ï¼è¿å¯¹åå§æ建çæ§è½å½±åå¾å°ã(RegExp | string)[]
ç±å 管çå¨ç®¡ççè·¯å¾æ°ç»ï¼å¨å ¶è·¯å¾ä¸å å«ä¸ä¸ªçæ¬æåå¸ï¼ä»¥ä¾¿æææ件é½æ¯ä¸å¯åçï¼immutableï¼ã
å¦æ使ç¨æ£å表达å¼ï¼è¯·ç¡®ä¿å°è·¯å¾å 裹å¨æè·ç»ä¸ã
å¦æ使ç¨æ£å表达å¼ï¼è¯·ç¡®ä¿å°è·¯å¾å 裹å¨æè·ç»ä¸ã
(RegExp | string)[]
ç±å 管çå¨ç®¡ççè·¯å¾æ°ç»ï¼å¯ä»¥ä¿¡ä»»å®ä¸ä¼è¢«ä¿®æ¹ã
å¦æ使ç¨æ£å表达å¼ï¼è¯·ç¡®ä¿å°è·¯å¾å 裹å¨æè·ç»ä¸ã
object = {hash boolean = true, timestamp boolean = true}
æ建模åçå¿«ç §ã
hash
ï¼æ¯è¾å
容åå¸ä»¥å¤ææ æãï¼æ¯ timestamp
æ´æè´µï¼ä½æ´æ¹çé¢çè¾ä½ï¼ãtimestamp
ï¼æ¯è¾æ¶é´æ³ä»¥ç¡®å®æ æãobject = {hash boolean = true, timestamp boolean = true}
解æ请æ±çå¿«ç §ã
hash
ï¼æ¯è¾å
容åå¸ä»¥å¤ææ æãï¼æ¯ timestamp
æ´æè´µï¼ä½æ´æ¹çé¢çè¾ä½ï¼ãtimestamp
ï¼æ¯è¾æ¶é´æ³ä»¥ç¡®å®æ æãobject = {hash boolean = true, timestamp boolean = true}
使ç¨æä¹ ç¼åæ¶ç¨äºè§£ææ建ä¾èµé¡¹çå¿«ç §ã
hash
ï¼æ¯è¾å
容åå¸ä»¥å¤ææ æãï¼æ¯ timestamp
æ´æè´µï¼ä½æ´æ¹çé¢çè¾ä½ï¼ãtimestamp
ï¼æ¯è¾æ¶é´æ³ä»¥ç¡®å®æ æã