gpt4 book ai didi

javascript - Webpack:仅使用特定配置为 IE9 加载脚本

转载 作者:行者123 更新时间:2023-11-29 21:46:56 25 4
gpt4 key购买 nike

我今天决定试用 webpack,但现在遇到了一个问题。

我们的应用程序与 API 对话。该 api 是可配置的,到目前为止,我一直在通过允许我的 gulpfile 的配置选项来实现这一点,这将确保使用正确的配置选项创建 config.js。

我可以用 webpack 做同样的事情,但我觉得我应该能够更漂亮地解决它。我调查了 DefinePlugin。这部分解决了我的问题,因为我能够定义在构建 .js 文件中使用的属性。

但是,由于我使用的是跨浏览器请求,所以我也在 index.html 中加载了 xdomain:

   <!--[if lte IE 9]>
<script>
var xdomainScript = document.createElement('script');

xdomainScript.setAttribute('src','xdomain.js');
xdomainScript.setAttribute('slave', config.apiUrl + '/proxy.html');

document.head.appendChild(xdomainScript);
</script>
<![endif]-->

这就是问题所在:definePlugin 插件只定义用于built javascript 文件的变量,而不是用于HTML 的变量。所以,我可以将这些东西移动到一个 JS 文件,但我不知道如何将它与 webpack 结合使用,使其仅适用于 IE9。

谁能教教我?

最佳答案

Webpack 允许您构建一些包。只需创建另一个具有更多依赖项的条目文件 - 特别是针对 IE - 然后在你的条件注释中使用它。

网络包配置:

entry: {
'index': 'pages/index/index.js',
'index-ie': 'pages/index/index.ie.js' //require('./index.js'); require('./ie-script.js');
},

你的页面:

<!--[if gt IE 9]>-->
<script src="index.js"></script>
<!--<![endif]-->
<!--[if lte IE 9]>
<script src="index-ie.js"></script>
<![endif]-->

关于javascript - Webpack:仅使用特定配置为 IE9 加载脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30806438/

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