just like this, use DefinePlugin to define global var ECZN
for my app with webpack runtimeValue
.
and... the WebpackModuleInfo is actually from the file where i refer ECZN
, but i just get the module file path, not the Entry
...
so, can i get the entry info for the runtimeValue's context in order to set different var value
for my multi html page app ?
text code:
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');
}),
})
i had found some solutions :
- make app to have multi webpack config to create multi instance like this: https://github.com/webpack/webpack/issues/5546 (performance warning)
- just use the
moduleResource
as reference to definevar Eczn
(it's not precisely)
I can think of three solutions to get the entry point that contains the import chain to
module
. I'll list all of them since I'm not sure which one is best for your case. Webpack doesn't document these objects, so it's hard to say which is the correct method.Note that I found this question when researching in the context of web workers and parser object.
module.parser.state.compilation.entries
module.parser.state.compilation.entries
is an array. I'm guessing because a module can have multiple entry points. I'm not sure how to determine the current entry point with this method since I don't have multiple entry points to test. Test it out in a debugger.For workers:
For files along webpack config entrypoints:
Parse
compilation.name
It would look something like this:
For workers:
You can use string manipulation to grab the entry point.
For files along webpack config entrypoints:
loop through
module.issuer
untilnull
For workers:
For files along webpack config entrypoints:
If there is only one entry point, the
issuer
module === module.parser.state.compilation.entries[0]
.