è¿äºé项å³å®äºå¦ä½å¤ç项ç®ä¸çä¸åç±»åç模åã
å¯ä»¥ä½¿ç¨ module.generator
å¨ä¸ä¸ªå°æ¹é
ç½®ææçæå¨çé项ã
webpack.config.js
module.exports = {
module: {
generator: {
asset: {
// asseet 模åç generator é项
// èªå®ä¹ asset 模åç publicPathï¼èª webpack 5.28.0 èµ·å¯ç¨
publicPath: 'assets/',
// å°éæèµæºè¾åºå°ç¸å¯¹äº 'output.path' çæå®æ件夹ä¸ï¼webpack 5.67.0 åå¯ç¨
outputPath: 'cdn-assets/',
},
'asset/inline': {
// asset/å
è模åç generator é项
},
'asset/resource': {
// asset/èµæºæ¨¡åç generator é项
// èªå®ä¹ asset/resource 模åç publicPathï¼èª webpack 5.28.0 èµ·å¯ç¨
publicPath: 'assets/',
// å°éæèµæºè¾åºå°ç¸å¯¹äº 'output.path' çæå®æ件夹ä¸ï¼webpack 5.67.0 åå¯ç¨
outputPath: 'cdn-assets/',
},
javascript: {
// 该模åç±»åå°ä¸æ¯æ generator é项
},
'javascript/auto': {
// åä¸
},
'javascript/dynamic': {
// åä¸
},
'javascript/esm': {
// åä¸
},
// å
¶ä»...
},
},
};
ç±»ä¼¼äº module.generator
ï¼ä½ å¯ä»¥ç¨ module.parser
å¨ä¸ä¸ªå°æ¹é
ç½®ææ解æå¨çé项ã
webpack.config.js
module.exports = {
module: {
parser: {
asset: {
// èµäº§æ¨¡åç parser é项
},
'asset/inline': {
// 该模åç±»åå°ä¸æ¯æ parser é项
},
'asset/resource': {
// åä¸
},
'asset/source': {
// åä¸
},
javascript: {
// javascript 模åç解æå¨é项
// ä¾å¦ï¼å¯ç¨è§£æ require.ensure è¯æ³çåè½
requireEnsure: true,
},
'javascript/auto': {
// åä¸
},
'javascript/dynamic': {
// åä¸
},
'javascript/esm': {
// åä¸
},
// å
¶ä»...
},
},
};
JavaScript parser çé 置项ã
module.exports = {
module: {
parser: {
javascript: {
// ...
commonjsMagicComments: true,
},
},
},
};
å
è®¸å¨ Rule.parser
ä¸é
ç½®è¿äºé项ãä¹å¯ä»¥æ¯ç¹å®æ¨¡åã
为 CommonJS å¯ç¨ éæ³æ³¨éã
ç±»åï¼boolean
å¯ç¨çæ¬ï¼5.17.0+
示ä¾ï¼
module.exports = {
module: {
parser: {
javascript: {
commonjsMagicComments: true,
},
},
},
};
请注æï¼ç®ååªæ¯æ webpackIgnore
注éï¼
const x = require(/* webpackIgnore: true */ 'x');
æå®å¨æå¯¼å ¥çå ¨å±æ¨¡å¼ã
ç±»åï¼'eager' | 'weak' | 'lazy' | 'lazy-once'
å¯ç¨çæ¬ï¼5.73.0+
示ä¾ï¼
module.exports = {
module: {
parser: {
javascript: {
dynamicImportMode: 'lazy',
},
},
},
};
æå®å¨æå¯¼å ¥çå ¨å± prefetchã
ç±»åï¼ number | boolean
å¯ç¨çæ¬ï¼5.73.0+
示ä¾ï¼
module.exports = {
module: {
parser: {
javascript: {
dynamicImportPrefetch: false,
},
},
},
};
æå®å¨æå¯¼å ¥çå ¨å± preloadã
ç±»åï¼ number | boolean
å¯ç¨çæ¬ï¼5.73.0+
示ä¾ï¼
module.exports = {
module: {
parser: {
javascript: {
dynamicImportPreload: false,
},
},
},
};
æåºå¨ \"import ... from ...\"
ä¸ \"export ... from ...\"
ä¸æ æ导åºå称çè¡ä¸ºã
ç±»åï¼'error' | 'warn' | 'auto' | false
å¯ç¨çæ¬ï¼5.62.0+
示ä¾ï¼
module.exports = {
module: {
parser: {
javascript: {
exportsPresence: 'error',
},
},
},
};
æåºå¨ \"import ... from ...\"
ä¸æ æ导åºå称çè¡ä¸ºã
ç±»åï¼'error' | 'warn' | 'auto' | false
å¯ç¨çæ¬ï¼5.62.0+
示ä¾ï¼
module.exports = {
module: {
parser: {
javascript: {
importExportsPresence: 'error',
},
},
},
};
Enable or disable evaluating import.meta
.
ç±»åï¼boolean = true
å¯ç¨çæ¬ï¼5.68.0+
示ä¾ï¼
module.exports = {
module: {
parser: {
javascript: {
importMeta: false,
},
},
},
};
å¯ç¨/ç¦ç¨ import.meta.webpackContext
åæã
ç±»åï¼boolean
å¯ç¨çæ¬ï¼5.70.0+
示ä¾ï¼
module.exports = {
module: {
parser: {
javascript: {
importMetaContext: true,
},
},
},
};
æåºå¨ \"export ... from ...\"
ä¸æ æ导åºå称çè¡ä¸ºãå½å¨ TypeScript éæ°å¯¼åºç±»åï¼ä» \"export ... from ...\"
è¿ç§»å° \"export type ... from ...\"
æ¶ç¦ç¨è¯¥é
置项æ¯æç¨çã
ç±»åï¼'error' | 'warn' | 'auto' | false
å¯ç¨çæ¬ï¼5.62.0+
示ä¾ï¼
module.exports = {
module: {
parser: {
javascript: {
reexportExportsPresence: 'error',
},
},
},
};
å¯ç¨ new URL()
è¯æ³è§£æã
ç±»åï¼boolean = true
| 'relative'
示ä¾:
module.exports = {
module: {
parser: {
javascript: {
url: false, // disable parsing of `new URL()` syntax
},
},
},
};
èª webpack 5.23.0 èµ·ï¼module.parser.javascript.url
ç 'relative'
å¼å¯ç¨ãå½ä½¿ç¨ 'relative'
æ¶ï¼webpack å°ä¸º new URL()
è¯æ³çæç¸å¯¹ç URLï¼å³ç»æ URL ä¸ä¸å
å«æ ¹ URLï¼
<!-- with 'relative' -->
<img src="c43188443804f1b1f534.svg" />
<!-- without 'relative' -->
<img src="file:///path/to/project/dist/c43188443804f1b1f534.svg" />
RegExp
[RegExp]
function(resource)
string
[string]
é²æ¢ webpack 解æé£äºä»»ä½ä¸ç»å®æ£å表达å¼ç¸å¹é
çæ件ã忽ç¥çæä»¶ä¸ ä¸åºè¯¥å«æ import
, require
, define
çè°ç¨ï¼æä»»ä½å
¶ä»å¯¼å
¥æºå¶ã忽ç¥å¤§åç library å¯ä»¥æé«æ建æ§è½ã
webpack.config.js
module.exports = {
//...
module: {
noParse: /jquery|lodash/,
},
};
module.exports = {
//...
module: {
noParse: (content) => /jquery|lodash/.test(content),
},
};
boolean
function (module)
ç¼å模å请æ±ç解æãmodule.unsafeCache
å
å«å¦ä¸å 个é»è®¤å¼ï¼
cache
æªè¢«å¯ç¨ï¼åé»è®¤å¼ä¸º false
ãcache
被å¯ç¨ï¼å¹¶ä¸æ¤æ¨¡åçæ¥èª node_modulesï¼åå¼ä¸º true
ï¼å¦å为 false
ãwebpack.config.js
module.exports = {
//...
module: {
unsafeCache: false,
},
};
[Rule]
å建模åæ¶ï¼å¹é 请æ±çè§åæ°ç»ãè¿äºè§åè½å¤ä¿®æ¹æ¨¡åçå建æ¹å¼ã è¿äºè§åè½å¤å¯¹æ¨¡å(module)åºç¨ loaderï¼æè ä¿®æ¹è§£æå¨(parser)ã
object
æ¯ä¸ªè§åå¯ä»¥å为ä¸é¨å - æ¡ä»¶(condition)ï¼ç»æ(result)ååµå¥è§å(nested rule)ã
æ¡ä»¶æ两ç§è¾å ¥å¼ï¼
resourceï¼èµæºæ件çç»å¯¹è·¯å¾ãå®å·²ç»æ ¹æ® resolve
è§å解æã
issuer: 请æ±è çæ件ç»å¯¹è·¯å¾ãæ¯å¯¼å ¥æ¶çä½ç½®ã
ä¾å¦ï¼ ä» app.js
导å
¥ './style.css'
ï¼resource æ¯ /path/to/style.css
. issuer æ¯ /path/to/app.js
ã
å¨è§åä¸ï¼å±æ§ test
, include
, exclude
å resource
对 resource å¹é
ï¼å¹¶ä¸å±æ§ issuer
对 issuer å¹é
ã
å½ä½¿ç¨å¤ä¸ªæ¡ä»¶æ¶ï¼æææ¡ä»¶é½å¹é ã
è§åç»æåªå¨è§åæ¡ä»¶å¹é æ¶ä½¿ç¨ã
è§åæ两ç§è¾å ¥å¼ï¼
è¿äºå±æ§ä¼å½±å loaderï¼loader
, options
, use
ã
ä¹å
¼å®¹è¿äºå±æ§ï¼query
, loaders
ã
enforce
å±æ§ä¼å½±å loader ç§ç±»ãä¸è®ºæ¯æ®éçï¼åç½®çï¼åç½®ç loaderã
parser
å±æ§ä¼å½±å parser é项ã
å¯ä»¥ä½¿ç¨å±æ§ rules
å oneOf
æå®åµå¥è§åã
è¿äºè§åç¨äºå¨è§åæ¡ä»¶(rule condition)å¹é æ¶è¿è¡åå¼ãæ¯ä¸ªåµå¥è§åå å«å®èªå·±çæ¡ä»¶ã
被解æç顺åºåºäºä»¥ä¸è§åï¼
string
å¯è½çå¼æï¼"pre" | "post"
æå® loader ç§ç±»ã没æå¼è¡¨ç¤ºæ¯æ®é loaderã
è¿æä¸ä¸ªé¢å¤çç§ç±»"è¡å loader"ï¼loader 被åºç¨å¨ import/require è¡å ã
ææä¸ä¸ªæ¥ä¸ä¸ªå°è¿å ¥ç loaderï¼é½æ两个é¶æ®µï¼
åç½®(post)ãè¡å
(inline)ãæ®é(normal)ãåç½®(pre)
ç顺åºè°ç¨ãæ´å¤è¯¦ç»ä¿¡æ¯ï¼è¯·æ¥ç Pitching Loaderãåç½®(pre)ãæ®é(normal)ãè¡å
(inline)ãåç½®(post)
ç顺åºè°ç¨ã模åæºç ç转æ¢ï¼
åçå¨è¿ä¸ªé¶æ®µãæææ®é loader å¯ä»¥éè¿å¨è¯·æ±ä¸å ä¸ !
åç¼æ¥å¿½ç¥ï¼è¦çï¼ã
æææ®éååç½® loader å¯ä»¥éè¿å¨è¯·æ±ä¸å ä¸ -!
åç¼æ¥å¿½ç¥ï¼è¦çï¼ã
æææ®éï¼åç½®ååç½® loader å¯ä»¥éè¿å¨è¯·æ±ä¸å ä¸ !!
åç¼æ¥å¿½ç¥ï¼è¦çï¼ã
// ç¦ç¨æ®é loaders
import { a } from '!./file1.js';
// ç¦ç¨åç½®åæ®é loaders
import { b } from '-!./file2.js';
// ç¦ç¨ææç laoders
import { c } from '!!./file3.js';
ä¸åºä½¿ç¨å
è loader å !
åç¼ï¼å 为å®æ¯éæ åçãå®ä»¬å¯è½ä¼è¢« loader çæ代ç 使ç¨ã
æé¤ææ符åæ¡ä»¶ç模åãå¦æä½ æä¾äº Rule.exclude
é项ï¼å°±ä¸è½åæä¾ Rule.resource
ã详ç»è¯·æ¥ç Rule.resource
å Condition.exclude
ã
å¼å
¥ç¬¦å以ä¸ä»»ä½æ¡ä»¶ç模åãå¦æä½ æä¾äº Rule.include
é项ï¼å°±ä¸è½åæä¾ Rule.resource
ã详ç»è¯·æ¥ç Rule.resource
å Condition.include
ã
ä¸ä¸ªæ¡ä»¶
ï¼ç¨æ¥ä¸è¢«ååºç request 对åºç模å项å¹é
ãå¨ä»¥ä¸ç¤ºä¾ä¸ï¼a.js
request çååºè
(issuer)
æ¯ index.js æ件çè·¯å¾ã
index.js
import A from './a.js';
è¿ä¸ªé项å¯ä»¥ç¨æ¥å° loader åºç¨å°ä¸ä¸ªç¹å®æ¨¡åæä¸ç»æ¨¡åçä¾èµä¸ã
å 许æ issuer ç layer è¿è¡è¿æ»¤/å¹é ã
webpack.config.js
module.exports = {
// ...
module: {
rules: [
{
issuerLayer: 'other-layer',
},
],
},
};
string
æå®æ¨¡ååºæ¾ç½®å¨åªä¸ª layerãä¸ç»æ¨¡åå¯ä»¥ç»ä¸å¨ä¸ä¸ª layer ä¸ï¼ç¶åå¯ä»¥è¢«ç¨å¨ split chunksãstats æè entry optionsã
webpack.config.js
module.exports = {
// ...
module: {
rules: [
{
test: /module-layer-change/,
layer: 'layer',
},
],
},
};
Rule.loader
æ¯ Rule.use: [ { loader } ]
çç®åã详ç»è¯·æ¥ç Rule.use
å UseEntry.loader
ã
Rule.loaders
æ¯ Rule.use
çå«åã详ç»è¯·æ¥ç Rule.use
ã
ä½ å¯ä»¥ä½¿ç¨ mimetype
使 rules é
ç½®ä¸ data ç uri è¿è¡å¹é
ã
webpack.config.js
module.exports = {
// ...
module: {
rules: [
{
mimetype: 'application/json',
type: 'json',
},
],
},
};
mimetype å·²é»è®¤å
å«äº application/json
ï¼text/javascript
ï¼application/javascript
ï¼application/node
以å application/wasm
ã
è§å
æ°ç»ï¼å½è§åå¹é
æ¶ï¼åªä½¿ç¨ç¬¬ä¸ä¸ªå¹é
è§åã
webpack.config.js
module.exports = {
//...
module: {
rules: [
{
test: /\.css$/,
oneOf: [
{
resourceQuery: /inline/, // foo.css?inline
use: 'url-loader',
},
{
resourceQuery: /external/, // foo.css?external
use: 'file-loader',
},
],
},
],
},
};
Rule.options
å Rule.query
æ¯ Rule.use: [ { options } ]
çç®åã详ç»è¯·æ¥ç Rule.use
å UseEntry.options
ã
解æé项对象ãææåºç¨ç解æé项é½å°å并ã
解æå¨(parser)å¯ä»¥æ¥é è¿äºé项ï¼å¹¶ç¸åºå°ç¦ç¨æéæ°é ç½®ã大å¤æ°é»è®¤æä»¶ï¼ ä¼å¦ä¸è§£éå¼ï¼
false
ï¼å°ç¦ç¨è§£æå¨ãtrue
ï¼æä¸ä¿®æ¹å°å
¶ä¿ç为 undefined
ï¼å¯ä»¥å¯ç¨è§£æå¨ãç¶èï¼ä¸äºè§£æå¨(parser)æ件å¯è½ä¸å
åªæ¥æ¶ä¸ä¸ªå¸å°å¼ãä¾å¦ï¼å
é¨ç NodeStuffPlugin
æ件ï¼å¯ä»¥æ¥æ¶ä¸ä¸ªå¯¹è±¡ï¼èä¸æ¯ true
ï¼
æ¥ä¸ºç¹å®çè§åæ·»å é¢å¤çé项ã
示ä¾ï¼é»è®¤çæ件解æå¨é项ï¼ï¼
module.exports = {
//...
module: {
rules: [
{
//...
parser: {
amd: false, // ç¦ç¨ AMD
commonjs: false, // ç¦ç¨ CommonJS
system: false, // ç¦ç¨ SystemJS
harmony: false, // ç¦ç¨ ES2015 Harmony import/export
requireInclude: false, // ç¦ç¨ require.include
requireEnsure: false, // ç¦ç¨ require.ensure
requireContext: false, // ç¦ç¨ require.context
browserify: false, // ç¦ç¨ç¹æ®å¤çç browserify bundle
requireJs: false, // ç¦ç¨ requirejs.*
node: false, // ç¦ç¨ __dirname, __filename, module, require.extensions, require.main, çã
commonjsMagicComments: false, // ç¦ç¨å¯¹ CommonJS ç magic comments æ¯æ
node: {}, // å¨æ¨¡å级å«(module level)ä¸éæ°é
ç½® node å±(layer)
worker: ['default from web-worker', '...'], // èªå®ä¹ WebWorker 对 JavaScript çå¤çï¼å
¶ä¸ "..." 为é»è®¤å¼ã
},
},
],
},
};
å¦æ Rule.type
çå¼ä¸º asset
ï¼é£ä¹ Rules.parser
é项å¯è½æ¯ä¸ä¸ªå¯¹è±¡æä¸ä¸ªå½æ°ï¼å
¶ä½ç¨å¯è½æ¯å°æ件å
容ç¼ç 为 Base64ï¼è¿å¯è½æ¯å°å
¶å½ååç¬æ件 emit å°è¾åºç®å½ã
å¦æ Rule.type
çå¼ä¸º asset
æ asset/inline
ï¼é£ä¹ Rule.generator
é项å¯è½æ¯ä¸ä¸ªæ述模åæºç¼ç æ¹å¼ç对象ï¼è¿å¯è½æ¯ä¸ä¸ªéè¿èªå®ä¹ç®æ³å¯¹æ¨¡åæºä»£ç è¿è¡ç¼ç çå½æ°ã
æ´å¤ä¿¡æ¯åç¨ä¾è¯·æ¥é Asset 模åæåã
object = { maxSize number = 8096 }
function (source, { filename, module }) => boolean
å¦æä¸ä¸ªæ¨¡åæºç 大å°å°äº maxSize
ï¼é£ä¹æ¨¡åä¼è¢«ä½ä¸ºä¸ä¸ª Base64 ç¼ç çå符串注å
¥å°å
ä¸ï¼
å¦å模åæ件ä¼è¢«çæå°è¾åºçç®æ ç®å½ä¸ã
webpack.config.js
module.exports = {
//...
module: {
rules: [
{
//...
parser: {
dataUrlCondition: {
maxSize: 4 * 1024,
},
},
},
],
},
};
å½æä¾å½æ°æ¶ï¼è¿å true
å¼æ¶åç¥ webpack å°æ¨¡åä½ä¸ºä¸ä¸ª Base64 ç¼ç çå符串注å
¥å°å
ä¸ï¼
å¦å模åæ件ä¼è¢«çæå°è¾åºçç®æ ç®å½ä¸ã
webpack.config.js
module.exports = {
//...
module: {
rules: [
{
//...
parser: {
dataUrlCondition: (source, { filename, module }) => {
const content = source.toString();
return content.includes('some marker');
},
},
},
],
},
};
object = { encoding string = 'base64' | false, mimetype string = undefined | false }
function (content, { filename, module }) => string
å½ Rule.generator.dataUrl
被ç¨ä½ä¸ä¸ªå¯¹è±¡ï¼ä½ å¯ä»¥é
置两个å±æ§ï¼
'base64'
ï¼æ¨¡åæºç ä¼ç¨ Baes64 ç®æ³ ç¼ç ã设置 encoding
为 falseï¼ä¼ç¦ç¨ç¼ç ãwebpack.config.js
module.exports = {
//...
module: {
rules: [
{
//...
generator: {
dataUrl: {
encoding: 'base64',
mimetype: 'mimetype/png',
},
},
},
],
},
};
å½è¢«ä½ä¸ºä¸ä¸ªå½æ°ä½¿ç¨ï¼å®å¿ 须为æ¯ä¸ªæ¨¡åæ§è¡ä¸å¹¶é¡»è¿åä¸ä¸ªæ°æ®é¾æ¥(data URI)å符串ã
module.exports = {
//...
module: {
rules: [
{
//...
generator: {
dataUrl: (content) => {
const svgToMiniDataURI = require('mini-svg-data-uri');
if (typeof content !== 'string') {
content = content.toString();
}
return svgToMiniDataURI(content);
},
},
},
],
},
};
é ç½®ä¸ä» Asset Modules åå ¥èµæºæ件, ä½ å¯è½ä¼å¨ SSR ä¸ä½¿ç¨å®ã
ç±»åï¼boolean = true
å¯ç¨çæ¬ï¼5.25.0+
示ä¾ï¼
module.exports = {
// â¦
module: {
rules: [
{
test: /\.png$/i,
type: 'asset/resource',
generator: {
emit: false,
},
},
],
},
};
对æäºç¹å®çè§åèè¨ä¸ output.assetModuleFilename
ç¸å. è¦çäº output.assetModuleFilename
é项并ä¸ä»
ä¸ asset
å asset/resource
模åç±»åä¸åèµ·ä½ç¨ã
webpack.config.js
module.exports = {
//...
output: {
assetModuleFilename: 'images/[hash][ext][query]',
},
module: {
rules: [
{
test: /\.png/,
type: 'asset/resource',
},
{
test: /\.html/,
type: 'asset/resource',
generator: {
filename: 'static/[hash][ext]',
},
},
],
},
};
对æå®çèµæºæ¨¡å¼æå® publicPath
ã
string | ((pathData: PathData, assetInfo?: AssetInfo) => string)
module.exports = {
//...
output: {
publicPath: 'static/',
},
module: {
rules: [
{
test: /\.png$/i,
type: 'asset/resource',
generator: {
publicPath: 'assets/',
},
},
],
},
};
å°éæèµæºè¾åºå°ç¸å¯¹äº 'output.path' çæå®æ件夹ä¸ãåªæå½ 'publicPath' 被ç¨æ¥å¹é æ件夹ç»ææ¶æä¼éè¦è®¾ç½®è¯¥é ç½®ã
string | ((pathData: PathData, assetInfo?: AssetInfo) => string)
module.exports = {
//...
output: {
publicPath: 'static/',
},
module: {
rules: [
{
test: /\.png$/i,
type: 'asset/resource',
generator: {
publicPath: 'https://cdn/assets/',
outputPath: 'cdn-assets/',
},
},
],
},
};
æ¡ä»¶
ä¼å¹é
resourceãå¨ Rule
æ¡ä»¶ ä¸æ¥é
详æ
ã
ä¸èµæºæ¥è¯¢ç¸å¹é
ç Condition
ãæ¤é项ç¨äºæµè¯è¯·æ±å符串çæ¥è¯¢é¨åï¼å³ä»é®å·å¼å§ï¼ãå¦æä½ éè¦éè¿ import Foo from './foo.css?inline'
导å
¥ Fooï¼åé符å以ä¸æ¡ä»¶ï¼
webpack.config.js
module.exports = {
//...
module: {
rules: [
{
test: /\.css$/,
resourceQuery: /inline/,
use: 'url-loader',
},
],
},
};
function(input) => string | object
å¦æ Rule.type
被设置æ 'json'
ï¼é£ä¹ Rules.parser.parse
éæ©å¯è½ä¼æ¯ä¸ä¸ªæ¹æ³ï¼è¯¥æ¹æ³å®ç°èªå®ä¹çé»è¾ï¼ä»¥è§£æ模åçæºå并å°å®è½¬æ¢æ JavaScript 对象
ã
å®å¯è½å¨æ²¡æç¹å®å è½½å¨çæ¶åï¼å¯¹å° toml
, yaml
åå
¶å®é JSON æ件导å
¥æ导å
¥é常æç¨ï¼
webpack.config.js
const toml = require('toml');
module.exports = {
//...
module: {
rules: [
{
test: /\.toml/,
type: 'json',
parser: {
parse: toml.parse,
},
},
],
},
};
è§å
æ°ç»ï¼å½è§åå¹é
æ¶ä½¿ç¨ã
å¹é
使ç¨ç schemaï¼ä¾å¦ data
ï¼http
ã
string | RegExp | ((value: string) => boolean) | RuleSetLogicalConditions | RuleSetCondition[]
webpack.config.js
module.exports = {
module: {
rules: [
{
scheme: 'data',
type: 'asset/resource',
},
],
},
};
bool
表æ模åçåªä¸é¨ä»½å å«å¯ä½ç¨ã请åé Tree Shaking 以äºè§£æ´å¤ä¿¡æ¯ã
å¼å
¥ææéè¿æè¨æµè¯ç模åãå¦æä½ æä¾äºä¸ä¸ª Rule.test
é项ï¼å°±ä¸è½åæä¾ Rule.resource
ã详ç»è¯·æ¥ç Rule.resource
å Condition.test
ã
string
å¯è®¾ç½®å¼: 'javascript/auto' | 'javascript/dynamic' | 'javascript/esm' | 'json' | 'webassembly/sync' | 'webassembly/async' | 'asset' | 'asset/source' | 'asset/resource' | 'asset/inline'
Rule.type
设置类åç¨äºå¹é
模åãå®é²æ¢äº defaultRules
åå®ä»¬çé»è®¤å¯¼å
¥è¡ä¸ºåçãä¾å¦ï¼å¦æä½ æ³
éè¿èªå®ä¹ loader å è½½ä¸ä¸ª .json
æ件ï¼ä½ ä¼éè¦å° type
设置为 javascript/auto
以ç»è¿ webpack å
ç½®ç json 导å
¥ã
ï¼è¯¦åv4.0 æ´æ°æ¥å¿ï¼
webpack.config.js
module.exports = {
//...
module: {
rules: [
//...
{
test: /\.json$/,
type: 'javascript/auto',
loader: 'custom-json-loader',
},
],
},
};
å ³äº
asset*
ç±»å详å èµæºæ¨¡åæå ã
[UseEntry]
function(info)
[UseEntry]
Rule.use
å¯ä»¥æ¯ä¸ä¸ªåºç¨äºæ¨¡åç UseEntries æ°ç»ãæ¯ä¸ªå
¥å£(entry)æå®ä½¿ç¨ä¸ä¸ª loaderã
ä¼ éå符串ï¼å¦ï¼use: [ 'style-loader' ]
ï¼æ¯ loader å±æ§çç®åæ¹å¼ï¼å¦ï¼use: [ { loader: 'style-loader'} ]
ï¼ã
Loaders å¯ä»¥éè¿ä¼ å ¥å¤ä¸ª loaders 以达å°é¾å¼è°ç¨çææï¼å®ä»¬ä¼ä»å³å°å·¦è¢«åºç¨ï¼ä»æåå°æå é ç½®ï¼ã
webpack.config.js
module.exports = {
//...
module: {
rules: [
{
//...
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
},
},
{
loader: 'less-loader',
options: {
noIeCompat: true,
},
},
],
},
],
},
};
function(info)
Rule.use
ä¹å¯ä»¥æ¯ä¸ä¸ªå½æ°ï¼æ¥å对象åæ°ï¼æ述被å è½½ç模åï¼èä¸å¿
é¡»
è¿å UseEntry
å
ç´ çæ°ç»ã
info
对象åæ°æ以ä¸çå段ï¼
compiler
: å½å webpack çç¼è¯å¨ï¼å¯ä»¥æ¯ undefined å¼ï¼issuer
: 模åçè·¯å¾ï¼è¯¥å
ç´ æ£å¨å¯¼å
¥ä¸ä¸ªè¢«å è½½ç模å(resource)realResource
: æ»ä¼æ¯è¢«å 载模åçè·¯å¾resource
: 被å è½½ç模åè·¯å¾ï¼å®å¸¸å¸¸ä¸ realResource
ç¸çï¼åªæå½èµæºå称被 request å符串ä¸ç !=!
è¦çæ¶æä¸ç¸çç¸åçä½ä¸ºæ°ç»çå¿«æ·æ¹å¼å¯ä»¥ç¨ä½è¿åå¼ï¼å¦ use: [ 'style-loader' ]
ï¼ã
webpack.config.js
module.exports = {
//...
module: {
rules: [
{
use: (info) => [
{
loader: 'custom-svg-loader',
},
{
loader: 'svgo-loader',
options: {
plugins: [
{
cleanupIDs: {
prefix: basename(info.resource),
},
},
],
},
},
],
},
],
},
};
åå UseEntryã
模å解æå¯ä»¥å¨æ¨¡åå±è¢«é ç½®ãresolve é 置页é¢å¯æ¥çææå¯ç¨çé ç½®ã ææ被åºç¨ç resolve é项被æ´é«å±çº§çresolveé ç½®å并ã
ä¾å¦ï¼å设æ们æä¸ä¸ªå
¥å£æä»¶å¨ ./src/index.js
, ./src/footer/default.js
åä¸ä¸ª ./src/footer/overridden.js
ï¼
ç¨ä»¥è®ºè¯æ¨¡åå±çº§ç解æã
./src/index.js
import footer from 'footer';
console.log(footer);
./src/footer/default.js
export default 'default footer';
./src/footer/overridden.js
export default 'overridden footer';
webpack.js.org
module.exports = {
resolve: {
alias: {
footer: './footer/default.js',
},
},
};
å½ç¨è¯¥é
ç½®å建ä¸ä¸ªå
ï¼console.log(footer)
ä¼è¾åº 'default footer'ã让æ们为 .js
æ件设置 Rule.resolve
ï¼ä»¥å
footer
ä½ä¸º overridden.js
çå«åã
webpack.js.org
module.exports = {
resolve: {
alias: {
footer: './footer/default.js',
},
},
module: {
rules: [
{
resolve: {
alias: {
footer: './footer/overridden.js',
},
},
},
],
},
};
å½ç¨è¯¥é
ç½®å建ä¸ä¸ªå
ï¼console.log(footer)
ä¼è¾åº 'overridden footer'ã
boolean = true
å¯ç¨åï¼ä½ è¥å¨ .mjs
æ件æå
¶ä» .js
æ件ä¸å¯¼å
¥æ¨¡åï¼å¹¶ä¸å®ä»¬æè¿ç package.json
ä¸å
å« "type"
å段ï¼å
¶å¼ä¸º "module"
æ¶ï¼ä½ åºä¸ºæ¤æ件æä¾æ©å±åï¼å¦å webpack ä¼æ示 Module not found
çé误ä¸ç¼è¯å¤±è´¥ãå¹¶ä¸ webpack ä¸ä¼è§£æ resolve.mainFiles
ä¸å®ä¹çæ件ç®å½ï¼ä½ å¿
é¡»èªå·±æå®æ件åã
webpack.config.js
module.exports = {
// ...
module: {
rules: [
{
test: /\.m?js$/,
resolve: {
fullySpecified: false, // disable the behaviour
},
},
],
},
};
æ¡ä»¶å¯ä»¥æ¯è¿äºä¹ä¸ï¼
{ and: [Condition] }
ï¼å¿
é¡»å¹é
æ°ç»ä¸çæææ¡ä»¶
{ or: [Condition] }
ï¼å¹é
æ°ç»ä¸ä»»ä½ä¸ä¸ªæ¡ä»¶
{ not: [Condition] }
ï¼å¿
é¡»æé¤è¿ä¸ªæ¡ä»¶
示ä¾:
const path = require('path');
module.exports = {
//...
module: {
rules: [
{
test: /\.css$/,
include: [
// will include any paths relative to the current directory starting with `app/styles`
// e.g. `app/styles.css`, `app/styles/styles.css`, `app/stylesheet.css`
path.resolve(__dirname, 'app/styles'),
// add an extra slash to only include the content of the directory `vendor/styles/`
path.join(__dirname, 'vendor/styles/'),
],
},
],
},
};
object
function(info)
object
å¿
é¡»æä¸ä¸ª loader
å±æ§æ¯å符串ãå®ä½¿ç¨ loader 解æé项ï¼resolveLoaderï¼ï¼ç¸å¯¹äºé
ç½®ä¸ç context
æ¥è§£æã
å¯ä»¥æä¸ä¸ª options
å±æ§ä¸ºå符串æ对象ãå¼å¯ä»¥ä¼ éå° loader ä¸ï¼å°å
¶ç解为 loader é项ã
ç±äºå
¼å®¹æ§åå ï¼ä¹å¯è½æ query
å±æ§ï¼å®æ¯ options
å±æ§çå«åãä½¿ç¨ options
å±æ§æ¿ä»£ã
注æï¼webpack éè¦çæèµæºåææ loader çç¬ç«æ¨¡åæ è¯ï¼å
æ¬é项ã
å®å°è¯å¯¹éé¡¹å¯¹è±¡ä½¿ç¨ JSON.stringify
ãè¿å¨ 99.9% çæ
åµä¸æ¯å¯ä»¥çï¼
ä½æ¯å¦æå°ç¸åç loader åºç¨äºç¸åèµæºçä¸åé项ï¼å¹¶ä¸é项å
·æä¸äºå¸¦å符çå¼ï¼åå¯è½ä¸æ¯å¯ä¸çã
å¦æé项对象ä¸è¢«å符åï¼ä¾å¦å¾ªç¯ JSONï¼ï¼å®ä¹ä¼ä¸æãå æ¤ï¼ä½ å¯ä»¥å¨éé¡¹å¯¹è±¡ä½¿ç¨ ident
å±æ§ï¼
ä½ä¸ºå¯ä¸æ è¯ç¬¦ã
webpack.config.js
module.exports = {
//...
module: {
rules: [
{
loader: 'css-loader',
options: {
modules: true,
},
},
],
},
};
function(info)
UseEntry
ä¹å¯ä»¥æ¯ä¸ä¸ªå½æ°ï¼æ¥å对象åæ°ï¼æ述被å è½½ç模åï¼èä¸å¿
é¡»è¿åä¸ä¸ªåæ°å¯¹è±¡ãè¿å¯ç¨äºæ模åæ´æ¹ loader é项ã
info
对象åæ°æ以ä¸çå段ï¼
compiler
: å½å webpack çç¼è¯å¨ï¼å¯ä»¥æ¯ undefined å¼ï¼issuer
: 模åçè·¯å¾ï¼è¯¥å
ç´ æ£å¨å¯¼å
¥ä¸ä¸ªè¢«å è½½ç模å(resource)realResource
: æ»ä¼æ¯è¢«å 载模åçè·¯å¾resource
: 被å è½½ç模åè·¯å¾ï¼å®å¸¸å¸¸ä¸ realResource
ç¸çï¼åªæå½èµæºå称被 request å符串ä¸ç !=!
è¦çæ¶æä¸ç¸çwebpack.config.js
module.exports = {
//...
module: {
rules: [
{
loader: 'file-loader',
options: {
outputPath: 'svgs',
},
},
(info) => ({
loader: 'svgo-loader',
options: {
plugins: [
{
cleanupIDs: { prefix: basename(info.resource) },
},
],
},
}),
],
},
};
è¿äºé项æè¿°äºå½éå°å¨æä¾èµæ¶ï¼å建ä¸ä¸æçé»è®¤è®¾ç½®ã
ä¾å¦ï¼æªç¥ç(unknown)
å¨æä¾èµï¼require
ã
ä¾å¦ï¼è¡¨è¾¾å¼(expr)
å¨æä¾èµï¼require(expr)
ã
ä¾å¦ï¼å
裹ç(wrapped)
å¨æä¾èµï¼require('./templates/' + expr)
ã
以ä¸æ¯å ¶é»è®¤å¼çå¯ç¨é项
webpack.config.js
module.exports = {
//...
module: {
exprContextCritical: true,
exprContextRecursive: true,
exprContextRegExp: false,
exprContextRequest: '.',
unknownContextCritical: true,
unknownContextRecursive: true,
unknownContextRegExp: false,
unknownContextRequest: '.',
wrappedContextCritical: false,
wrappedContextRecursive: true,
wrappedContextRegExp: /.*/,
strictExportPresence: false,
},
};
å 个ç¨ä¾ï¼
wrappedContextCritical: true
ãrequire(expr)
åºè¯¥å
å«æ´ä¸ªç®å½ï¼exprContextRegExp: /^\.\//
require("./templates/" + expr)
ä¸åºè¯¥å
å«é»è®¤åç®å½ï¼wrappedContextRecursive: false
strictExportPresence
å°ç¼ºå¤±ç导åºæ示æé误èä¸æ¯è¦åwrappedContextRegExp: /\\.\\*/