|
1 | 1 | import * as esModuleLexer from 'es-module-lexer' |
| 2 | +import { parseAstAsync } from 'vite' |
| 3 | +import { walk } from 'estree-walker' |
2 | 4 |
|
3 | 5 | // https://github.com/vitejs/vite/blob/86d2e8be50be535494734f9f5f5236c61626b308/packages/vite/src/node/plugins/importMetaGlob.ts#L113 |
4 | 6 | const importGlobRE = /\bimport\.meta\.glob(?:<\w+>)?\s*\(/g |
5 | 7 |
|
6 | | -export function transformScanBuildStrip(code: string): string | undefined { |
7 | | - // bail out if import.meta.glob |
8 | | - // https://github.com/vitejs/rolldown-vite/issues/373 |
9 | | - if (importGlobRE.test(code)) return |
10 | | - |
| 8 | +export async function transformScanBuildStrip(code: string): Promise<string> { |
11 | 9 | const [imports] = esModuleLexer.parse(code) |
12 | 10 | let output = imports |
13 | 11 | .map((e) => e.n && `import ${JSON.stringify(e.n)};\n`) |
14 | 12 | .filter(Boolean) |
15 | 13 | .join('') |
16 | | - output += 'module.exports = {};\n' |
| 14 | + |
| 15 | + // preserve import.meta.glob for rolldown-vite |
| 16 | + // https://github.com/vitejs/rolldown-vite/issues/373 |
| 17 | + if (importGlobRE.test(code)) { |
| 18 | + const ast = await parseAstAsync(code) |
| 19 | + walk(ast, { |
| 20 | + enter(node) { |
| 21 | + if ( |
| 22 | + node.type === 'CallExpression' && |
| 23 | + node.callee.type === 'MemberExpression' && |
| 24 | + node.callee.object.type === 'MetaProperty' && |
| 25 | + node.callee.object.meta.type === 'Identifier' && |
| 26 | + node.callee.object.meta.name === 'import' && |
| 27 | + node.callee.object.property.type === 'Identifier' && |
| 28 | + node.callee.object.property.name === 'meta' && |
| 29 | + node.callee.property.type === 'Identifier' && |
| 30 | + node.callee.property.name === 'glob' |
| 31 | + ) { |
| 32 | + output += code.slice(node.start, node.end) + '\n' |
| 33 | + } |
| 34 | + }, |
| 35 | + }) |
| 36 | + output += '' |
| 37 | + } |
17 | 38 |
|
18 | 39 | return output |
19 | 40 | } |
0 commit comments