javascript - WebPack replace vendor require call with global variable -


having trouble webpack. have vendor library (saying childvendor), implements requirejs , commonjs compatibility, so, need require in webpack project var lib = require('./childvendor');. childvendor library has dependency (saying supervendor), , both of them requirejs- , commonjs-adapted, so, heading of childvendor.js looks like:

(function(root, factory) {   if (typeof define === 'function' && define.amd) {     define(["supervendor"], factory);   } else if (typeof exports === 'object') {     module.exports = factory(require('supervendor'));   } else {     root.shepherd = factory(root.supervendor);   } }(this, function(supervendor) { /*...*/ })); 

the main problem need include supervendor library globally on html-file manually (so, initialized window.supervendor), because should used other third-party libraries.

to solve this, have tried webpack.provideplugin, like

plugins: [     new webpack.provideplugin({         'supervendor': 'supervendor'     }) ], 

but error still same (module not found: error: can't resolve 'supervendor' in '...').

provideplugin not solution want do. configuration you've set tells webpack:

whenever supervendor encountered free variable in module, load module supervendor , set variable supervendor exported supervendor.

in other words if have module contains this:

supervendor.somemethod(); 

webpack interprets if were:

var supervendor = require("supervendor"); supervendor.somemethod(); 

what should use externals configuration option:

externals: {   'supervendor': 'supervendor' } 

this tells webpack if module supervendor required should sought external environment name supervendor.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -