Skip to content

Commit 3ec82ab

Browse files
committed
refactor: parse it
1 parent 074c755 commit 3ec82ab

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

packages/plugin-rsc/src/plugin.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -909,13 +909,10 @@ function scanBuildStripPlugin(): Plugin {
909909
name: 'rsc:scan-strip',
910910
apply: 'build',
911911
enforce: 'post',
912-
transform(code, _id, _options) {
912+
async transform(code, _id, _options) {
913913
if (!isScanBuild) return
914-
915-
const output = transformScanBuildStrip(code)
916-
if (output) {
917-
return { code: output, map: { mappings: '' } }
918-
}
914+
const output = await transformScanBuildStrip(code)
915+
return { code: output, map: { mappings: '' } }
919916
},
920917
}
921918
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { transformScanBuildStrip } from './scan'
3+
4+
describe(transformScanBuildStrip, () => {
5+
it('basic', async () => {
6+
const input = `\
7+
import { a } from "a";
8+
import "b";
9+
import(String("c"))
10+
import.meta.glob("d", {
11+
query: "?e",
12+
})
13+
import.meta.globee("d", { query: "?e" })
14+
export default "foo";
15+
`
16+
expect(await transformScanBuildStrip(input)).toMatchInlineSnapshot(`
17+
"import "a";
18+
import "b";
19+
import.meta.glob("d", {
20+
query: "?e",
21+
})
22+
"
23+
`)
24+
})
25+
})
Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,40 @@
11
import * as esModuleLexer from 'es-module-lexer'
2+
import { parseAstAsync } from 'vite'
3+
import { walk } from 'estree-walker'
24

35
// https://github.com/vitejs/vite/blob/86d2e8be50be535494734f9f5f5236c61626b308/packages/vite/src/node/plugins/importMetaGlob.ts#L113
46
const importGlobRE = /\bimport\.meta\.glob(?:<\w+>)?\s*\(/g
57

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> {
119
const [imports] = esModuleLexer.parse(code)
1210
let output = imports
1311
.map((e) => e.n && `import ${JSON.stringify(e.n)};\n`)
1412
.filter(Boolean)
1513
.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+
}
1738

1839
return output
1940
}

0 commit comments

Comments
 (0)