'use strict'; /** * 动态设置环境host.js的地址 * hostUrl => host.js路径 * process.env.npm_config_argv => 返回 {"remain":[],"cooked":["run","dev"],"original":["run","dev"]} | {"remain":[],"cooked":["run","build"],"original":["run","build"]} * runType => 执行哪个环境 */ const fs = require('fs'); const path = require('path'); let hostUrl = path.resolve(__dirname, './../request/host.js'); let configArgv = process.env.npm_config_argv; let runType = JSON.parse(configArgv).cooked[1]; !(function() { let obj = {}; console.log('runType执行'); if (runType === 'dev') { console.log('执行线上环境'); obj = { ex: 'http://xxx.xx.148.81', hd: 'http://xxx.xx.148.81', }; } if (runType === 'build') { console.log('执行开发环境'); obj = { ex: 'https://xxx.io', hd: 'https://xxx.io', }; } let routerContent = `export default ${JSON.stringify(obj)}`; fs.writeFile(hostUrl, routerContent, (err, data) => { console.log('setHost设置成功'); }); })();