webpackworker-loader

How to know if a module is being compiled through worker-loader


My aim is to use the new DefinePlugin.runtimeValue() feature to conditionally define 'typeof window' to 'undefined' in the worker and 'object' outside the worker (web target)

currently, my unsatisfying code is:

new webpack.DefinePlugin({
  'typeof window': webpack.DefinePlugin.runtimeValue(function({ module }) {

        const isWorker = module.nameForCondition && /\.worker\./.test(module.nameForCondition());
        return JSON.stringify(isWorker ? 'undefined' : 'object');
    })
}),

and I wondering if I can detect worker-loader through the module object.


Solution

  • sorry for the noise, I found the answer by myself:

    const isWorker = module.parser.state.compilation.compiler.name === 'worker';