WebPack Expose Loader - ythy/blog GitHub Wiki
可以把jquery 模块中的全局变量window.$ 暴露出来
The expose loader adds modules to the global object. This is useful for debugging, or supporting libraries that depend on libraries in globals.
Let's say you also want to expose it as window.jQuery in addition to window.$ . For multiple expose you can use ! in loader string:
module: {
rules: [{
test: require.resolve('jquery'),
use: [{
loader: 'expose-loader',
options: 'jQuery'
},{
loader: 'expose-loader',
options: '$'
}]
}]
}
The require.resolve is a Node.js call (unrelated to require.resolve in webpack processing). require.resolve gives you the absolute path to the module ( "/.../app/node_modules/react/react.js" ). So the expose only applies to the react module. And it's only exposed when used in the bundle.
jQuery 2.x exports itself to the window object, even if a CommonJs loader is availible.