在 webpack AngularJS 项目中访问环境变量

Accessing env variables inside webpack AngularJS project

我有一个 AngularJS 项目,它使用 webpack 进行捆绑、服务和构建 - 没有使用诸如 bower 或 gulp 之类的任务运行器。我希望能够为我将在本地和生产中使用的 REST API 端点等设置环境变量,然后在我的实际 AngularJS 项目文件中访问它们,特别是内部控制器。定义这些环境变量并将其传递到项目中的最佳方式是什么?

解决方案是使用 Webpack 的 definePlugin 来定义 free/global 个变量:

webpack.github.io/docs/list-of-plugins.html#defineplugin

new webpack.DefinePlugin({
    VERSION: JSON.stringify("5fa3b9"),
    BROWSER_SUPPORTS_HTML5: true,
    TWO: "1+1",
    "typeof window": JSON.stringify("object")
})