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

推荐手册

ContextReplacementPlugin

上下文(Context) 与一个带表达式的 require 语句 相关,例如 require('./locale/' + name + '.json')。遇见此类表达式时,webpack 查找目录 ('./locale/') 下符合正则表达式 (/^.*\.json$/)的文件。由于 name 在编译时(compile time)还是未知的,webpack 会将每个文件都作为模块引入到 bundle 中。

上下文替换插件(ContextReplacementPlugin) 允许你覆盖查找规则,该插件有许多配置方式:

用法

new webpack.ContextReplacementPlugin(
  resourceRegExp: RegExp,
  newContentResource?: string,
  newContentRecursive?: boolean,
  newContentRegExp?: RegExp)

如果资源(或目录)符合 resourceRegExp 正则表达式,插件会替换默认资源为 newContentResource,布尔值 newContentRecursive 表明是否使用递归查找,newContextRegExp 用于筛选新上下文里的资源。如果 newContentResource 为相对路径,会相对于前一匹配资源路径去解析。

这是一个限制模块使用的小例子:

new webpack.ContextReplacementPlugin(  /moment[\/\\]locale$/,  /de|fr|hu/)

限定查找 moment/locale 上下文里符合 /de|fr|hu/ 表达式的文件,因此也只会打包这几种本地化内容(更多详细信息,请查看这个 issue)。

内容回调函数

new webpack.ContextReplacementPlugin(
  resourceRegExp: RegExp,
  newContentCallback: (data) => void)

newContentCallback 函数的第一形参为上下文模块工厂(ContextModuleFactory)data 对象,你需要覆写该对象的 request 属性。

使用这个回调函数,我们可以动态地将请求重定向到一个新的位置:

new webpack.ContextReplacementPlugin(/^\.\/locale$/, (context) => {
  if ( !/\/moment\//.test(context.context) ) return;

  Object.assign(context, {
    regExp: /^\.\/\w+/,
    request: '../../locale' // 相对路径解析
  });
}),

其他选项

newContentResourcenewContentCreateContextMap 参数也可用:

new webpack.ContextReplacementPlugin(
  resourceRegExp: RegExp,
  newContentResource: string,
  newContentCreateContextMap: object // 将运行时请求(runtime-request)映射到编译时请求(compile-time request)
)

这两个参数可以一起使用,来更加有针对性的重定向请求。 newContentCreateContextMap 允许你将运行时的请求,映射为形式为对象的编译请求:

new ContextReplacementPlugin(/selector/, './folder', {
  './request': './request',
  './other-request': './new-request'
})
网站导航
标签地图
学习路径
视频教程
开发软件
旗下子站
php中文网
phpstudy
技术文章
文档工具
关于我们
企业合作
人才招聘
联系我们
讲师招募
QQ交流群
QQ官方交流群
微信公众号
微信公众号