在 webpack 中覆盖全局模块上的方法 [英] overriding a method on a global module in webpack

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

问题描述

我的 webpack 配置中有这样的东西:

I have something like this in my webpack config:

plugins:[
  new webpack.ProvidePlugin({ THREE: 'three' }),
  ...

这使得 THREE 在全球范围内可用(或至少在任何使用它的地方?)

which makes THREE available globally (or at least wherever its used?)

我想覆盖这个库中的一个方法,例如在入口点:

I would like to override a method from this library, for example in the entry point:

THREE.Something = mySomething;

我没有成功,这是怎么做到的?

I'm not succeding, how is this done?

或者我尝试过类似的东西.

Alternatively i tried something like.

require(expose?THREE!./myCustomThree.js); 

但这也不起作用,我只在我进行 require 调用的范围内得到了它.虽然我能够覆盖该方法,但不能使其成为全局.

But that didnt work either, i only got it in the scope where i made the require call. I was able to override the method though, but can't make it global.

推荐答案

ProvidePlugin 只是全局地替换由定义的模块实例提供的字符串.

ProvidePlugin just replaces globally the string provided with the instance of the module defined.

new webpack.ProvidePlugin({
        '$': 'jquery',
        '$.each': 'moment'
    })

上面的插件现在用 jquery 的实例替换了你代码中的所有 $ 实例.在第二种情况下,它将 $.moment 替换为 moment 的实例.

The above plugin now replaces all instances of $ in your code with the instance of jquery. And in the second case, it replaces $.moment with the instance of moment.

您必须明白,ProvidePlugin 只是将模块重命名为您提供的字符串,我想这是一种覆盖.

You have to understand that ProvidePlugin simply renames the module to the string you provide and I guess that is kind of an override.

这篇关于在 webpack 中覆盖全局模块上的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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