File tree Expand file tree Collapse file tree 7 files changed +113
-7
lines changed Expand file tree Collapse file tree 7 files changed +113
-7
lines changed Original file line number Diff line number Diff line change
1
+ export default function Dep ( ) {
2
+ return < > test-import-meta-glob</ >
3
+ }
Original file line number Diff line number Diff line change
1
+ export async function TestImportMetaGlob ( ) {
2
+ const mod : any = await Object . values ( import . meta. glob ( './dep.tsx' ) ) [ 0 ] ( )
3
+ return < mod . default />
4
+ }
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ import { TestHmrSharedServer } from './hmr-shared/server'
36
36
import { TestHmrSharedClient } from './hmr-shared/client'
37
37
import { TestHmrSharedAtomic } from './hmr-shared/atomic/server'
38
38
import { TestCssQueries } from './css-queries/server'
39
+ import { TestImportMetaGlob } from './import-meta-glob/server'
39
40
40
41
export function Root ( props : { url : URL } ) {
41
42
return (
@@ -85,6 +86,7 @@ export function Root(props: { url: URL }) {
85
86
< TestUseCache />
86
87
< TestReactCache url = { props . url } />
87
88
< TestCssQueries />
89
+ < TestImportMetaGlob />
88
90
</ body >
89
91
</ html >
90
92
)
Original file line number Diff line number Diff line change @@ -131,6 +131,7 @@ export default { fetch: handler };
131
131
}
132
132
} ,
133
133
} ,
134
+ testScanPlugin ( ) ,
134
135
] ,
135
136
build : {
136
137
minify : false ,
@@ -158,6 +159,38 @@ export default { fetch: handler };
158
159
} ,
159
160
} ) as any
160
161
162
+ function testScanPlugin ( ) : Plugin [ ] {
163
+ const moduleIds : { name : string ; ids : string [ ] } [ ] = [ ]
164
+ return [
165
+ {
166
+ name : 'test-scan' ,
167
+ apply : 'build' ,
168
+ buildEnd ( ) {
169
+ moduleIds . push ( {
170
+ name : this . environment . name ,
171
+ ids : [ ...this . getModuleIds ( ) ] ,
172
+ } )
173
+ } ,
174
+ buildApp : {
175
+ order : 'post' ,
176
+ async handler ( ) {
177
+ // client scan build discovers additional modules for server references.
178
+ const [ m1 , m2 ] = moduleIds . filter ( ( m ) => m . name === 'rsc' )
179
+ const diff = m2 . ids . filter ( ( id ) => ! m1 . ids . includes ( id ) )
180
+ assert ( diff . length > 0 )
181
+
182
+ // but make sure it's not due to import.meta.glob
183
+ // https://github.com/vitejs/rolldown-vite/issues/373
184
+ assert . equal (
185
+ diff . find ( ( id ) => id . includes ( 'import-meta-glob/dep.tsx' ) ) ,
186
+ undefined ,
187
+ )
188
+ } ,
189
+ } ,
190
+ } ,
191
+ ]
192
+ }
193
+
161
194
function vitePluginUseCache ( ) : Plugin [ ] {
162
195
return [
163
196
{
Original file line number Diff line number Diff line change @@ -40,6 +40,7 @@ import {
40
40
import { cjsModuleRunnerPlugin } from './plugins/cjs'
41
41
import { evalValue , parseIdQuery } from './plugins/utils'
42
42
import { createDebug } from '@hiogawa/utils'
43
+ import { transformScanBuildStrip } from './plugins/scan'
43
44
44
45
// state for build orchestration
45
46
let serverReferences : Record < string , string > = { }
@@ -901,19 +902,16 @@ globalThis.AsyncLocalStorage = __viteRscAyncHooks.AsyncLocalStorage;
901
902
]
902
903
}
903
904
905
+ // During scan build, we strip all code but imports to
906
+ // traverse module graph faster and just discover client/server references.
904
907
function scanBuildStripPlugin ( ) : Plugin {
905
908
return {
906
909
name : 'rsc:scan-strip' ,
907
910
apply : 'build' ,
908
911
enforce : 'post' ,
909
- transform ( code , _id , _options ) {
912
+ async transform ( code , _id , _options ) {
910
913
if ( ! isScanBuild ) return
911
- // During server scan, we strip all code but imports to only discover client/server references.
912
- const [ imports ] = esModuleLexer . parse ( code )
913
- const output = imports
914
- . map ( ( e ) => e . n && `import ${ JSON . stringify ( e . n ) } ;\n` )
915
- . filter ( Boolean )
916
- . join ( '' )
914
+ const output = await transformScanBuildStrip ( code )
917
915
return { code : output , map : { mappings : '' } }
918
916
} ,
919
917
}
Original file line number Diff line number Diff line change
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
+ console.log(import.meta.glob("d", {
20
+ query: "?e",
21
+ }));
22
+ "
23
+ ` )
24
+ } )
25
+ } )
Original file line number Diff line number Diff line change
1
+ import * as esModuleLexer from 'es-module-lexer'
2
+ import { parseAstAsync } from 'vite'
3
+ import { walk } from 'estree-walker'
4
+
5
+ // https://github.com/vitejs/vite/blob/86d2e8be50be535494734f9f5f5236c61626b308/packages/vite/src/node/plugins/importMetaGlob.ts#L113
6
+ const importGlobRE = / \b i m p o r t \. m e t a \. g l o b (?: < \w + > ) ? \s * \( / g
7
+
8
+ export async function transformScanBuildStrip ( code : string ) : Promise < string > {
9
+ const [ imports ] = esModuleLexer . parse ( code )
10
+ let output = imports
11
+ . map ( ( e ) => e . n && `import ${ JSON . stringify ( e . n ) } ;\n` )
12
+ . filter ( Boolean )
13
+ . join ( '' )
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
+ const importMetaGlob = code . slice ( node . start , node . end )
33
+ output += `console.log(${ importMetaGlob } );\n`
34
+ }
35
+ } ,
36
+ } )
37
+ output += ''
38
+ }
39
+
40
+ return output
41
+ }
You can’t perform that action at this time.
0 commit comments