从webpack bundle中排除模块 [英] Exclude a module from webpack bundle

查看:312
本文介绍了从webpack bundle中排除模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要像RequireJS 相同的功能空: http ://requirejs.org/docs/optimization.html#empty

I'd like the same functionality like RequireJS empty: http://requirejs.org/docs/optimization.html#empty

我的用例是我包含 jquery-migrate 正在开发中,但我想在生产时将其排除。

My use-case is that I include jquery-migrate in development, but I'd like to exclude this when built for production.

使用 IgnorePlugin 只是让它不包括在内,当在代码中需要时,它会抛出一个错误( Uncaught Error:找不到模块 jquery-migrate)。

Using IgnorePlugin just makes it not included, and when requireing it in the code, it then throws an error (Uncaught Error: Cannot find module "jquery-migrate").

我想要发生的是它只返回undefined,或类似的东西(如在RequireJS中为空:。我想不要触摸代码中的导入,只是将其配置为返回 undefined

What I'd like to happen is for it to just return undefined, or something of the like (like empty: in RequireJS). Id like to not touch the import in the code, just configuring it to return undefined.

编辑:使用<如果我将替换指向空文件,code> NormalModuleReplacementPlugin 有效。但是为此保留一个空文件似乎是不必要的。

Using NormalModuleReplacementPlugin works, if I point the replacement to an empty file. But keeping an empty file around just for this seems unnecessary.

推荐答案

我使用null-loader来消隐模块。
noop-loader可用于配置中较不尴尬的if-else。

I use the null-loader for blanking out modules. The noop-loader can be used for a less awkward if-else in the config.

尝试:

rules: [{
    test: /jquery-migrate/,
    use: IS_DEV ? 'null-loader' : 'noop-loader'
}]

这篇关于从webpack bundle中排除模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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