// getArgList.js 获取参数列表
module.exports = function getArgList() {
let argvs
const res = {}
try {
argvs = JSON.parse(process.env.npm_config_argv).original
} catch (ex) {
argvs = process.argv
}
const argv = argvs.slice(2)
for (const i in argv) {
const key = argv[i].match(/--(\S*)=/)[1]
const value = argv[i].split('=')[1]
res[key] = value
}
return res
}
// client.js
const fs = require('fs')
const path = require('path')
const argList = require('./getArgList')()
// 构建配置参数
const site_url = argList.site_url || 'chanshiyu.com'
const site_id = argList.site_id || '500'
module.exports = {
setStart() {
this.setSetting()
},
setSetting() {
console.log('argList==>', argList)
const setting = {
site_url,
site_id
}
console.log('setting==>', setting)
fs.writeFileSync(
path.resolve(__dirname, '../public/setting.js'),
'var setting = ' + JSON.stringify(setting)
)
}
}
// vue.config.js
const client = require('./build/client')
client.setStart()
<script src="<%= BASE_URL %>setting.js?<%=Date.now()%>"></script>