-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.js
96 lines (79 loc) · 2.9 KB
/
generate.js
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
const toExportName = (name) => `${name.charAt(0).toUpperCase()}${name.slice(1)}`
const isFirstUpperCase = (name) => /[A-Z]/.test(name.charAt(0))
// for mixed-content Directory or upper-case-named File
// merge export:
// import * as Aaa from './aaa/index.js'
// import * as Bbb from './Bbb.js'
// export { Aaa, Bbb }
//
// for lower-cased-named File
// hoist export:
// export { c1, c2 } from './ccc.js'
// export { d1, d2 } from './ddd.js'
const generateIndexScript = ({ sourceRouteMap }) => {
const indexScriptMap = {
// [ indexScriptPath ]: 'code'
}
Object.values(sourceRouteMap).forEach(({ routeList, directoryList, fileList }) => {
const textList = []
const importList = []
directoryList.forEach((name) => { // merge: mixed-content Directory
const exportName = toExportName(name)
textList.push(`import * as ${exportName} from './${name}/index.js'`)
importList.push(exportName)
})
fileList.forEach(({ name, exportList }) => {
if (
directoryList.length || // merge: File in mixed-content Directory
isFirstUpperCase(name) // merge: upper-case-named File
) {
const exportName = toExportName(name)
textList.push(`import * as ${exportName} from './${name}.js'`)
importList.push(exportName)
} else { // hoist: lower-cased-named File
textList.push(`export { ${exportList.join(', ')} } from './${name}.js'`)
}
})
importList.length && textList.push(`export { ${importList.join(', ')} }`)
indexScriptMap[ [ ...routeList, 'index.js' ].join('/') ] = textList.join('\n')
})
return indexScriptMap
}
const HOIST_LIST_KEY = '~hoist'
const EXPORT_LIST_KEY = '~export'
const EXPORT_HOIST_LIST_KEY = '~export-hoist'
const generateExportInfo = ({ sourceRouteMap }) => {
const exportInfoMap = {}
const getExportInfo = (...routeList) => {
const key = routeList.join('/')
if (!exportInfoMap[ key ]) exportInfoMap[ key ] = {}
return exportInfoMap[ key ]
}
Object.values(sourceRouteMap).forEach(({ routeList, directoryList, fileList }) => {
const exportInfo = getExportInfo(...routeList)
directoryList.forEach((name) => {
exportInfo[ toExportName(name) ] = getExportInfo(...routeList, name)
})
fileList.forEach(({ name, exportList }) => {
const shouldMergeExport = directoryList.length || isFirstUpperCase(name)
if (shouldMergeExport) {
exportInfo[ toExportName(name) ] = { [ EXPORT_LIST_KEY ]: exportList }
} else {
exportInfo[ name ] = { [ HOIST_LIST_KEY ]: exportList }
exportInfo[ EXPORT_HOIST_LIST_KEY ] = [
...(exportInfo[ EXPORT_HOIST_LIST_KEY ] || []),
...exportList
]
}
getExportInfo(...routeList, name)[ EXPORT_LIST_KEY ] = exportList
})
})
return exportInfoMap
}
export {
generateIndexScript,
HOIST_LIST_KEY,
EXPORT_LIST_KEY,
EXPORT_HOIST_LIST_KEY,
generateExportInfo
}