简体   繁体   English

管理不同构建环境(如QA,UAT,Production)的JavaScript配置文件

[英]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.

相关问题 将相同的Javascript Webapp构建部署到不同的环境 - Deploy same Javascript webapp build to different environments 设置 vue.config.js 根据环境构建不同的页面 - Set vue.config.js to build different pages depends on environments 为不同环境构建 Electron - Build Electron for different environments ExpressJs:不同环境中的配置变量 - ExpressJs: Config variables in different environments 如何在我的 vue 应用程序的 webpack 配置中排除生产构建中的文件 - How to exclude files in production build in the webpack config of my vue application 生成各种环境的javascript配置 - Generate javascript config for various environments 在 Javascript 文件中管理事件传播 - Managing Event Propagation in Javascript Files 合并和管理多个JavaScript文件 - Merging and managing multiple javascript files 是否可以在Web应用程序中使用不同的javascript文件进行调试和生成? - Is it possible to have different javascript files for debugging and production in web application? 用于生产和非生产环境的Segment.io和mixpanel实现配置文件? - Segment.io and mixpanel implementation config file for production and non-production environments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM