Skip to content

Commit 25487ba

Browse files
毛瑞Maorey
authored andcommitted
Feat: 增加可访问的全局变量
1 parent 2c25285 commit 25487ba

File tree

4 files changed

+39
-26
lines changed

4 files changed

+39
-26
lines changed

.env

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ SEARCH_FIELD=_target\d+
2020

2121
# 指定入口或通过cli命令(优先 --entries=foo,bar)指定,逗号分隔
2222
# _ENTRIES=index
23-
# 配置路由别名, json: [
23+
# 配置别名, json: [
2424
# [别名, 路径(相对根目录或入口), 默认指令(可省略)] 或者
2525
# [别名, 路径, [允许的指令, ...], 默认指令(可省略)]
26-
# ... ], 可使用cli命令指定指令(优先 --routes=foo.bar,bar.foo )
27-
_ROUTES=[["@iRoute", "index/route"], ["@oRoute", "other/route"]]
26+
# ... ], 可使用cli命令指定指令(优先 --alias=foo.bar,bar.foo )
27+
_ALIAS=[["@iRoute", "index/route"]]

README.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ yarn build # --watch: 跟踪文件变化 --report: 生成打包分析
151151
yarn build --entries=foo,bar
152152
```
153153

154+
可通过环境变量访问: `process.env.ENTRIES: string[]`
155+
154156
#### 指定路由配置(通过别名):
155157

156158
1. `route` 目录结构
@@ -162,24 +164,26 @@ yarn build # --watch: 跟踪文件变化 --report: 生成打包分析
162164
# ...
163165
```
164166

165-
2. 配置 [.env](.env) `_ROUTES`, 示例:
167+
2. 配置 [.env](.env) `_ALIAS`, 示例:
166168

167169
```bash
168-
# 配置路由别名, json: [
170+
# 配置别名, json: [
169171
# [别名, 路径(相对根目录或入口), 默认指令(可省略)] 或者
170172
# [别名, 路径, [允许的指令, ...], 默认指令(可省略)]
171-
# ... ], 可使用cli命令指定指令(优先 --routes=foo.bar,bar.foo )
172-
_ROUTES=[["@fRoute", "foo/route", "foo"], ["@bRoute", "src/pages/bar/route", ["foo", "bar"], "bar"]]
173+
# ... ], 可使用cli命令指定指令(优先 --alias=foo.bar,bar.foo )
174+
_ALIAS=[["@fRoute", "foo/route", "foo"], ["@bRoute", "src/pages/bar/route", ["foo", "bar"], "bar"]]
173175
```
174176

175-
3. 通过cli命令: `routes`: `入口.指令,...`
177+
3. 通过cli命令: `alias`: `入口.指令,...`
176178

177179
```bash
178-
yarn dev --routes=foo.bar
179-
yarn build --routes=bar # 省略形式: 指令唯一时
180-
yarn build --routes=foo.bar,bar.foo
180+
yarn dev --alias=foo.bar
181+
yarn build --alias=bar # 省略形式: 指令唯一时
182+
yarn build --alias=foo.bar,bar.foo
181183
```
182184

185+
可通过环境变量访问: `process.env.ALIAS: { [key in process.env.ENTRIES]: string[] }`
186+
183187
#### 命令帮助
184188

185189
```bash
@@ -239,8 +243,10 @@ yarn vue-cli-service help # [命令] : 比如 yarn vue-cli-service help test:e2e
239243
│ │── e2e # e2e 测试(cypress): https://www.cypress.io
240244
│ └── unit # unit 测试(jest): https://jestjs.io
241245
├── build # 工具类脚本
242-
├── cypress.json # cypress 配置: https://docs.cypress.io/guides/references/configuration.html
243-
├── tsconfig.json # typeScript 配置: https://www.tslang.cn/docs/handbook/tsconfig-json.html
246+
├── .env # 所有环境的环境变量(可通过 process.env 访问)
247+
├── .env.[mode] # 指定环境的环境变量
248+
├── .env.*.local # 本地环境变量(git忽略)
249+
├── ... # 配置文件
244250
└── vue.config.js # 工程(vue cli)配置入口
245251
```
246252

build/alias.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const path = require('path')
88
const updateJSON = require('./updateJSON')
99

1010
function getArgs() {
11-
const REG_ROUTE = /(?:--)?routes[= ](\w+(?:\.\w+)?(?:,\w+(?:\.\w+)?)*)/
11+
const REG_ROUTE = /(?:--)?alias[= ](\w+(?:\.\w+)?(?:,\w+(?:\.\w+)?)*)/
1212
let args, i
1313
for (args of process.argv) {
1414
if ((args = REG_ROUTE.exec(args))) {
@@ -25,9 +25,9 @@ function getArgs() {
2525
* @param {Objects} pages 页面入口配置
2626
* @param {ChainWebpack} config
2727
* @param {Object} ALIAS 别名字典
28-
* @param {Array} ROUTES 路由别名
28+
* @param {Array} ALIAS_CONFIG 别名配置
2929
*/
30-
module.exports = function(pages, config, ALIAS, ROUTES) {
30+
module.exports = function(pages, config, ALIAS, ALIAS_CONFIG) {
3131
const SUFFIX = '/*'
3232
const REG_BACKSLASH = /\\/g
3333
const ROOT_DIR = path.resolve()
@@ -61,10 +61,11 @@ module.exports = function(pages, config, ALIAS, ROUTES) {
6161
}
6262

6363
/// 【路由别名配置】 ///
64-
if (ROUTES && ROUTES.length) {
64+
const DIC = {} // for 环境变量
65+
if (ALIAS_CONFIG && ALIAS_CONFIG.length) {
6566
const args = getArgs()
6667
let page, arg, i, j
67-
for (entry of ROUTES) {
68+
for (entry of ALIAS_CONFIG) {
6869
alias = entry[1].split('/')
6970
alias[0] || alias.shift()
7071
if (pages[alias[0]]) {
@@ -115,10 +116,13 @@ module.exports = function(pages, config, ALIAS, ROUTES) {
115116
if (!entry[2] || (arg && entry[2].includes(arg))) {
116117
alias = entry[0]
117118
arg && (folderName += '.' + arg)
119+
;(DIC[page] || (DIC[page] = [])).push(arg || '')
118120
fs.existsSync(folderName) && setAlias()
119121
}
120122
}
121123
}
122124

123125
updateJSON('tsconfig.json', 'compilerOptions.paths', TS_PATHS)
126+
127+
return DIC
124128
}

vue.config.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,20 @@ module.exports = {
4141
// https://github.com/neutrinojs/webpack-chain#getting-started
4242
chainWebpack(config) {
4343
config.cache(true) // 使用缓存
44+
45+
/// 别名&环境变量 ///
46+
const prefix = 'process.env.'
47+
const REG_ENV = /^[A-Z]+(?:_[A-Z]+)?$/
4448
let env // 工具人
45-
/// 设置目录别名 已有: @ => src ///
4649
try {
47-
env = JSON.parse(ENV._ROUTES)
50+
env = JSON.parse(ENV._ALIAS)
4851
} catch (error) {}
49-
require('./build/alias')(pages, config, ALIAS, env)
50-
51-
/// 环境变量 ///
52-
env = {}
53-
const prefix = 'process.env.' // 工具人二号
54-
const REG_ENV = /^[A-Z]+(?:_[A-Z]+)?$/
52+
env = {
53+
[prefix + 'ENTRIES']: JSON.stringify(Object.keys(pages)),
54+
[prefix + 'ALIAS']: JSON.stringify(
55+
require('./build/alias')(pages, config, ALIAS, env)
56+
),
57+
}
5558
for (const att in ENV) {
5659
REG_ENV.test(att) && (env[prefix + att] = JSON.stringify(ENV[att]))
5760
}

0 commit comments

Comments
 (0)