overriding a method on a global module in webpack

2019-03-04 01:00发布

问题:

I have something like this in my webpack config:

plugins:[
  new webpack.ProvidePlugin({ 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); 

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.

回答1:

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

new webpack.ProvidePlugin({
        '$': 'jquery',
        '$.each': '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.

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