非常教程

Webpack参考手册

组态 | Configuration

解析 | Resolve

这些选项能设置模块如何被解析。webpack 提供合理的默认值,但是还是可能会修改一些解析的细节。关于 resolver 具体如何工作的更多解释说明,请查看模块解析。

resolve

object

配置模块如何解析。例如,当在 ES2015 中调用 import "lodash"resolve 选项能够对 webpack 查找 "lodash" 的方式去做修改(查看模块)。

resolve.alias

object

创建 importrequire 的别名,来确保模块引入变得更简单。例如,一些位于 src/ 文件夹下的常用模块:

alias: {
  Utilities: path.resolve(__dirname, 'src/utilities/'),
  Templates: path.resolve(__dirname, 'src/templates/')
}

现在,在导入时不使用相对路径,而是像这样:

import Utility from '../../utilities/utility';

你可以使用别名:

import Utility from 'Utilities/utility';

也可以在给定对象的键后的末尾添加 $,以表示精准匹配:

alias: {
  xyz$: path.resolve(__dirname, 'path/to/file.js')
}

这将产生这些结果:

import Test1 from 'xyz'; // Exact match, so path/to/file.js is resolved and imported
import Test2 from 'xyz/file.js'; // Not an exact match, normal resolution takes place

下表解释了其他情况:

alias:

import "xyz"

import "xyz/file.js"

{}

/abc/node_modules/xyz/index.js

/abc/node_modules/xyz/file.js

{ xyz: "/abs/path/to/file.js" }

/abs/path/to/file.js

error

{ xyz$: "/abs/path/to/file.js" }

/abs/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/dir/file.js

{ xyz: "xyz/dir" }

/abc/node_modules/xyz/dir/index.js

/abc/node_modules/xyz/dir/file.js

{ xyz$: "xyz/dir" }

/abc/node_modules/xyz/dir/index.js

/abc/node_modules/xyz/file.js

如果在 package.json 中定义,index.js 可能会被解析为另一个文件。

/abc/node_modules也可以解决/node_modules

resolve.aliasFields

string

指定一个字段,例如 browser,根据此规范进行解析。默认:

aliasFields: ["browser"]

resolve.cacheWithContext

boolean (since webpack 3.1.0)

如果启用了不安全缓存,请在缓存键(cache key)中引入 request.context。这个选项被 enhanced-resolve 模块考虑在内。从 webpack 3.1.0 开始,在配置了 resolve 或 resolveLoader 插件时,解析缓存(resolve caching)中的上下文(context)会被忽略。这解决了性能衰退的问题。

resolve.descriptionFiles

array

用于描述的 JSON 文件。默认:

descriptionFiles: ["package.json"]

resolve.enforceExtension

boolean

如果是 true,将不允许无扩展名(extension-less)文件。默认如果 ./foo.js 扩展,require('./foo') 可以正常运行。但如果启用此选项,只有 require('./foo.js') 能够正常工作。默认:

enforceExtension: false

resolve.enforceModuleExtension

boolean

对模块是否需要使用的扩展(例如 loader)。默认:

enforceModuleExtension: false

resolve.extensions

array

自动解析某些扩展。这默认为:

extensions: [".js", ".json"]

能够使用户在引入模块时不带扩展:

import File from '../path/to/file'

使用这将覆盖默认数组,这就意味着 webpack 将不再尝试使用默认扩展来解析模块。对于使用其扩展导入的模块,例如,import SomeFile from "./somefile.ext",要想正确的解析,一个包含“*”的字符串必须包含在数组中。

resolve.mainFields

array

当从 npm 包中导入模块时(例如,import * as D3 from "d3"),此选项将决定在 package.json 中使用哪个字段导入模块。根据 webpack 配置中指定的 target 不同,默认值也会有所不同。

target 属性设置为 webworker, web 或者没有指定,默认值为:

mainFields: ["browser", "module", "main"]

对于其他任意的 target(包括 node),默认值为:

mainFields: ["module", "main"]

例如,package.json的 D3包含以下字段:

{
  ...
  main: 'build/d3.Node.js',
  browser: 'build/d3.js',
  module: 'index',
  ...
}

这意味着当我们 import * as D3 from "d3",实际从 browser 属性解析文件。在这里 browser 属性是最优先选择的,因为它是 mainFields 的第一项。同时,由 webpack 打包的 Node.js 应用程序默认会从 module 字段中解析文件。

resolve.mainFiles

array

解析目录时使用的文件名。默认:

mainFiles: ["index"]

resolve.modules

array

告诉 webpack 解析模块时应该搜索的目录。

绝对路径和相对路径都能使用,但是要知道它们之间有一点差异。

通过查看当前目录以及祖先路径(即 ./node_modules, ../node_modules 等等),相对路径将类似于 Node 查找 'node_modules' 的方式进行查找。

使用绝对路径,它只会在给定目录中进行搜索。

resolve.modules defaults to:

modules: ["node_modules"]

如果你想要添加一个目录到模块搜索目录,此目录优先于 node_modules/ 搜索:

modules: [path.resolve(__dirname, "src"), "node_modules"]

resolve.unsafeCache

regex array boolean

启用,会主动缓存模块,但并不安全。传递 true 将缓存一切。默认:

unsafeCache: true

正则表达式,或正则表达式数组,可以用于匹配文件路径或只缓存某些模块。例如,只缓存 utilities 模块:

unsafeCache: /src\/utilities/

在极少数情况下,对缓存路径的更改可能会导致失败。

resolve.plugins

应该应用的附加解析插件列表。它允许插件如 DirectoryNamedWebpackPlugin

plugins: [
  new DirectoryNamedWebpackPlugin()
]

resolve.symlinks

boolean

是否解析符号链接到它们的符号链接位置。默认:

symlinks: true

resolve.cachePredicate

function

决定请求是否应该被缓存的函数。函数传入一个带有 pathrequest 属性的对象。默认:

cachePredicate: function() { return true }

resolveLoader

object

这组选项与上面的 resolve 对象的属性集合相同,但仅用于解析 webpack 的 loader 包。默认:

{
  modules: [ 'node_modules' ],
  extensions: [ '.js', '.json' ],
  mainFields: [ 'loader', 'main' ]
}

请注意,您可以在此处使用别名以及从解析中熟悉的其他功能。例如{ txt: 'raw-loader' }可以txt!templates/demo.txt使用垫片raw-loader

resolveLoader.moduleExtensions

array

解析 loader 时,用到扩展名(extensions)/后缀(suffixes)。从 webpack 2 开始,我们强烈建议使用全名,例如 example-loader,以尽可能清晰。然而,如果你确实想省略 -loader,也就是说只使用 example,则可以使用此选项来实现:

moduleExtensions: [ '-loader' ]
Webpack

webpack 是一个模块打包器。webpack 处理带有依赖关系的模块,生成一系列表示这些模块的静态资源。

主页 https://webpack.js.org/
源码 https://github.com/webpack/webpack
发布版本 3.8.1

Webpack目录

1.指南 | Guides
2.接口 | API
3.概念 | Concepts
4.组态 | Configuration
5.装载 | Loaders