@@ -6,29 +6,30 @@ import { parseAst } from 'rollup/parseAst';
66
77import { createBackendModuleGraphCollector } from './backend-module-graph-collector' ;
88
9- type FakeModuleInfo = { id : string ; code ? : string | null } ;
9+ type FakeModuleInfo = { id : string ; code : string | null } ;
1010
1111/**
12- * Invokes the hook with a plugin context exposing `parse`, which is where it gets
13- * its parser. `rollup/parseAst` is what Rollup's real context uses.
12+ * Calls the `moduleParsed` hook with a plugin context exposing `parse`, which is
13+ * where it takes its parser from. `rollup/parseAst` is what Rollup's real
14+ * context supplies. The hook reads only the few `ModuleInfo` fields these fakes
15+ * model, so building a complete one would be noise.
1416 */
17+ const getModuleParsedHook = ( collector : ReturnType < typeof createBackendModuleGraphCollector > ) => {
18+ const hook = collector . plugin . moduleParsed ;
19+ if ( typeof hook !== 'function' ) {
20+ throw new Error ( 'Expected "moduleParsed" to be a function hook.' ) ;
21+ }
22+
23+ return ( moduleInfo : object ) => Reflect . apply ( hook , { parse : parseAst } , [ moduleInfo ] ) ;
24+ } ;
25+
1526const getEmit = ( collector : ReturnType < typeof createBackendModuleGraphCollector > ) => {
16- const moduleParsed = collector . plugin . moduleParsed as (
17- this : { parse : typeof parseAst } ,
18- moduleInfo : unknown ,
19- ) => void ;
27+ const callHook = getModuleParsedHook ( collector ) ;
2028
21- // Fills the remaining fields in place rather than spreading into a new
22- // object: a spread would drop (or trigger) an `ast` getter, and one case
23- // below depends on that getter surviving intact.
24- return ( moduleInfo : FakeModuleInfo , importedIds : string [ ] = [ ] ) =>
25- moduleParsed . call (
26- { parse : parseAst } ,
27- Object . assign ( moduleInfo , {
28- importedIds,
29- importedIdResolutions : importedIds . map ( ( id ) => ( { id } ) ) ,
30- } ) ,
31- ) ;
29+ return ( moduleInfo : FakeModuleInfo , importedIds : string [ ] = [ ] ) => {
30+ const importedIdResolutions = importedIds . map ( ( id ) => ( { id } ) ) ;
31+ callHook ( { ...moduleInfo , importedIds, importedIdResolutions } ) ;
32+ } ;
3233} ;
3334
3435describe ( 'Backend Functions - backend module graph collector' , ( ) => {
@@ -70,23 +71,28 @@ describe('Backend Functions - backend module graph collector', () => {
7071
7172 test ( 'Should collect records under a bundler that does not support ModuleInfo#ast' , ( ) => {
7273 const collector = createBackendModuleGraphCollector ( '/project' ) ;
73- const emit = getEmit ( collector ) ;
74+ const callHook = getModuleParsedHook ( collector ) ;
7475
75- // Rolldown, the bundler Vite 8 uses by default, keeps `ast` on its
76- // Rollup-compat object but stubs the getter to throw. Reading the
77- // property at all is the failure, so it must throw rather than be absent.
78- const moduleInfo : FakeModuleInfo = Object . defineProperty (
79- { id : '/project/src/backend/actions.backend.ts' , code : 'export const id = "conn-1";' } ,
80- 'ast' ,
81- {
82- get ( ) {
83- throw new Error ( 'UNSUPPORTED: ModuleInfo#ast' ) ;
84- } ,
85- enumerable : true ,
76+ // Rolldown, Vite 8's default bundler, keeps `ast` on its Rollup-compat
77+ // object but stubs the getter to throw. Reading the property at all is
78+ // the failure, so it has to throw rather than be absent — which is also
79+ // why this is assembled in place instead of going through `getEmit`,
80+ // whose spread would trigger the getter during setup.
81+ const moduleInfo = {
82+ id : '/project/src/backend/actions.backend.ts' ,
83+ code : 'export const id = "conn-1";' ,
84+ importedIds : [ ] ,
85+ importedIdResolutions : [ ] ,
86+ } ;
87+ Object . defineProperty ( moduleInfo , 'ast' , {
88+ get ( ) {
89+ throw new Error ( 'UNSUPPORTED: ModuleInfo#ast' ) ;
8690 } ,
87- ) ;
91+ enumerable : true ,
92+ } ) ;
93+
94+ callHook ( moduleInfo ) ;
8895
89- expect ( ( ) => emit ( moduleInfo ) ) . not . toThrow ( ) ;
9096 expect ( [ ...collector . getModuleRecords ( ) . keys ( ) ] ) . toEqual ( [
9197 '/project/src/backend/actions.backend.ts' ,
9298 ] ) ;
0 commit comments