购物车
登陆 / 注册
微信扫码登陆

推荐手册

解析(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'; // 精确匹配,所以 path/to/file.js 被解析和导入
import Test2 from 'xyz/file.js'; // 非精确匹配,触发普通解析

下面的表格展示了一些其他情况:

别名: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.jserror
{ xyz$: "/abs/path/to/file.js" }/abs/path/to/file.js/abc/node_modules/xyz/file.js
{ xyz: "./dir/file.js" }/abc/dir/file.jserror
{ 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.jserror
{ 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(从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'

W>使用此选项,会覆盖默认数组,这就意味着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"]

例如,D3package.json含有这些字段:

{
  ...
  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/

W> 修改缓存路径可能在极少数情况下导致失败。

resolve.plugins

应该使用的额外的解析插件列表。它允许插件,如DirectoryNamedWebpackPlugin

plugins: [
  new DirectoryNamedWebpackPlugin()
  ]

boolean

是否将符号链接(symlink)解析到它们的符号链接位置(symlink location)。默认:

启用时,符号链接(symlink)的资源,将解析为其真实路径,而不是其符号链接(symlink)位置。注意,当使用符号链接package包工具时(如npm link),可能会导致模块解析失败。

resolve.symlinks 默认值为:

symlinks: true

resolve.cachePredicate

function

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

cachePredicate: function() { return true }

resolveLoader

object

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

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

T>注意,这里你可以使用别名,并且其他特性类似于resolve对象。例如,{ txt: 'raw-loader' }会使用raw-loader去shim(填充) txt!templates/demo.txt

resolveLoader.moduleExtensions

array

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

moduleExtensions: [ '-loader' ]
网站导航
标签地图
学习路径
视频教程
开发软件
旗下子站
php中文网
phpstudy
技术文章
文档工具
关于我们
企业合作
人才招聘
联系我们
讲师招募
QQ交流群
QQ官方交流群
微信公众号
微信公众号