如何知道模块是否正在通过 worker-loader 编译 [英] How to know if a module is being compiled through worker-loader

查看:30
本文介绍了如何知道模块是否正在通过 worker-loader 编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用新的 DefinePlugin.runtimeValue() 功能有条件地将 'typeof window' 定义为 'undefined' 在工作器中和在工作器外部的'object'(网络目标)

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.

推荐答案

抱歉打扰了,我自己找到了答案:

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

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

这篇关于如何知道模块是否正在通过 worker-loader 编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆