@mpilk/mp-route 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -0
- package/bin/cli.js +1 -1
- package/dist/chunk-6MZ3UWKK.js +262 -262
- package/dist/cli.cjs +331 -331
- package/dist/cli.d.cts +1 -1
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +18 -18
- package/dist/index.cjs +287 -287
- package/dist/index.d.cts +357 -357
- package/dist/index.d.ts +357 -357
- package/dist/index.js +11 -11
- package/package.json +51 -40
package/dist/cli.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { builder, builderWithWatch } from './chunk-6MZ3UWKK.js';
|
|
3
|
-
import { program } from 'commander';
|
|
4
|
-
|
|
5
|
-
// package.json
|
|
6
|
-
var version = "
|
|
7
|
-
|
|
8
|
-
// src/cli.ts
|
|
9
|
-
program.name("uni-route").description(`\u8FD9\u662F\u4E00\u4E2A\u7528\u6765\u751F\u6210 uni-app \u8DEF\u7531\u6587\u4EF6\u7684\u5DE5\u5177
|
|
10
|
-
\u901A\u8FC7\u626B\u63CFsrc\u76EE\u5F55\u4E0B\u7684\u914D\u7F6E\u6587\u4EF6\uFF0C\u751F\u6210 uni-app \u8DEF\u7531\u914D\u7F6E\u6587\u4EF6\u3002`).version(version);
|
|
11
|
-
program.option("-i, --input <input>", "\u626B\u63CF\u914D\u7F6E\u7684\u6587\u4EF6\u5939\u5165\u53E3\u8DEF\u5F84", "src/").option("-rn, --routeName <routeName>", "\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84", "route").option("-tn, --tabbarName <tabbarName>", "\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84", "tabbar").option("-pp, --pagesPath <pagesPath>", "\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84", "src/pages.json").option("-vvv, --verbose", "\u8F93\u51FA\u8BE6\u7EC6\u7684\u8C03\u8BD5\u4FE1\u606F");
|
|
12
|
-
program.command("build", { isDefault: true }).description("\u641C\u7D22\u8DEF\u5F84\u4E0B\u7684\u8DEF\u7531\u6587\u4EF6\uFF0C\u751F\u6210\u8DEF\u7531\u914D\u7F6E").action((_, command) => {
|
|
13
|
-
builder(command.optsWithGlobals());
|
|
14
|
-
});
|
|
15
|
-
program.command("watch").description("\u76D1\u542C\u6587\u4EF6\u53D8\u5316\uFF0C\u5B9E\u65F6\u751F\u6210\u8DEF\u7531\u914D\u7F6E").option("-t --delay [delay]", "\u5EF6\u8FDF\u65F6\u95F4").action((_, command) => {
|
|
16
|
-
builderWithWatch(command.optsWithGlobals());
|
|
17
|
-
});
|
|
18
|
-
program.parse();
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { builder, builderWithWatch } from './chunk-6MZ3UWKK.js';
|
|
3
|
+
import { program } from 'commander';
|
|
4
|
+
|
|
5
|
+
// package.json
|
|
6
|
+
var version = "3.0.1";
|
|
7
|
+
|
|
8
|
+
// src/cli.ts
|
|
9
|
+
program.name("uni-route").description(`\u8FD9\u662F\u4E00\u4E2A\u7528\u6765\u751F\u6210 uni-app \u8DEF\u7531\u6587\u4EF6\u7684\u5DE5\u5177
|
|
10
|
+
\u901A\u8FC7\u626B\u63CFsrc\u76EE\u5F55\u4E0B\u7684\u914D\u7F6E\u6587\u4EF6\uFF0C\u751F\u6210 uni-app \u8DEF\u7531\u914D\u7F6E\u6587\u4EF6\u3002`).version(version);
|
|
11
|
+
program.option("-i, --input <input>", "\u626B\u63CF\u914D\u7F6E\u7684\u6587\u4EF6\u5939\u5165\u53E3\u8DEF\u5F84", "src/").option("-rn, --routeName <routeName>", "\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84", "route").option("-tn, --tabbarName <tabbarName>", "\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84", "tabbar").option("-pp, --pagesPath <pagesPath>", "\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84", "src/pages.json").option("-vvv, --verbose", "\u8F93\u51FA\u8BE6\u7EC6\u7684\u8C03\u8BD5\u4FE1\u606F");
|
|
12
|
+
program.command("build", { isDefault: true }).description("\u641C\u7D22\u8DEF\u5F84\u4E0B\u7684\u8DEF\u7531\u6587\u4EF6\uFF0C\u751F\u6210\u8DEF\u7531\u914D\u7F6E").action((_, command) => {
|
|
13
|
+
builder(command.optsWithGlobals());
|
|
14
|
+
});
|
|
15
|
+
program.command("watch").description("\u76D1\u542C\u6587\u4EF6\u53D8\u5316\uFF0C\u5B9E\u65F6\u751F\u6210\u8DEF\u7531\u914D\u7F6E").option("-t --delay [delay]", "\u5EF6\u8FDF\u65F6\u95F4").action((_, command) => {
|
|
16
|
+
builderWithWatch(command.optsWithGlobals());
|
|
17
|
+
});
|
|
18
|
+
program.parse();
|
package/dist/index.cjs
CHANGED
|
@@ -1,287 +1,287 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
var path = require('path')
|
|
4
|
-
var fs = require('fs')
|
|
5
|
-
var pino = require('pino')
|
|
6
|
-
var mpShared = require('@mpilk/mp-shared')
|
|
7
|
-
var lodashEs = require('lodash-es')
|
|
8
|
-
var chokidar = require('chokidar')
|
|
9
|
-
|
|
10
|
-
function _interopDefault(e) {
|
|
11
|
-
return e && e.__esModule ? e : { default: e }
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
var path__default = /*#__PURE__*/ _interopDefault(path)
|
|
15
|
-
var fs__default = /*#__PURE__*/ _interopDefault(fs)
|
|
16
|
-
var pino__default = /*#__PURE__*/ _interopDefault(pino)
|
|
17
|
-
var chokidar__default = /*#__PURE__*/ _interopDefault(chokidar)
|
|
18
|
-
|
|
19
|
-
// src/helper.ts
|
|
20
|
-
function defineMiniRouter(config) {
|
|
21
|
-
return config
|
|
22
|
-
}
|
|
23
|
-
function defineMiniTabbar(config) {
|
|
24
|
-
return config
|
|
25
|
-
}
|
|
26
|
-
var _indexPath = ''
|
|
27
|
-
var logger = pino__default.default({
|
|
28
|
-
transport: {
|
|
29
|
-
target: 'pino-pretty',
|
|
30
|
-
options: {
|
|
31
|
-
colorize: true
|
|
32
|
-
// ignore: 'hostname,pid',
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
})
|
|
36
|
-
var verboseLogger = logger.child({ verbose: true })
|
|
37
|
-
function builderWithWatch(options) {
|
|
38
|
-
let timer = null
|
|
39
|
-
function run() {
|
|
40
|
-
if (timer) clearTimeout(timer)
|
|
41
|
-
timer = setTimeout(() => {
|
|
42
|
-
timer = null
|
|
43
|
-
return builder(options)
|
|
44
|
-
}, options?.delay || 200)
|
|
45
|
-
}
|
|
46
|
-
return chokidar__default.default
|
|
47
|
-
.watch([
|
|
48
|
-
`src/**/${options?.routeName || 'route'}.{ts,js}`,
|
|
49
|
-
`src/${options?.tabbarName || 'tabbar'}.{ts,js}`
|
|
50
|
-
])
|
|
51
|
-
.on('all', (eventName) => {
|
|
52
|
-
switch (eventName) {
|
|
53
|
-
case 'add':
|
|
54
|
-
case 'change':
|
|
55
|
-
case 'unlink':
|
|
56
|
-
run()
|
|
57
|
-
break
|
|
58
|
-
}
|
|
59
|
-
})
|
|
60
|
-
}
|
|
61
|
-
async function builder(options = {}) {
|
|
62
|
-
const {
|
|
63
|
-
input = './src',
|
|
64
|
-
routeName = 'route',
|
|
65
|
-
tabbarName = 'tabbar',
|
|
66
|
-
pagesPath = './src/pages.json',
|
|
67
|
-
verbose
|
|
68
|
-
} = options
|
|
69
|
-
logger.info('\u5F00\u59CB\u751F\u6210\u8DEF\u7531\u6587\u4EF6...')
|
|
70
|
-
const filePaths = loadDirFiles(input, routeName)
|
|
71
|
-
logger.info(
|
|
72
|
-
`\u6839\u636E ${routeName} \u627E\u5230 ${filePaths.length} \u4E2A\u6587\u4EF6`
|
|
73
|
-
)
|
|
74
|
-
const routeConfigs = []
|
|
75
|
-
for (const filePath of filePaths) {
|
|
76
|
-
verbose &&
|
|
77
|
-
verboseLogger.info(`\u5F00\u59CB\u89E3\u6790\u6587\u4EF6 ${filePath}`)
|
|
78
|
-
let content = await mpShared.loadFileContent(filePath)
|
|
79
|
-
if (!content || typeof content !== 'object' || lodashEs.isEmpty(content)) {
|
|
80
|
-
verbose &&
|
|
81
|
-
verboseLogger.info(
|
|
82
|
-
`\u6587\u4EF6 ${filePath} \u4E3A\u7A7A\uFF0C\u8DF3\u8FC7`
|
|
83
|
-
)
|
|
84
|
-
continue
|
|
85
|
-
}
|
|
86
|
-
if (!lodashEs.isArray(content)) content = [content]
|
|
87
|
-
verbose &&
|
|
88
|
-
verboseLogger.info(
|
|
89
|
-
`\u6587\u4EF6 ${filePath} \u89E3\u6790\u5B8C\u6210\uFF1A${JSON.stringify(
|
|
90
|
-
content
|
|
91
|
-
)}`
|
|
92
|
-
)
|
|
93
|
-
routeConfigs.push(...content)
|
|
94
|
-
}
|
|
95
|
-
const rs = buildRoute(routeConfigs)
|
|
96
|
-
if (_indexPath) {
|
|
97
|
-
const idx2 = rs.pages.findIndex((page) => page.path === _indexPath)
|
|
98
|
-
if (idx2 !== -1) rs.pages.unshift(...rs.pages.splice(idx2, 1))
|
|
99
|
-
}
|
|
100
|
-
logger.info('\u8DEF\u7531\u6587\u4EF6\u89E3\u6790\u5B8C\u6210')
|
|
101
|
-
if (!rs.condition.list.length) rs.condition = void 0
|
|
102
|
-
else if (rs.condition.list.length && rs.condition.current === -1)
|
|
103
|
-
rs.condition.current = 0
|
|
104
|
-
const idx = rs.pages.findIndex((page) => page['_index'])
|
|
105
|
-
if (idx !== -1) rs.pages.unshift(...rs.pages.splice(idx, 1))
|
|
106
|
-
let tabbarObj
|
|
107
|
-
const [tabbarPath] = loadDirFiles('./src', tabbarName)
|
|
108
|
-
if (tabbarPath && !lodashEs.isEmpty(tabbarPath)) {
|
|
109
|
-
logger.info(`\u6839\u636E ${tabbarName} \u627E\u52301\u4E2A\u6587\u4EF6`)
|
|
110
|
-
tabbarObj = await mpShared.loadFileContent(tabbarPath)
|
|
111
|
-
if (tabbarObj && !lodashEs.isEmpty(tabbarObj)) rs.tabBar = tabbarObj
|
|
112
|
-
}
|
|
113
|
-
const {
|
|
114
|
-
debug: _debug = false,
|
|
115
|
-
pages,
|
|
116
|
-
subPackages,
|
|
117
|
-
preloadRule,
|
|
118
|
-
condition,
|
|
119
|
-
tabBar,
|
|
120
|
-
...otherProps
|
|
121
|
-
} = await mpShared.loadJson(pagesPath)
|
|
122
|
-
const writeContent = {
|
|
123
|
-
debug: _debug,
|
|
124
|
-
pages: rs.pages || pages,
|
|
125
|
-
subPackages: rs.subPackages || subPackages,
|
|
126
|
-
preloadRule: rs.preloadRule || preloadRule,
|
|
127
|
-
condition: rs.condition || condition,
|
|
128
|
-
tabBar: tabbarObj || tabBar,
|
|
129
|
-
...otherProps
|
|
130
|
-
}
|
|
131
|
-
await mpShared.outputFile(pagesPath, JSON.stringify(writeContent, null, 2))
|
|
132
|
-
logger.info('\u8DEF\u7531\u6587\u4EF6\u751F\u6210\u5B8C\u6210\uFF01')
|
|
133
|
-
}
|
|
134
|
-
function buildRoute(content, ext = {}) {
|
|
135
|
-
const result = {
|
|
136
|
-
pages: [],
|
|
137
|
-
subPackages: [],
|
|
138
|
-
preloadRule: {},
|
|
139
|
-
condition: {
|
|
140
|
-
current: -1,
|
|
141
|
-
list: []
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
const { parentName = '', parentPath = '', subpackageRoot = '' } = ext
|
|
145
|
-
for (const route of content) {
|
|
146
|
-
const {
|
|
147
|
-
subPackages = false,
|
|
148
|
-
isSubpackage = subPackages,
|
|
149
|
-
path: path2,
|
|
150
|
-
title,
|
|
151
|
-
name,
|
|
152
|
-
children
|
|
153
|
-
} = route
|
|
154
|
-
const style = {
|
|
155
|
-
navigationBarTitleText: title || name || parentName,
|
|
156
|
-
...route.style
|
|
157
|
-
}
|
|
158
|
-
const _path = pathJoin(parentPath, path2)
|
|
159
|
-
if (isSubpackage && !subpackageRoot) {
|
|
160
|
-
let { preLoadPath, preLoadNetwork } = route
|
|
161
|
-
const { pages, preloadRule, condition } = buildRoute(children ?? [], {
|
|
162
|
-
parentName: style.navigationBarTitleText,
|
|
163
|
-
subpackageRoot: _path
|
|
164
|
-
})
|
|
165
|
-
const subPackage = {
|
|
166
|
-
root: _path,
|
|
167
|
-
pages
|
|
168
|
-
}
|
|
169
|
-
result.preloadRule = deepMerge(result.preloadRule, preloadRule)
|
|
170
|
-
result.condition = deepMerge(result.condition, condition)
|
|
171
|
-
if (condition.current > -1)
|
|
172
|
-
result.condition.current = result.condition?.list?.indexOf(
|
|
173
|
-
condition.list[condition.current]
|
|
174
|
-
)
|
|
175
|
-
result.subPackages.push(subPackage)
|
|
176
|
-
if (preLoadPath) {
|
|
177
|
-
if (!lodashEs.isArray(preLoadPath)) preLoadPath = [preLoadPath]
|
|
178
|
-
const preLoadRule = preLoadPath.reduce((acc, cur) => {
|
|
179
|
-
acc[pathJoin(cur)] = {
|
|
180
|
-
network: preLoadNetwork,
|
|
181
|
-
packages: [_path]
|
|
182
|
-
}
|
|
183
|
-
return acc
|
|
184
|
-
}, {})
|
|
185
|
-
result.preloadRule = deepMerge(result.preloadRule, preLoadRule)
|
|
186
|
-
}
|
|
187
|
-
continue
|
|
188
|
-
}
|
|
189
|
-
if (children?.length) {
|
|
190
|
-
const {
|
|
191
|
-
pages,
|
|
192
|
-
subPackages: subPackages2,
|
|
193
|
-
preloadRule,
|
|
194
|
-
condition
|
|
195
|
-
} = buildRoute(children, {
|
|
196
|
-
parentName: style.navigationBarTitleText,
|
|
197
|
-
parentPath: _path,
|
|
198
|
-
subpackageRoot
|
|
199
|
-
})
|
|
200
|
-
result.pages = deepMerge(result.pages, pages)
|
|
201
|
-
result.subPackages = deepMerge(result.subPackages, subPackages2)
|
|
202
|
-
result.preloadRule = deepMerge(result.preloadRule, preloadRule)
|
|
203
|
-
result.condition = deepMerge(result.condition, condition)
|
|
204
|
-
if (condition.current > -1)
|
|
205
|
-
result.condition.current = result.condition?.list?.indexOf(
|
|
206
|
-
condition.list[condition.current]
|
|
207
|
-
)
|
|
208
|
-
} else {
|
|
209
|
-
const { conditionActive, conditionQuery } = route
|
|
210
|
-
const item = {
|
|
211
|
-
path: _path,
|
|
212
|
-
style
|
|
213
|
-
}
|
|
214
|
-
route.isIndex && (_indexPath = _path)
|
|
215
|
-
result.pages.push(item)
|
|
216
|
-
if (conditionQuery || conditionActive) {
|
|
217
|
-
const conditionItem = {
|
|
218
|
-
name: style.navigationBarTitleText,
|
|
219
|
-
path: _path,
|
|
220
|
-
query: conditionQuery
|
|
221
|
-
}
|
|
222
|
-
result.condition.list.push(conditionItem)
|
|
223
|
-
if (conditionActive)
|
|
224
|
-
result.condition.current =
|
|
225
|
-
result.condition.list?.indexOf(conditionItem)
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
return result
|
|
230
|
-
}
|
|
231
|
-
var extension = ['.ts', '.js', '.mjs', '.json']
|
|
232
|
-
function loadDirFiles(roots, name, exitByFirst = false) {
|
|
233
|
-
if (!Array.isArray(roots)) roots = [roots]
|
|
234
|
-
const files = []
|
|
235
|
-
for (let root of roots) {
|
|
236
|
-
root = path__default.default.resolve(root)
|
|
237
|
-
if (!fs__default.default.existsSync(root)) continue
|
|
238
|
-
const stat = fs__default.default.statSync(root)
|
|
239
|
-
const regex = new RegExp(`${name}(${extension.join('|')})$`)
|
|
240
|
-
if (stat.isFile() && regex.test(root)) {
|
|
241
|
-
files.push(root)
|
|
242
|
-
if (exitByFirst) break
|
|
243
|
-
}
|
|
244
|
-
if (stat.isDirectory()) {
|
|
245
|
-
const dirents = fs__default.default
|
|
246
|
-
.readdirSync(root)
|
|
247
|
-
.map((o) => path__default.default.resolve(root, o))
|
|
248
|
-
files.push(...loadDirFiles(dirents, name))
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
return files
|
|
252
|
-
}
|
|
253
|
-
function pathJoin(...paths) {
|
|
254
|
-
return paths.join('/').split('/').filter(Boolean).join('/')
|
|
255
|
-
}
|
|
256
|
-
function deepMerge(target, ...sources) {
|
|
257
|
-
if (typeof target !== 'object' || target === null) target = {}
|
|
258
|
-
if (!sources.length) return target
|
|
259
|
-
for (const source of sources) {
|
|
260
|
-
if (typeof source !== 'object' || source === null) continue
|
|
261
|
-
if (lodashEs.isArray(target) && lodashEs.isArray(source)) {
|
|
262
|
-
const set = /* @__PURE__ */ new Set([...target, ...source])
|
|
263
|
-
return [...set]
|
|
264
|
-
} else if (
|
|
265
|
-
lodashEs.isPlainObject(target) &&
|
|
266
|
-
lodashEs.isPlainObject(source)
|
|
267
|
-
) {
|
|
268
|
-
for (const key in source) {
|
|
269
|
-
if (
|
|
270
|
-
key in target &&
|
|
271
|
-
typeof target[key] === 'object' &&
|
|
272
|
-
typeof source[key] === 'object'
|
|
273
|
-
) {
|
|
274
|
-
target[key] = deepMerge(target[key], source[key])
|
|
275
|
-
} else {
|
|
276
|
-
target[key] = source[key] || target[key]
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
return target
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
exports.builder = builder
|
|
285
|
-
exports.builderWithWatch = builderWithWatch
|
|
286
|
-
exports.defineMiniRouter = defineMiniRouter
|
|
287
|
-
exports.defineMiniTabbar = defineMiniTabbar
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
var path = require('path')
|
|
4
|
+
var fs = require('fs')
|
|
5
|
+
var pino = require('pino')
|
|
6
|
+
var mpShared = require('@mpilk/mp-shared')
|
|
7
|
+
var lodashEs = require('lodash-es')
|
|
8
|
+
var chokidar = require('chokidar')
|
|
9
|
+
|
|
10
|
+
function _interopDefault(e) {
|
|
11
|
+
return e && e.__esModule ? e : { default: e }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
var path__default = /*#__PURE__*/ _interopDefault(path)
|
|
15
|
+
var fs__default = /*#__PURE__*/ _interopDefault(fs)
|
|
16
|
+
var pino__default = /*#__PURE__*/ _interopDefault(pino)
|
|
17
|
+
var chokidar__default = /*#__PURE__*/ _interopDefault(chokidar)
|
|
18
|
+
|
|
19
|
+
// src/helper.ts
|
|
20
|
+
function defineMiniRouter(config) {
|
|
21
|
+
return config
|
|
22
|
+
}
|
|
23
|
+
function defineMiniTabbar(config) {
|
|
24
|
+
return config
|
|
25
|
+
}
|
|
26
|
+
var _indexPath = ''
|
|
27
|
+
var logger = pino__default.default({
|
|
28
|
+
transport: {
|
|
29
|
+
target: 'pino-pretty',
|
|
30
|
+
options: {
|
|
31
|
+
colorize: true
|
|
32
|
+
// ignore: 'hostname,pid',
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
var verboseLogger = logger.child({ verbose: true })
|
|
37
|
+
function builderWithWatch(options) {
|
|
38
|
+
let timer = null
|
|
39
|
+
function run() {
|
|
40
|
+
if (timer) clearTimeout(timer)
|
|
41
|
+
timer = setTimeout(() => {
|
|
42
|
+
timer = null
|
|
43
|
+
return builder(options)
|
|
44
|
+
}, options?.delay || 200)
|
|
45
|
+
}
|
|
46
|
+
return chokidar__default.default
|
|
47
|
+
.watch([
|
|
48
|
+
`src/**/${options?.routeName || 'route'}.{ts,js}`,
|
|
49
|
+
`src/${options?.tabbarName || 'tabbar'}.{ts,js}`
|
|
50
|
+
])
|
|
51
|
+
.on('all', (eventName) => {
|
|
52
|
+
switch (eventName) {
|
|
53
|
+
case 'add':
|
|
54
|
+
case 'change':
|
|
55
|
+
case 'unlink':
|
|
56
|
+
run()
|
|
57
|
+
break
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
async function builder(options = {}) {
|
|
62
|
+
const {
|
|
63
|
+
input = './src',
|
|
64
|
+
routeName = 'route',
|
|
65
|
+
tabbarName = 'tabbar',
|
|
66
|
+
pagesPath = './src/pages.json',
|
|
67
|
+
verbose
|
|
68
|
+
} = options
|
|
69
|
+
logger.info('\u5F00\u59CB\u751F\u6210\u8DEF\u7531\u6587\u4EF6...')
|
|
70
|
+
const filePaths = loadDirFiles(input, routeName)
|
|
71
|
+
logger.info(
|
|
72
|
+
`\u6839\u636E ${routeName} \u627E\u5230 ${filePaths.length} \u4E2A\u6587\u4EF6`
|
|
73
|
+
)
|
|
74
|
+
const routeConfigs = []
|
|
75
|
+
for (const filePath of filePaths) {
|
|
76
|
+
verbose &&
|
|
77
|
+
verboseLogger.info(`\u5F00\u59CB\u89E3\u6790\u6587\u4EF6 ${filePath}`)
|
|
78
|
+
let content = await mpShared.loadFileContent(filePath)
|
|
79
|
+
if (!content || typeof content !== 'object' || lodashEs.isEmpty(content)) {
|
|
80
|
+
verbose &&
|
|
81
|
+
verboseLogger.info(
|
|
82
|
+
`\u6587\u4EF6 ${filePath} \u4E3A\u7A7A\uFF0C\u8DF3\u8FC7`
|
|
83
|
+
)
|
|
84
|
+
continue
|
|
85
|
+
}
|
|
86
|
+
if (!lodashEs.isArray(content)) content = [content]
|
|
87
|
+
verbose &&
|
|
88
|
+
verboseLogger.info(
|
|
89
|
+
`\u6587\u4EF6 ${filePath} \u89E3\u6790\u5B8C\u6210\uFF1A${JSON.stringify(
|
|
90
|
+
content
|
|
91
|
+
)}`
|
|
92
|
+
)
|
|
93
|
+
routeConfigs.push(...content)
|
|
94
|
+
}
|
|
95
|
+
const rs = buildRoute(routeConfigs)
|
|
96
|
+
if (_indexPath) {
|
|
97
|
+
const idx2 = rs.pages.findIndex((page) => page.path === _indexPath)
|
|
98
|
+
if (idx2 !== -1) rs.pages.unshift(...rs.pages.splice(idx2, 1))
|
|
99
|
+
}
|
|
100
|
+
logger.info('\u8DEF\u7531\u6587\u4EF6\u89E3\u6790\u5B8C\u6210')
|
|
101
|
+
if (!rs.condition.list.length) rs.condition = void 0
|
|
102
|
+
else if (rs.condition.list.length && rs.condition.current === -1)
|
|
103
|
+
rs.condition.current = 0
|
|
104
|
+
const idx = rs.pages.findIndex((page) => page['_index'])
|
|
105
|
+
if (idx !== -1) rs.pages.unshift(...rs.pages.splice(idx, 1))
|
|
106
|
+
let tabbarObj
|
|
107
|
+
const [tabbarPath] = loadDirFiles('./src', tabbarName)
|
|
108
|
+
if (tabbarPath && !lodashEs.isEmpty(tabbarPath)) {
|
|
109
|
+
logger.info(`\u6839\u636E ${tabbarName} \u627E\u52301\u4E2A\u6587\u4EF6`)
|
|
110
|
+
tabbarObj = await mpShared.loadFileContent(tabbarPath)
|
|
111
|
+
if (tabbarObj && !lodashEs.isEmpty(tabbarObj)) rs.tabBar = tabbarObj
|
|
112
|
+
}
|
|
113
|
+
const {
|
|
114
|
+
debug: _debug = false,
|
|
115
|
+
pages,
|
|
116
|
+
subPackages,
|
|
117
|
+
preloadRule,
|
|
118
|
+
condition,
|
|
119
|
+
tabBar,
|
|
120
|
+
...otherProps
|
|
121
|
+
} = await mpShared.loadJson(pagesPath)
|
|
122
|
+
const writeContent = {
|
|
123
|
+
debug: _debug,
|
|
124
|
+
pages: rs.pages || pages,
|
|
125
|
+
subPackages: rs.subPackages || subPackages,
|
|
126
|
+
preloadRule: rs.preloadRule || preloadRule,
|
|
127
|
+
condition: rs.condition || condition,
|
|
128
|
+
tabBar: tabbarObj || tabBar,
|
|
129
|
+
...otherProps
|
|
130
|
+
}
|
|
131
|
+
await mpShared.outputFile(pagesPath, JSON.stringify(writeContent, null, 2))
|
|
132
|
+
logger.info('\u8DEF\u7531\u6587\u4EF6\u751F\u6210\u5B8C\u6210\uFF01')
|
|
133
|
+
}
|
|
134
|
+
function buildRoute(content, ext = {}) {
|
|
135
|
+
const result = {
|
|
136
|
+
pages: [],
|
|
137
|
+
subPackages: [],
|
|
138
|
+
preloadRule: {},
|
|
139
|
+
condition: {
|
|
140
|
+
current: -1,
|
|
141
|
+
list: []
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const { parentName = '', parentPath = '', subpackageRoot = '' } = ext
|
|
145
|
+
for (const route of content) {
|
|
146
|
+
const {
|
|
147
|
+
subPackages = false,
|
|
148
|
+
isSubpackage = subPackages,
|
|
149
|
+
path: path2,
|
|
150
|
+
title,
|
|
151
|
+
name,
|
|
152
|
+
children
|
|
153
|
+
} = route
|
|
154
|
+
const style = {
|
|
155
|
+
navigationBarTitleText: title || name || parentName,
|
|
156
|
+
...route.style
|
|
157
|
+
}
|
|
158
|
+
const _path = pathJoin(parentPath, path2)
|
|
159
|
+
if (isSubpackage && !subpackageRoot) {
|
|
160
|
+
let { preLoadPath, preLoadNetwork } = route
|
|
161
|
+
const { pages, preloadRule, condition } = buildRoute(children ?? [], {
|
|
162
|
+
parentName: style.navigationBarTitleText,
|
|
163
|
+
subpackageRoot: _path
|
|
164
|
+
})
|
|
165
|
+
const subPackage = {
|
|
166
|
+
root: _path,
|
|
167
|
+
pages
|
|
168
|
+
}
|
|
169
|
+
result.preloadRule = deepMerge(result.preloadRule, preloadRule)
|
|
170
|
+
result.condition = deepMerge(result.condition, condition)
|
|
171
|
+
if (condition.current > -1)
|
|
172
|
+
result.condition.current = result.condition?.list?.indexOf(
|
|
173
|
+
condition.list[condition.current]
|
|
174
|
+
)
|
|
175
|
+
result.subPackages.push(subPackage)
|
|
176
|
+
if (preLoadPath) {
|
|
177
|
+
if (!lodashEs.isArray(preLoadPath)) preLoadPath = [preLoadPath]
|
|
178
|
+
const preLoadRule = preLoadPath.reduce((acc, cur) => {
|
|
179
|
+
acc[pathJoin(cur)] = {
|
|
180
|
+
network: preLoadNetwork,
|
|
181
|
+
packages: [_path]
|
|
182
|
+
}
|
|
183
|
+
return acc
|
|
184
|
+
}, {})
|
|
185
|
+
result.preloadRule = deepMerge(result.preloadRule, preLoadRule)
|
|
186
|
+
}
|
|
187
|
+
continue
|
|
188
|
+
}
|
|
189
|
+
if (children?.length) {
|
|
190
|
+
const {
|
|
191
|
+
pages,
|
|
192
|
+
subPackages: subPackages2,
|
|
193
|
+
preloadRule,
|
|
194
|
+
condition
|
|
195
|
+
} = buildRoute(children, {
|
|
196
|
+
parentName: style.navigationBarTitleText,
|
|
197
|
+
parentPath: _path,
|
|
198
|
+
subpackageRoot
|
|
199
|
+
})
|
|
200
|
+
result.pages = deepMerge(result.pages, pages)
|
|
201
|
+
result.subPackages = deepMerge(result.subPackages, subPackages2)
|
|
202
|
+
result.preloadRule = deepMerge(result.preloadRule, preloadRule)
|
|
203
|
+
result.condition = deepMerge(result.condition, condition)
|
|
204
|
+
if (condition.current > -1)
|
|
205
|
+
result.condition.current = result.condition?.list?.indexOf(
|
|
206
|
+
condition.list[condition.current]
|
|
207
|
+
)
|
|
208
|
+
} else {
|
|
209
|
+
const { conditionActive, conditionQuery } = route
|
|
210
|
+
const item = {
|
|
211
|
+
path: _path,
|
|
212
|
+
style
|
|
213
|
+
}
|
|
214
|
+
route.isIndex && (_indexPath = _path)
|
|
215
|
+
result.pages.push(item)
|
|
216
|
+
if (conditionQuery || conditionActive) {
|
|
217
|
+
const conditionItem = {
|
|
218
|
+
name: style.navigationBarTitleText,
|
|
219
|
+
path: _path,
|
|
220
|
+
query: conditionQuery
|
|
221
|
+
}
|
|
222
|
+
result.condition.list.push(conditionItem)
|
|
223
|
+
if (conditionActive)
|
|
224
|
+
result.condition.current =
|
|
225
|
+
result.condition.list?.indexOf(conditionItem)
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return result
|
|
230
|
+
}
|
|
231
|
+
var extension = ['.ts', '.js', '.mjs', '.json']
|
|
232
|
+
function loadDirFiles(roots, name, exitByFirst = false) {
|
|
233
|
+
if (!Array.isArray(roots)) roots = [roots]
|
|
234
|
+
const files = []
|
|
235
|
+
for (let root of roots) {
|
|
236
|
+
root = path__default.default.resolve(root)
|
|
237
|
+
if (!fs__default.default.existsSync(root)) continue
|
|
238
|
+
const stat = fs__default.default.statSync(root)
|
|
239
|
+
const regex = new RegExp(`${name}(${extension.join('|')})$`)
|
|
240
|
+
if (stat.isFile() && regex.test(root)) {
|
|
241
|
+
files.push(root)
|
|
242
|
+
if (exitByFirst) break
|
|
243
|
+
}
|
|
244
|
+
if (stat.isDirectory()) {
|
|
245
|
+
const dirents = fs__default.default
|
|
246
|
+
.readdirSync(root)
|
|
247
|
+
.map((o) => path__default.default.resolve(root, o))
|
|
248
|
+
files.push(...loadDirFiles(dirents, name))
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return files
|
|
252
|
+
}
|
|
253
|
+
function pathJoin(...paths) {
|
|
254
|
+
return paths.join('/').split('/').filter(Boolean).join('/')
|
|
255
|
+
}
|
|
256
|
+
function deepMerge(target, ...sources) {
|
|
257
|
+
if (typeof target !== 'object' || target === null) target = {}
|
|
258
|
+
if (!sources.length) return target
|
|
259
|
+
for (const source of sources) {
|
|
260
|
+
if (typeof source !== 'object' || source === null) continue
|
|
261
|
+
if (lodashEs.isArray(target) && lodashEs.isArray(source)) {
|
|
262
|
+
const set = /* @__PURE__ */ new Set([...target, ...source])
|
|
263
|
+
return [...set]
|
|
264
|
+
} else if (
|
|
265
|
+
lodashEs.isPlainObject(target) &&
|
|
266
|
+
lodashEs.isPlainObject(source)
|
|
267
|
+
) {
|
|
268
|
+
for (const key in source) {
|
|
269
|
+
if (
|
|
270
|
+
key in target &&
|
|
271
|
+
typeof target[key] === 'object' &&
|
|
272
|
+
typeof source[key] === 'object'
|
|
273
|
+
) {
|
|
274
|
+
target[key] = deepMerge(target[key], source[key])
|
|
275
|
+
} else {
|
|
276
|
+
target[key] = source[key] || target[key]
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return target
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
exports.builder = builder
|
|
285
|
+
exports.builderWithWatch = builderWithWatch
|
|
286
|
+
exports.defineMiniRouter = defineMiniRouter
|
|
287
|
+
exports.defineMiniTabbar = defineMiniTabbar
|