Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Webpack.ProvidePlugin加载用户定义的全局对象?_Webpack - Fatal编程技术网

如何使用Webpack.ProvidePlugin加载用户定义的全局对象?

如何使用Webpack.ProvidePlugin加载用户定义的全局对象?,webpack,Webpack,我知道您可以使用ProvidePlugin将每个jQuery注入到使用它的模块中 plugins: [ new Webpack.ProvidePlugin({ $: "jquery", jQuery: "jquery", }) ], 在全局空间中有一个我自己的实用程序模块,我想使用ProvidePlugin来加载,就像jQuery一样,我该怎么做 plugins: [ new webpack.ProvidePlugin({

我知道您可以使用ProvidePlugin将每个jQuery注入到使用它的模块中

plugins: [
    new Webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery",
    })
  ],
在全局空间中有一个我自己的实用程序模块,我想使用ProvidePlugin来加载,就像jQuery一样,我该怎么做

 plugins: [ 
    new webpack.ProvidePlugin({
        'mtUtilities': '../Presentation/Base/Default/js/source/_common/mtUtilities.js'
    })
 ],
上述代码将导致路径错误。程序找不到“mtUtilities”,因为路径是相对的,所以它是错误的。如何修复它

下面的方法应该有效

new webpack.ProvidePlugin({
    'mtUtilities': __dirname + '/../Presentation/Base/Default/js/source/_common/mtUtilities.js'
})

如果我没记错的话,你只需要在你的html中包含你的
mtUtilities.js
标签和配置就像插件:[new webpack.ProvidePlugin({'mtUtilities':'mtUtilities'),