How do I dynamically load scripts based on the environment in the Webpack-packaged project html?

for example, I want to dynamically load Google Analytics based on the environment in index.html. The development environment does not introduce its script, and the production environment introduces its script.

I tried to read the process.env attribute, but unlike the js file within the project, I couldn"t read it in index.html.

is there a solution?

Jun.30,2022

If

has html-webpack-plugin , it would be nice to add a judgment.

<%=process.env.NODE_ENV==='production' ? '<script src="xxx.js"></script>' : ''%>

A simpler approach, you can use different index.html in different environments, which is always easy to do.

Menu