-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (104 loc) · 3.68 KB
/
index.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
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
const { createHash } = require('node:crypto')
const { debug } = require('./util.js')
const { relative } = require('node:path')
const glob = require('fast-glob')
const name = 'wildcard-imports'
module.exports = function ({ ignore = [] } = {}) {
return {
name,
setup(build) {
const { bundle, format = 'cjs', platform } = build.initialOptions
const isCjs = format === 'cjs'
debug({ bundle, isCjs, format })
build.onResolve(
{ filter: /[?+*{}[\]()]/ },
({ importer, kind, path, resolveDir }) => {
if (platform !== 'node') {
return
}
debug({ kind })
switch (kind) {
case 'dynamic-import':
case 'import-statement':
case 'require-call':
return {
namespace: name,
path,
pluginData: {
importer,
kind,
resolveDir
}
}
}
}
)
build.onLoad(
{ filter: /.*/, namespace: name },
async ({ path, pluginData: { importer, kind, resolveDir } }) => {
const allFiles = await glob(path, {
absolute: true,
cwd: resolveDir,
ignore: ['**/node_modules', ...ignore]
})
const targetFiles = allFiles
.filter((path) => path !== importer)
.map((path) => relative(resolveDir, path))
const exports = {}
const esExports = {}
const contents = [
'',
targetFiles.map((path) => {
const key = createHash('md5').update(path).digest('hex')
const alias = `_${key}`
exports[`./${path}`] = alias
esExports[alias] = `./${path}`
return [
kind === 'dynamic-import' &&
`const ${alias} = ['./${path}', async () => ${isCjs ? 'require' : 'await import'}('./${path}')]`,
((kind === 'import-statement' && !isCjs) ||
(kind === 'require-call' && !isCjs)) &&
`const ${alias} = async () => import('./${path}');`,
((kind === 'import-statement' && isCjs) ||
(kind === 'require-call' && isCjs)) &&
`const ${alias} = async () => require('./${path}');`
]
}),
'',
(() => {
const stringified = JSON.stringify(exports, null, 2)
const fragment = stringified.replace(
/"(_[0-9a-f]+)"/g,
(_, alias) => alias
)
const esStringified = JSON.stringify(esExports, null, 2)
const esFragment = esStringified.replace(
/"(_[0-9a-f]+)": ("[^"]+")/g,
(_, alias, path) => `${alias} as ${path}`
)
return [
kind === 'dynamic-import' &&
`${isCjs ? 'module.exports =' : 'export default'} Object.fromEntries([${Object.values(exports).join(', ')}])`,
((kind === 'import-statement' && !isCjs) ||
(kind === 'require-call' && !isCjs)) &&
`export ${esFragment};`,
((kind === 'import-statement' && isCjs) ||
(kind === 'require-call' && isCjs)) &&
`module.exports = ${fragment};`
]
})(),
''
]
.flat(Number.MAX_SAFE_INTEGER)
.filter((line) => typeof line === 'string')
.join('\n')
debug({ importer, contents })
return {
contents,
resolveDir
}
}
)
}
}
}