Skip to content

Commit

Permalink
feat: 支持cli传如x=y的参数并注入页面中,以前只能在node中访问
Browse files Browse the repository at this point in the history
  • Loading branch information
倪俊杰 committed Oct 28, 2022
1 parent 2a87ca9 commit e4baa5c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"url": "git+https://github.com/CodeLittlePrince/epay-sparta.git"
},
"scripts": {
"watch": "epay-sparta-service development",
"watch": "epay-sparta-service development hello=1",
"mock": "nodemon -q ./mockServer/index.js",
"demo": "concurrently \"npm run mock\" \"npm run watch\" --kill-others",
"prod": "epay-sparta-service production",
"prod": "epay-sparta-service production PREPUB=Y",
"analyze": "epay-sparta-service production --analyze",
"online": "epay-sparta-service production --online",
"test:unit": "epay-sparta-service test:unit",
Expand Down
1 change: 1 addition & 0 deletions packages/demo/src/pages/home/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default {
}
},
mounted() {
console.log(process.env)
// ajax get data
// this.$ajax.post('/home/hello', { page: 7 }) // FOR POST
this.$ajax.get(
Expand Down
3 changes: 3 additions & 0 deletions packages/epay-sparta-service/bin/epay-sparta-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ function getArgsFromCommondLine() {
* @param {array} processEnvSegments
*/
function applyProcessEnv(processEnvSegments) {
process.epaySpartaCliArgv = {}

processEnvSegments.forEach(segment => {
if (validateEnvSegmentFormat(segment)) {
const kv = segment.split('=')
const key = kv[0]
const value = kv[1]
process.env[key] = value
process.epaySpartaCliArgv[key] = value // In order to inject to pages, adn only apply x=y arguments
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ module.exports.getConfig = context => {
}
]
}),
// 定义全局常量
new webpack.DefinePlugin({
'process.env': JSON.stringify(process.epaySpartaCliArgv)
}),
// 热替换插件
new webpack.HotModuleReplacementPlugin(),
// 更友好地输出错误信息
Expand Down
13 changes: 9 additions & 4 deletions packages/epay-sparta-service/lib/webpack/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@ module.exports.getConfig = context => {
}),
// 定义全局常量
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"',
ONLINE: ONLINE ? 'true' : 'false' // 一般来说,上线之后埋点会用线上正式的key
}
'process.env': JSON.stringify({
...process.epaySpartaCliArgv,
NODE_ENV: 'production',
ONLINE: ONLINE ? true : false // 一般来说,上线之后埋点会用线上正式的key
})
}),
// 定义全局常量
new webpack.DefinePlugin({
'process.env': JSON.stringify(process.epaySpartaCliArgv)
}),
// It will make sprite.svg error
// new webpack.BannerPlugin('Copyright by 网易支付 https://epay.163.com/'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ module.exports.getConfig = () => {
plugins: [
// 定义全局常量
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"test"'
}
'process.env': JSON.stringify({
...process.epaySpartaCliArgv,
NODE_ENV: 'test'
})
})
]
})
Expand Down

0 comments on commit e4baa5c

Please sign in to comment.