Webpack.DefinePlugin.runtimeValue是否可以获取模块的条目?

问题描述

enter image description here

就像这样,使用DefinePlugin通过ECZN为我的应用程序定义全局变量webpack runtimeValue

然后... WebpackModuleInfo实际上来自我引用的文件ECZN,但是我只是获取模块文件路径,而不是Entry ...

所以,我可以获取runtimeValue上下文的条目信息,以便为我的多HTML页面应用设置不同的var value吗?


文本代码:

      new Webpack.DefinePlugin({
        'process.env.NODE_ENV': JSON.stringify(WEBPACK_NODE_ENV),development: JSON.stringify(WEBPACK_NODE_ENV_DEV),production: JSON.stringify(WEBPACK_NODE_ENV_PROD),ECZN: Webpack.DefinePlugin.runtimeValue((ctx) => {
          console.log('WebpackModuleInfo :: ',ctx.module);
          // @ts-ignore webpack.d.ts doesn't define resource,but it exists on ctx.module acutally
          const moduleResource: unknown = ctx.module.resource;
          if (typeof moduleResource === 'string' && moduleResource.length) {
            const absPathFromCwd = path.relative(cwdResolve('.'),moduleResource);

            return JSON.stringify(absPathFromCwd);
          }
          // throw error if not found
          throw new Error('dynamic');
        }),})

我找到了一些解决方案:

  1. 使应用具有多个webpack配置,以创建多个实例,例如:https://github.com/webpack/webpack/issues/5546(性能警告)
  2. 仅使用moduleResource作为定义var Eczn的引用(并非完全如此)

解决方法

我可以想到三种解决方案来获取包含 module 的导入链的入口点。我将列出所有这些,因为我不确定哪一个最适合您的情况。 Webpack 没有记录这些对象,所以很难说哪种方法是正确的。

请注意,我在研究 web workersparser object 的上下文时发现了这个问题。


module.parser.state.compilation.entries

module.parser.state.compilation.entries 是一个数组。我猜是因为一个模块可以有多个入口点。我不确定如何使用此方法确定当前入口点,因为我没有要测试的多个入口点。在调试器中进行测试。

对于工人:

module.parser.state.compilation.entries[0].resource === '/.../moduleEntryPoint.js'

对于沿 webpack 配置入口点的文件:

module.parser.state.compilation.entries[0].name === 'main'

# Can have array of `dependencies` if your webpack config entrypoint is an array
module.parser.state.compilation.entries[0].dependencies[*].module.resource === '/.../moduleEntryPoint.js'

module.parser.state.compilation.entries[0].dependencies[*].request === '/.../moduleEntryPoint.js'

解析compilation.name

它看起来像这样:

对于工人:

module.parser.state.compilation.name ===
'worker-loader /.../node_modules/babel-loader/lib/index.js??ref--5!/.../node_modules/source-map-loader/index.js!/.../moduleEntryPoint.js'

您可以使用字符串操作来抓取入口点。

对于沿 webpack 配置入口点的文件:

module.parser.state.compilation.name === 'client'

循环 module.issuer 直到 null

对于工人:

while (module.issuer) {
  module = module.issuer
}
module.resource === '/.../moduleEntryPoint.js'

对于沿 webpack 配置入口点的文件:

while (module.issuer) {
  module = module.issuer
}

module.parser.state.compilation.entries[0].name === 'main'

# Can have array of `dependencies` if your webpack config entrypoint is an array
module.parser.state.compilation.entries[0].dependencies[*].module.resource === '/.../moduleEntryPoint.js'

module.parser.state.compilation.entries[0].dependencies[*].request === '/.../moduleEntryPoint.js'

如果只有一个入口点,则 issuer module === module.parser.state.compilation.entries[0]

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...