根据 inDev 来设置dev和prod的不同环境变量值
This commit is contained in:
		
							parent
							
								
									12d5278aed
								
							
						
					
					
						commit
						675c16f2b4
					
				@ -9,13 +9,16 @@ module.exports = {
 | 
				
			|||||||
   */
 | 
					   */
 | 
				
			||||||
  merge_envar ({ rawEnvar = {}, envarFiles = ['./envar-base-basic.js', './envar-base-custom.js', './envar-base-secret.js'], hasCommander = true } = {}) {
 | 
					  merge_envar ({ rawEnvar = {}, envarFiles = ['./envar-base-basic.js', './envar-base-custom.js', './envar-base-secret.js'], hasCommander = true } = {}) {
 | 
				
			||||||
    if (!global.envar) {
 | 
					    if (!global.envar) {
 | 
				
			||||||
      global.envar = rawEnvar
 | 
					      global.envar = rawEnvar // 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
 | 
				
			||||||
      // 不知为何,必须定义成全局变量,才能保证多次require只执行一次。
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      console.info('<<<<<<<< Configuring Environment Variables <<<<<<<<')
 | 
					      // process.env.NODE_ENV 不是天然就有的,而是由 cross-env 或 vue/uniapp 配置的。如果通过 vscode 的 launch.json 直接启动,就不存在 process.env.NODE_ENV。因此默认设置一个 prodev,默认值 development 因为在 launch.json 时显然是开发环境。
 | 
				
			||||||
 | 
					      // 在 uniapp 里,在 main.js/App.vue 里都可以访问 process.env.NODE_ENV。在 页面.vue 的代码区域,可以访问,但在模版区域,不能访问。在浏览器中时,不能访问。
 | 
				
			||||||
 | 
					      // 在 uniCloud 里,存在 process.env,但不存在 process.env.NODE_ENV
 | 
				
			||||||
 | 
					      global.envar.prodev = global.envar.prodev || process.env.NODE_ENV || 'development' // server = require('express')(); server.get('env') === server.settings.env === process.env.NODE_ENV
 | 
				
			||||||
 | 
					      global.inDev = global.envar.prodev === 'development' // 为了方便从配置文件里,根据 inDev 来设置不同参数,例如 port: inDev ? 8080 : undefined
 | 
				
			||||||
 | 
					      console.info(`<<<<<<<< Configuring [${global.envar.prodev}] Environment <<<<<<<<`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      console.info('- Loading Configuration Files (读取配置文件)')
 | 
					      console.info('- Loading Configuration Files (读取配置文件)')
 | 
				
			||||||
 | 
					 | 
				
			||||||
      for (let configFile of envarFiles) {
 | 
					      for (let configFile of envarFiles) {
 | 
				
			||||||
        if (fs.existsSync(path.resolve(configFile))) {
 | 
					        if (fs.existsSync(path.resolve(configFile))) {
 | 
				
			||||||
          global.envar = deepmerge(global.envar, require(path.resolve(configFile)))
 | 
					          global.envar = deepmerge(global.envar, require(path.resolve(configFile)))
 | 
				
			||||||
@ -25,17 +28,6 @@ module.exports = {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // process.env.NODE_ENV 不是天然就有的,而是由 cross-env 或 vue/uniapp 配置的。如果通过 vscode 的 launch.json 直接启动,就不存在 process.env.NODE_ENV。因此默认设置一个 prodev,默认值 development 因为在 launch.json 时显然是开发环境。
 | 
					 | 
				
			||||||
      // 在 uniapp 里,在 main.js/App.vue 里都可以访问 process.env.NODE_ENV。在 页面.vue 的代码区域,可以访问,但在模版区域,不能访问。
 | 
					 | 
				
			||||||
      // 在 uniCloud 里,存在 process.env,但不存在 process.env.NODE_ENV
 | 
					 | 
				
			||||||
      global.envar.prodev = global.envar.prodev || process.env.NODE_ENV || 'development' // server = require('express')(); server.get('env') === server.settings.env === process.env.NODE_ENV
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (global.envar.prodev === 'production' && global.envar.ENV_PRODUCTION) {
 | 
					 | 
				
			||||||
        console.info('- Applying Production Configuration (加载生产环境配置)')
 | 
					 | 
				
			||||||
        global.envar = deepmerge(global.envar, global.envar.ENV_PRODUCTION) // 注意,objectMerge后,产生了一个新的对象,而不是在原来的Config里添加
 | 
					 | 
				
			||||||
        delete global.envar.ENV_PRODUCTION
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      if (hasCommander) {
 | 
					      if (hasCommander) {
 | 
				
			||||||
        console.info('- Loading Command Line Parameters (载入命令行参数)')
 | 
					        console.info('- Loading Command Line Parameters (载入命令行参数)')
 | 
				
			||||||
        commander.version(global.envar.Base_Version || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
 | 
					        commander.version(global.envar.Base_Version || '0.0.1', '-v, --version') // 默认是 -V。如果要 -v,就要加 '-v --version'
 | 
				
			||||||
@ -1,5 +1,6 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
  "name": "basend-envar",
 | 
					  "name": "basend-envar",
 | 
				
			||||||
 | 
					  "main": "envar.js",
 | 
				
			||||||
  "description": "环境变量配置",
 | 
					  "description": "环境变量配置",
 | 
				
			||||||
  "version": "0.1.0",
 | 
					  "version": "0.1.0",
 | 
				
			||||||
  "private": true,
 | 
					  "private": true,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user