我有 index.html,我想在其中加载 hotjar 跟踪代码(仅内联脚本),但这取决于 env 变量。我尝试使用 webpack DefinePlugin
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
}),
并得到这个环境,但我只能从反应组件中得到它。
componentWillMount() {
const env = process.env.NODE_ENV;
document.getElementById('hotjar').innerHTML = env === 'production' ? "...script1" : "...script2"
}
然后我使用“innerHTML”将我的脚本放到 index.html 中,但它并没有像我预期的那样工作。
这是 index.html 的主体
<body>
<div id="root"></div>
<script src="/dist/bundle.js"></script>
<script id="hotjar">
</script>
</body>
那么,我需要在 index.html 或类似的脚本中获取它吗?有人能帮我吗?谢谢。