gpt4 book ai didi

javascript - webpack - imports-loader 和 ProvidePlugin : similar but not the same?

转载 作者:行者123 更新时间:2023-11-29 23:46:26 24 4
gpt4 key购买 nike

这是我的:

a.js

$(() => {
$.x = 42;
console.log('hello from a');
});

b.js

$(() => {
console.log($.x);
console.log('hello from b');
});

index.html

<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="/vendor.js"></script>
<script src="/a.js"></script>
<script src="/b.js"></script>

我的问题是,下面的两个 webpack 配置是否实现了完全相同的结果?

[1] webpack.config.js(带导入加载器)

const config = {
entry: {
a: './a.js',
b: './b.js',
vendor: [
'jquery',
],
},
module: {
rules: [
{
test: /(a|b)\.js$/,
use: 'imports-loader?$=jquery',
},
],
},
};

[2] webpack.config.js(带 ProvidePlugin)

const config = {
entry: {
a: './a.js',
b: './b.js',
vendor: [
'jquery',
],
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
}),
],
};

如果它们相似但不相同,那么什么时候使用一个而不是另一个?

另外,在b.js中,$.x等于undefined,这是否意味着$引用不同的 jQuery 对象?如果是这样,我如何让它们引用相同的(全局)jQuery 实例?

最佳答案

imports-loader 仅在父加载器运行时引入导入。 (在您的情况下,仅在 js 文件上。)导入的文件“附加”到父文件。

ProviderPlugin 导入到“全局”命名空间,使您导入的任何内容都可以在任何地方使用。它通常用于像 jquery 这样的独立库。

相似,但不相同。

关于javascript - webpack - imports-loader 和 ProvidePlugin : similar but not the same?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43853418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com