è¿äºé项è½è®¾ç½®æ¨¡åå¦ä½è¢«è§£æãwebpack æä¾åççé»è®¤å¼ï¼ä½æ¯è¿æ¯å¯è½ä¼ä¿®æ¹ä¸äºè§£æçç»èãå ³äº resolver å ·ä½å¦ä½å·¥ä½çæ´å¤è§£é说æï¼è¯·æ¥ç模å解æã
object
é
置模åå¦ä½è§£æãä¾å¦ï¼å½å¨ ES2015 ä¸è°ç¨ import 'lodash'
ï¼resolve
é项è½å¤å¯¹ webpack æ¥æ¾ 'lodash'
çæ¹å¼å»åä¿®æ¹ï¼æ¥ç模å
ï¼ã
webpack.config.js
module.exports = {
//...
resolve: {
// configuration options
},
};
object
å建 import
æ require
çå«åï¼æ¥ç¡®ä¿æ¨¡åå¼å
¥åå¾æ´ç®åãä¾å¦ï¼ä¸äºä½äº src/
æ件夹ä¸ç常ç¨æ¨¡åï¼
webpack.config.js
const path = require('path');
module.exports = {
//...
resolve: {
alias: {
Utilities: path.resolve(__dirname, 'src/utilities/'),
Templates: path.resolve(__dirname, 'src/templates/'),
},
},
};
ç°å¨ï¼æ¿æ¢âå¨å¯¼å ¥æ¶ä½¿ç¨ç¸å¯¹è·¯å¾âè¿ç§æ¹å¼ï¼å°±åè¿æ ·ï¼
import Utility from '../../utilities/utility';
ä½ å¯ä»¥è¿æ ·ä½¿ç¨å«åï¼
import Utility from 'Utilities/utility';
ä¹å¯ä»¥å¨ç»å®å¯¹è±¡çé®åçæ«å°¾æ·»å $
ï¼ä»¥è¡¨ç¤ºç²¾åå¹é
ï¼
webpack.config.js
const path = require('path');
module.exports = {
//...
resolve: {
alias: {
xyz$: path.resolve(__dirname, 'path/to/file.js'),
},
},
};
è¿å°äº§ç以ä¸ç»æï¼
import Test1 from 'xyz'; // 精确å¹é
ï¼æ以 path/to/file.js 被解æå导å
¥
import Test2 from 'xyz/file.js'; // é精确å¹é
ï¼è§¦åæ®é解æ
ä¸é¢çè¡¨æ ¼å±ç¤ºäºä¸äºå ¶ä»æ åµï¼
alias: | import 'xyz' | import 'xyz/file.js' |
---|---|---|
{} | /abc/node_modules/xyz/index.js | /abc/node_modules/xyz/file.js |
{ xyz: '/abc/path/to/file.js' } | /abc/path/to/file.js | error |
{ xyz$: '/abc/path/to/file.js' } | /abc/path/to/file.js | /abc/node_modules/xyz/file.js |
{ xyz: './dir/file.js' } | /abc/dir/file.js | error |
{ xyz$: './dir/file.js' } | /abc/dir/file.js | /abc/node_modules/xyz/file.js |
{ xyz: '/some/dir' } | /some/dir/index.js | /some/dir/file.js |
{ xyz$: '/some/dir' } | /some/dir/index.js | /abc/node_modules/xyz/file.js |
{ xyz: './dir' } | /abc/dir/index.js | /abc/dir/file.js |
{ xyz: 'modu' } | /abc/node_modules/modu/index.js | /abc/node_modules/modu/file.js |
{ xyz$: 'modu' } | /abc/node_modules/modu/index.js | /abc/node_modules/xyz/file.js |
{ xyz: 'modu/some/file.js' } | /abc/node_modules/modu/some/file.js | error |
{ xyz: 'modu/dir' } | /abc/node_modules/modu/dir/index.js | /abc/node_modules/modu/dir/file.js |
{ xyz$: 'modu/dir' } | /abc/node_modules/modu/dir/index.js | /abc/node_modules/xyz/file.js |
å¦æå¨ package.json
ä¸å®ä¹ï¼index.js
å¯è½ä¼è¢«è§£æ为å¦ä¸ä¸ªæ件ã
/abc/node_modules
ä¹å¯è½å¨ /node_modules
ä¸è§£æã
module.exports = {
//...
resolve: {
alias: {
_: [
path.resolve(__dirname, 'src/utilities/'),
path.resolve(__dirname, 'src/templates/'),
],
},
},
};
å° resolve.alias
设置为 false
å°åç¥ webpack 忽ç¥æ¨¡åã
module.exports = {
//...
resolve: {
alias: {
'ignored-module': false,
'./ignored-module': false,
},
},
};
[string]: ['browser']
æå®ä¸ä¸ªå段ï¼ä¾å¦ browser
ï¼æ ¹æ®
æ¤è§èè¿è¡è§£æã
webpack.config.js
module.exports = {
//...
resolve: {
aliasFields: ['browser'],
},
};
boolean
å¦æå¯ç¨äºä¸å®å
¨ç¼åï¼è¯·å¨ç¼åé®(cache key)ä¸å¼å
¥ request.context
ãè¿ä¸ªé项被 enhanced-resolve
模åèèå¨å
ãä» webpack 3.1.0 å¼å§ï¼å¨é
ç½®äº resolve æ resolveLoader æ件æ¶ï¼è§£æç¼å(resolve caching)ä¸çä¸ä¸æ(context)ä¼è¢«å¿½ç¥ãè¿è§£å³äºæ§è½è¡°éçé®é¢ã
string[]
exports
é
置项 ï¼å®ä¹ä¸ä¸ªåºçå
¥å£ï¼ç conditionNameã
webpack.config.js
module.exports = {
//...
resolve: {
conditionNames: ['require', 'node'],
},
};
Webpack å°ä¼å¹é
åå¨ resolve.conditionNames
æ°ç»ä¸ç export conditionsã
exports
å段ä¸çé®é¡ºåºå¾éè¦ãå¨æ¡ä»¶å¹é
æé´ï¼è¾æ©çå
¥å£å
·ææ´é«çä¼å
级并ä¼å
äºåé¢çå
¥å£ã
ä¾å¦ï¼
package.json
{
"name": "foo",
"exports": {
".": {
"import": "./index-import.js",
"require": "./index-require.js",
"node": "./index-node.js"
},
"./bar": {
"node": "./bar-node.js",
"require": "./bar-require.js"
},
"./baz": {
"import": "./baz-import.js",
"node": "./baz-node.js"
}
}
}
webpack.config.js
module.exports = {
//...
resolve: {
conditionNames: ['require', 'node'],
},
};
importing
'foo'
å°è§£æ为 'foo/index-require.js'
'foo/bar'
å°è§£æ为 'foo/bar-node.js'
ï¼å 为å¨æ¡ä»¶å¯¼åºå¯¹è±¡ä¸ï¼ "node"
åæ®µå¨ "require"
ä¹åã'foo/baz'
å°è§£æ为 'foo/baz-node.js'
[string] = ['package.json']
ç¨äºæè¿°ç JSON æ件ã
webpack.config.js
module.exports = {
//...
resolve: {
descriptionFiles: ['package.json'],
},
};
boolean = false
å¦ææ¯ true
ï¼å°ä¸å
许æ æ©å±åæ件ãé»è®¤å¦æ ./foo
æ .js
æ©å±ï¼require('./foo')
å¯ä»¥æ£å¸¸è¿è¡ãä½å¦æå¯ç¨æ¤é项ï¼åªæ require('./foo.js')
è½å¤æ£å¸¸å·¥ä½ã
webpack.config.js
module.exports = {
//...
resolve: {
enforceExtension: false,
},
};
object
ä¸ä¸ªå°æå±åä¸æå±åå«åæ å°ç对象ã
webpack.config.js
module.exports = {
//...
resolve: {
extensionAlias: {
'.js': ['.ts', '.js'],
'.mjs': ['.mts', '.mjs'],
},
},
};
[string] = ['.js', '.json', '.wasm']
å°è¯æ顺åºè§£æè¿äºåç¼åãå¦ææå¤ä¸ªæ件æç¸åçååï¼ä½åç¼åä¸åï¼webpack ä¼è§£æåå¨æ°ç»é¦ä½çåç¼çæ件 并跳è¿å ¶ä½çåç¼ã
webpack.config.js
module.exports = {
//...
resolve: {
extensions: ['.js', '.json', '.wasm'],
},
};
è½å¤ä½¿ç¨æ·å¨å¼å ¥æ¨¡åæ¶ä¸å¸¦æ©å±ï¼
import File from '../path/to/file';
请注æï¼ä»¥ä¸è¿æ ·ä½¿ç¨ resolve.extensions
ä¼ è¦çé»è®¤æ°ç»ï¼è¿å°±æå³ç webpack å°ä¸åå°è¯ä½¿ç¨é»è®¤æ©å±æ¥è§£æ模åãç¶èä½ å¯ä»¥ä½¿ç¨ '...'
访é®é»è®¤æå±åï¼
module.exports = {
//...
resolve: {
extensions: ['.ts', '...'],
},
};
object
å½æ£å¸¸è§£æ失败æ¶ï¼éå®å模å请æ±ã
webpack.config.js
module.exports = {
//...
resolve: {
fallback: {
abc: false, // do not include a polyfill for abc
xyz: path.resolve(__dirname, 'path/to/file.js'), // include a polyfill for xyz
},
},
};
webpack 5 ä¸åèªå¨ polyfill Node.js çæ ¸å¿æ¨¡åï¼è¿æå³çå¦æä½ å¨æµè§å¨æ类似çç¯å¢ä¸è¿è¡ç代ç ä¸ä½¿ç¨å®ä»¬ï¼ä½ å¿ é¡»ä» NPM ä¸å®è£ å ¼å®¹ç模åï¼å¹¶èªå·±å å«å®ä»¬ã以ä¸æ¯ webpack å¨ webpack 5 ä¹å使ç¨è¿ç polyfill å å表ï¼
module.exports = {
//...
resolve: {
fallback: {
assert: require.resolve('assert'),
buffer: require.resolve('buffer'),
console: require.resolve('console-browserify'),
constants: require.resolve('constants-browserify'),
crypto: require.resolve('crypto-browserify'),
domain: require.resolve('domain-browser'),
events: require.resolve('events'),
http: require.resolve('stream-http'),
https: require.resolve('https-browserify'),
os: require.resolve('os-browserify/browser'),
path: require.resolve('path-browserify'),
punycode: require.resolve('punycode'),
process: require.resolve('process/browser'),
querystring: require.resolve('querystring-es3'),
stream: require.resolve('stream-browserify'),
string_decoder: require.resolve('string_decoder'),
sys: require.resolve('util'),
timers: require.resolve('timers-browserify'),
tty: require.resolve('tty-browserify'),
url: require.resolve('url'),
util: require.resolve('util'),
vm: require.resolve('vm-browserify'),
zlib: require.resolve('browserify-zlib'),
},
},
};
[string]
å½ä» npm å
ä¸å¯¼å
¥æ¨¡åæ¶ï¼ä¾å¦ï¼import * as D3 from 'd3'
ï¼ï¼æ¤é项å°å³å®å¨ package.json
ä¸ä½¿ç¨åªä¸ªå段导å
¥æ¨¡åãæ ¹æ® webpack é
ç½®ä¸æå®ç target
ä¸åï¼é»è®¤å¼ä¹ä¼ææä¸åã
å½ target
å±æ§è®¾ç½®ä¸º webworker
, web
æè
没ææå®ï¼
webpack.config.js
module.exports = {
//...
resolve: {
mainFields: ['browser', 'module', 'main'],
},
};
对äºå
¶ä»ä»»æç targetï¼å
æ¬ node
ï¼ï¼é»è®¤å¼ä¸ºï¼
webpack.config.js
module.exports = {
//...
resolve: {
mainFields: ['module', 'main'],
},
};
ä¾å¦ï¼èèä»»æä¸ä¸ªå为 upstream
çç±»åº package.json
å
å«ä»¥ä¸å段ï¼
{
"browser": "build/upstream.js",
"module": "index"
}
å¨æ们 import * as Upstream from 'upstream'
æ¶ï¼è¿å®é
ä¸ä¼ä» browser
å±æ§è§£ææ件ãå¨è¿é browser
å±æ§æ¯æä¼å
éæ©çï¼å 为å®æ¯ mainFields
ç第ä¸é¡¹ãåæ¶ï¼ç± webpack æå
ç Node.js åºç¨ç¨åºé¦å
ä¼å°è¯ä» module
å段ä¸è§£ææ件ã
[string] = ['index']
解æç®å½æ¶è¦ä½¿ç¨çæ件åã
webpack.config.js
module.exports = {
//...
resolve: {
mainFiles: ['index'],
},
};
[string] = ['exports']
å¨ package.json ä¸ç¨äºè§£æ模å请æ±çå段ã欲äºè§£æ´å¤ä¿¡æ¯ï¼è¯·æ¥é package-exports guideline ææ¡£ã
webpack.config.js
module.exports = {
//...
resolve: {
exportsFields: ['exports', 'myCompanyExports'],
},
};
[string] = ['node_modules']
åè¯ webpack 解æ模åæ¶åºè¯¥æç´¢çç®å½ã
ç»å¯¹è·¯å¾åç¸å¯¹è·¯å¾é½è½ä½¿ç¨ï¼ä½æ¯è¦ç¥éå®ä»¬ä¹é´æä¸ç¹å·®å¼ã
éè¿æ¥çå½åç®å½ä»¥åç¥å
è·¯å¾ï¼å³ ./node_modules
, ../node_modules
ççï¼ï¼
ç¸å¯¹è·¯å¾å°ç±»ä¼¼äº Node æ¥æ¾ 'node_modules' çæ¹å¼è¿è¡æ¥æ¾ã
使ç¨ç»å¯¹è·¯å¾ï¼å°åªå¨ç»å®ç®å½ä¸æç´¢ã
webpack.config.js
module.exports = {
//...
resolve: {
modules: ['node_modules'],
},
};
å¦æä½ æ³è¦æ·»å ä¸ä¸ªç®å½å°æ¨¡åæç´¢ç®å½ï¼æ¤ç®å½ä¼å
äº node_modules/
æç´¢ï¼
webpack.config.js
const path = require('path');
module.exports = {
//...
resolve: {
modules: [path.resolve(__dirname, 'src'), 'node_modules'],
},
};
RegExp
[RegExp]
boolean: true
å¯ç¨ï¼ä¼ä¸»å¨ç¼å模åï¼ä½å¹¶ ä¸å®å
¨ãä¼ é true
å°ç¼åä¸åã
webpack.config.js
module.exports = {
//...
resolve: {
unsafeCache: true,
},
};
æ£å表达å¼ï¼ææ£å表达å¼æ°ç»ï¼å¯ä»¥ç¨äºå¹é æ件路å¾æåªç¼åæäºæ¨¡åã ä¾å¦ï¼åªç¼å utilities 模åï¼
webpack.config.js
module.exports = {
//...
resolve: {
unsafeCache: /src\/utilities/,
},
};
boolean
对 resolver 使ç¨åæ¥æ件系ç»è°ç¨ã
webpack.config.js
module.exports = {
//...
resolve: {
useSyncFileSystemCalls: true,
},
};
åºè¯¥ä½¿ç¨çé¢å¤ç解ææ件å表ã
å®å
许æ件ï¼å¦ DirectoryNamedWebpackPlugin
ã
webpack.config.js
module.exports = {
//...
resolve: {
plugins: [new DirectoryNamedWebpackPlugin()],
},
};
boolean
å½å¯ç¨æ¤é项æ¶ï¼webpack æ´å¾åäºå°æ¨¡å请æ±è§£æ为ç¸å¯¹è¯·æ±ï¼èä¸ä½¿ç¨æ¥èª node_modules
ç®å½ä¸ç模åã
webpack.config.js
module.exports = {
//...
resolve: {
preferRelative: true,
},
};
src/index.js
// let's say `src/logo.svg` exists
import logo1 from 'logo.svg'; // this is viable when `preferRelative` enabled
import logo2 from './logo.svg'; // otherwise you can only use relative path to resolve logo.svg
// `preferRelative` is enabled by default for `new URL()` case
const b = new URL('module/path', import.meta.url);
const a = new URL('./module/path', import.meta.url);
boolean
解ææ¶ï¼é¦éçç»å¯¹è·¯å¾ä¸º resolve.roots
ã
webpack.config.js
module.exports = {
//...
resolve: {
preferAbsolute: true,
},
};
boolean = true
æ¯å¦å°ç¬¦å·é¾æ¥(symlink)解æå°å®ä»¬ç符å·é¾æ¥ä½ç½®(symlink location)ã
å¯ç¨æ¶ï¼ç¬¦å·é¾æ¥(symlink)çèµæºï¼å°è§£æ为å
¶ çå® è·¯å¾ï¼èä¸æ¯å
¶ç¬¦å·é¾æ¥(symlink)çä½ç½®ã注æï¼å½ä½¿ç¨å建符å·é¾æ¥å
çå·¥å
·ï¼å¦ npm link
ï¼æ¶ï¼è¿ç§æ¹å¼å¯è½ä¼å¯¼è´æ¨¡å解æ失败ã
webpack.config.js
module.exports = {
//...
resolve: {
symlinks: true,
},
};
function(module) => boolean
å³å®è¯·æ±æ¯å¦åºè¯¥è¢«ç¼åçå½æ°ãå½æ°ä¼ å
¥ä¸ä¸ªå¸¦æ path
å request
å±æ§ç对象ã
å¿
é¡»è¿åä¸ä¸ª boolean å¼ã
webpack.config.js
module.exports = {
//...
resolve: {
cachePredicate: (module) => {
// additional logic
return true;
},
},
};
[string, RegExp]
解æéå¶å表ï¼ç¨äºéå¶å¯ä»¥è§£æ请æ±çè·¯å¾ã
webpack.config.js
module.exports = {
//...
resolve: {
restrictions: [/\.(sass|scss|css)$/],
},
};
[string]
A list of directories where requests of server-relative URLs (starting with '/') are resolved, defaults to context
configuration option. On non-Windows systems these requests are resolved as an absolute path first.
webpack.config.js
const fixtures = path.resolve(__dirname, 'fixtures');
module.exports = {
//...
resolve: {
roots: [__dirname, fixtures],
},
};
[string]
Fields from package.json
which are used to provide the internal requests of a package (requests starting with #
are considered internal).
webpack.config.js
module.exports = {
//...
resolve: {
importsFields: ['browser', 'module', 'main'],
},
};
éè¿ module 请æ±ç±»åæ¥é ç½® resolve é项ã
Type: [type: string]: ResolveOptions
Example:
module.exports = {
// ...
resolve: {
byDependency: {
// ...
esm: {
mainFields: ['browser', 'module'],
},
commonjs: {
aliasFields: ['browser'],
},
url: {
preferRelative: true,
},
},
},
};
object { modules [string] = ['node_modules'], extensions [string] = ['.js', '.json'], mainFields [string] = ['loader', 'main']}
è¿ç»é项ä¸ä¸é¢ç resolve
对象çå±æ§éåç¸åï¼
ä½ä»
ç¨äºè§£æ webpack ç loader å
ã
webpack.config.js
module.exports = {
//...
resolveLoader: {
modules: ['node_modules'],
extensions: ['.js', '.json'],
mainFields: ['loader', 'main'],
},
};