[英]Managing JavaScript config files for different build environments (like QA, UAT, Production)
So you are building a stand alone Single Page Application in JavaScript that is served statically. 因此,您正在构建一个静态服务的独立单页应用程序。 It connects to an API.
它连接到API。
Your application has a JSON object in its main.js
like this: 您的应用程序在其
main.js
有一个JSON对象,如下所示:
var config = {
apiPath: 'http://dev.api.example.com',
appTitle: 'hello'
};
You use Grunt to create a release build, this transpiles all your SASS, ES6, AMD Modules etc code into one or two JavaScript files, along with this config object. 您使用Grunt创建发布版本,这会将所有SASS,ES6,AMD模块等代码转换为一个或两个JavaScript文件以及此配置对象。
Your production API url is http://api.example.com
and when you do a release build, you want just this value in the config to change. 您的生产API网址是
http://api.example.com
,当您执行发布版本时,您希望配置中的此值更改。 (Maybe you would want to change other config parameters too). (也许你也想改变其他配置参数)。 You have a release config
你有一个发布配置
var config = {
apiPath: 'http://api.example.com'
};
How can a JavaScript config file be altered depending on different types of release environments during a build. 如何在构建期间根据不同类型的发布环境更改JavaScript配置文件。 Notice that I want to merge the release config into the default, not maintain totally seperate files with all the settings.
请注意,我想将发布配置合并到默认设置中, 而不是使用所有设置维护完全独立的文件。 While there are grunt plugins for setting a build mode, I've not found any that have the ability to update config files, only hacks that change which file is pointed to.
虽然有用于设置构建模式的grunt插件,但我没有发现任何能够更新配置文件,只有黑客可以更改指向哪个文件。
ASP.NET has web.config
transforms that do exactly this, any property in the release transform for example, will overwrite the 'default' config on packaging. ASP.NET具有完全相同的
web.config
转换,例如,发布转换中的任何属性都将覆盖打包时的“默认”配置。 Does such a workflow exist using grunt or something? 这样的工作流程是否存在使用grunt或其他东西?
最后,我通过结合使用环境变量和一个名为Confidence的神奇节点来解决问题,在这个节点中,您将所有不同的envs配置设置放在一个文件中,并根据过滤器将它们解析为单个配置(例如环境变量) - 然后可以使用WebPack等工具将这些注入到构建过程中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.