-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy patheasycom.ts
379 lines (351 loc) · 10.5 KB
/
easycom.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import fs from 'fs'
import path from 'path'
import debug from 'debug'
import { camelize, capitalize, extend } from '@vue/shared'
import { createFilter } from '@rollup/pluginutils'
import { once } from '@dcloudio/uni-shared'
import { normalizePath } from './utils'
import { parsePagesJson, parsePagesJsonOnce } from './json/pages'
import { M } from './messages'
import { initUTSComponents } from './uts'
import { genUTSClassName } from './utsUtils'
interface EasycomOption {
isX?: boolean
dirs?: string[]
rootDir: string
extensions: string[]
autoscan?: boolean
custom?: EasycomCustom
}
export interface EasycomMatcher {
name: string
pattern: RegExp
replacement: string
}
interface EasycomCustom {
[key: string]: string
}
const debugEasycom = debug('uni:easycom')
const easycoms: EasycomMatcher[] = []
const easycomsCache = new Map<string, string>()
const easycomsInvalidCache = new Set<string>()
let hasEasycom = false
function clearEasycom() {
easycoms.length = 0
easycomsCache.clear()
easycomsInvalidCache.clear()
}
export function initEasycoms(
inputDir: string,
{
dirs,
platform,
isX,
}: { dirs: string[]; platform: UniApp.PLATFORM; isX?: boolean }
) {
const componentsDir = path.resolve(inputDir, 'components')
const uniModulesDir = path.resolve(inputDir, 'uni_modules')
const initEasycomOptions = (pagesJson?: UniApp.PagesJson) => {
// 初始化时,从once中读取缓存,refresh时,实时读取
const { easycom } = pagesJson || parsePagesJson(inputDir, platform, false)
const easycomOptions: EasycomOption = {
isX,
dirs:
easycom && easycom.autoscan === false
? [...dirs] // 禁止自动扫描
: [
...dirs,
componentsDir,
...initUniModulesEasycomDirs(uniModulesDir),
],
rootDir: inputDir,
autoscan: !!(easycom && easycom.autoscan),
custom: (easycom && easycom.custom) || {},
extensions: [...(isX ? ['.uvue'] : []), ...['.vue', '.jsx', '.tsx']],
}
debugEasycom(easycomOptions)
return easycomOptions
}
const options = initEasycomOptions(parsePagesJsonOnce(inputDir, platform))
const initUTSEasycom = () => {
initUTSComponents(inputDir, platform).forEach((item) => {
const index = easycoms.findIndex((easycom) => item.name === easycom.name)
if (index > -1) {
easycoms.splice(index, 1, item)
} else {
easycoms.push(item)
}
})
if (isX && (globalThis as any).uts2jsSourceCodeMap) {
;(globalThis as any).uts2jsSourceCodeMap.initUts2jsEasycom(easycoms)
}
}
initEasycom(options)
initUTSEasycom()
const componentExtNames = isX ? 'uvue|vue' : 'vue'
const res = {
options,
filter: createFilter(
[
'components/*/*.(' + componentExtNames + '|jsx|tsx)',
'uni_modules/*/components/*/*.(' + componentExtNames + '|jsx|tsx)',
'utssdk/*/**/*.(' + componentExtNames + ')',
'uni_modules/*/utssdk/*/*.(' + componentExtNames + ')',
],
[],
{
resolve: inputDir,
}
),
refresh() {
res.options = initEasycomOptions()
initEasycom(res.options)
initUTSEasycom()
},
easycoms,
}
return res
}
export const initEasycomsOnce = once(initEasycoms)
function initUniModulesEasycomDirs(uniModulesDir: string) {
if (!fs.existsSync(uniModulesDir)) {
return []
}
return fs
.readdirSync(uniModulesDir)
.map((uniModuleDir) => {
const uniModuleComponentsDir = path.resolve(
uniModulesDir,
uniModuleDir,
'components'
)
if (fs.existsSync(uniModuleComponentsDir)) {
return uniModuleComponentsDir
}
})
.filter<string>(Boolean as any)
}
function initEasycom({
isX,
dirs,
rootDir,
custom,
extensions,
}: EasycomOption) {
clearEasycom()
rootDir = normalizePath(rootDir)
const easycomsObj = Object.create(null)
if (dirs && dirs.length && rootDir) {
const autoEasyComObj = initAutoScanEasycoms(dirs, rootDir, extensions)
if (isX) {
Object.keys(autoEasyComObj).forEach((tagName) => {
let source = autoEasyComObj[tagName]
tagName = tagName.slice(1, -1)
if (path.isAbsolute(source) && source.startsWith(rootDir)) {
source = '@/' + normalizePath(path.relative(rootDir, source))
}
addUTSEasyComAutoImports(source, [
genUTSComponentPublicInstanceImported(rootDir, source),
genUTSComponentPublicInstanceIdent(tagName),
])
})
}
extend(easycomsObj, autoEasyComObj)
}
if (custom) {
Object.keys(custom).forEach((name) => {
const componentPath = custom[name]
easycomsObj[name] = componentPath.startsWith('@/')
? normalizePath(path.join(rootDir!, componentPath.slice(2)))
: componentPath
})
}
Object.keys(easycomsObj).forEach((name) => {
easycoms.push({
name:
name.startsWith('^') && name.endsWith('$') ? name.slice(1, -1) : name,
pattern: new RegExp(name),
replacement: easycomsObj[name],
})
})
debugEasycom(easycoms)
hasEasycom = !!easycoms.length
return easycoms
}
export function matchEasycom(tag: string) {
if (!hasEasycom) {
return
}
let source = easycomsCache.get(tag)
if (source) {
return source
}
if (easycomsInvalidCache.has(tag)) {
return false
}
const matcher = easycoms.find((matcher) => matcher.pattern.test(tag))
if (!matcher) {
easycomsInvalidCache.add(tag)
return false
}
source = tag.replace(matcher.pattern, matcher.replacement)
easycomsCache.set(tag, source)
debugEasycom('matchEasycom', tag, source)
return source
}
const isDir = (path: string) => fs.lstatSync(path).isDirectory()
function initAutoScanEasycom(
dir: string,
rootDir: string,
extensions: string[]
): Record<string, string> {
if (!path.isAbsolute(dir)) {
dir = path.resolve(rootDir, dir)
}
const easycoms = Object.create(null)
if (!fs.existsSync(dir)) {
return easycoms
}
const is_uni_modules =
path.basename(path.resolve(dir, '../..')) === 'uni_modules'
const is_encrypt_uni_modules =
is_uni_modules && fs.existsSync(path.resolve(dir, '../encrypt'))
const uni_modules_plugin_id =
is_encrypt_uni_modules && path.basename(path.resolve(dir, '..'))
fs.readdirSync(dir).forEach((name) => {
const folder = path.resolve(dir, name)
if (!isDir(folder)) {
return
}
const importDir = normalizePath(folder)
const files = fs.readdirSync(folder)
// 读取文件夹文件列表,比对文件名(fs.existsSync在大小写不敏感的系统会匹配不准确)
for (let i = 0; i < extensions.length; i++) {
const ext = extensions[i]
if (files.includes(name + ext)) {
easycoms[`^${name}$`] = is_encrypt_uni_modules
? normalizePath(
path.join(
rootDir,
`uni_modules/${uni_modules_plugin_id}?${
// android 走 proxy
process.env.UNI_APP_X === 'true' &&
process.env.UNI_UTS_PLATFORM === 'app-android'
? 'uts-proxy'
: 'uni_helpers'
}`
)
)
: `${importDir}/${name}${ext}`
break
}
}
})
return easycoms
}
function initAutoScanEasycoms(
dirs: string[],
rootDir: string,
extensions: string[]
) {
const conflict: Record<string, string[]> = {}
const res = dirs.reduce<Record<string, string>>(
(easycoms: Record<string, string>, dir: string) => {
const curEasycoms = initAutoScanEasycom(dir, rootDir, extensions)
Object.keys(curEasycoms).forEach((name) => {
// Use the first component when name conflict
const componentPath = easycoms[name]
if (!componentPath) {
easycoms[name] = curEasycoms[name]
} else {
;(conflict[componentPath] || (conflict[componentPath] = [])).push(
normalizeComponentPath(curEasycoms[name], rootDir)
)
}
})
return easycoms
},
Object.create(null)
)
const conflictComponents = Object.keys(conflict)
if (conflictComponents.length) {
console.warn(M['easycom.conflict'])
conflictComponents.forEach((com) => {
console.warn(
[normalizeComponentPath(com, rootDir), conflict[com]].join(',')
)
})
}
return res
}
function normalizeComponentPath(componentPath: string, rootDir: string) {
return normalizePath(path.relative(rootDir, componentPath))
}
export function addImportDeclaration(
importDeclarations: string[],
local: string,
source: string,
imported?: string
) {
importDeclarations.push(createImportDeclaration(local, source, imported))
return local
}
function createImportDeclaration(
local: string,
source: string,
imported?: string
) {
if (imported) {
return `import { ${imported} as ${local} } from '${source}';`
}
return `import ${local} from '${source}';`
}
const RESOLVE_EASYCOM_IMPORT_CODE = `import { resolveDynamicComponent as __resolveDynamicComponent } from 'vue';import { resolveEasycom } from '@dcloudio/uni-app';`
export function genResolveEasycomCode(
importDeclarations: string[],
code: string,
name: string
) {
if (!importDeclarations.includes(RESOLVE_EASYCOM_IMPORT_CODE)) {
importDeclarations.push(RESOLVE_EASYCOM_IMPORT_CODE)
}
return `resolveEasycom(${code.replace(
'_resolveComponent',
'__resolveDynamicComponent'
)}, ${name})`
}
export const UNI_EASYCOM_EXCLUDE = [/@dcloudio\/uni-h5/]
const utsEasyComAutoImports: Record<string, [[string, string]]> = {}
export function getUTSEasyComAutoImports() {
return utsEasyComAutoImports
}
export function addUTSEasyComAutoImports(
source: string,
imports: [string, string]
) {
if (!utsEasyComAutoImports[source]) {
utsEasyComAutoImports[source] = [imports]
} else {
if (!utsEasyComAutoImports[source].find((item) => item[0] === imports[0])) {
utsEasyComAutoImports[source].push(imports)
}
}
}
export function genUTSComponentPublicInstanceIdent(tagName: string) {
return capitalize(camelize(tagName)) + 'ComponentPublicInstance'
}
export function genUTSComponentPublicInstanceImported(
root: string,
fileName: string
) {
root = normalizePath(root)
if (path.isAbsolute(fileName) && fileName.startsWith(root)) {
fileName = normalizePath(path.relative(root, fileName))
}
if (fileName.startsWith('@/')) {
return (
genUTSClassName(fileName.replace('@/', '')) + 'ComponentPublicInstance'
)
}
return genUTSClassName(fileName) + 'ComponentPublicInstance'
}