11import { createHotContext } from './client'
22
33class DevRuntime {
4- modules : Record < string , { exports : any } > = { }
4+ modules : Record < string , { exports : any } > = { }
55
6- static getInstance ( ) {
6+ static getInstance ( ) {
7+ // @ts -expect-error __rolldown_runtime__
8+ let instance = globalThis . __rolldown_runtime__
9+ if ( ! instance ) {
10+ instance = new DevRuntime ( )
711 // @ts -expect-error __rolldown_runtime__
8- let instance = globalThis . __rolldown_runtime__ ;
9- if ( ! instance ) {
10- instance = new DevRuntime ( ) ;
11- // @ts -expect-error __rolldown_runtime__
12- globalThis . __rolldown_runtime__ = instance ;
13- }
14- return instance
12+ globalThis . __rolldown_runtime__ = instance
1513 }
14+ return instance
15+ }
1616
17- createModuleHotContext ( moduleId : string ) {
18- return createHotContext ( moduleId ) ;
19- }
17+ createModuleHotContext ( moduleId : string ) {
18+ return createHotContext ( moduleId )
19+ }
2020
21- applyUpdates ( _boundaries : string [ ] ) {
22- // trigger callbacks of accept() correctly
23- // for (const moduleId of boundaries) {
24- // const hotContext = this.moduleHotContexts.get(moduleId);
25- // if (hotContext) {
26- // const acceptCallbacks = hotContext.acceptCallbacks;
27- // acceptCallbacks.filter((cb) => {
28- // cb.fn(this.modules[moduleId].exports);
29- // })
30- // }
31- // }
32- // this.moduleHotContextsToBeUpdated.forEach((hotContext, moduleId) => {
33- // this.moduleHotContexts[moduleId] = hotContext;
34- // })
35- // this.moduleHotContextsToBeUpdated.clear()
36- // swap new contexts
37- }
21+ applyUpdates ( _boundaries : string [ ] ) {
22+ // trigger callbacks of accept() correctly
23+ // for (const moduleId of boundaries) {
24+ // const hotContext = this.moduleHotContexts.get(moduleId);
25+ // if (hotContext) {
26+ // const acceptCallbacks = hotContext.acceptCallbacks;
27+ // acceptCallbacks.filter((cb) => {
28+ // cb.fn(this.modules[moduleId].exports);
29+ // })
30+ // }
31+ // }
32+ // this.moduleHotContextsToBeUpdated.forEach((hotContext, moduleId) => {
33+ // this.moduleHotContexts[moduleId] = hotContext;
34+ // })
35+ // this.moduleHotContextsToBeUpdated.clear()
36+ // swap new contexts
37+ }
3838
39- registerModule ( id : string , exportGetters : Record < string , ( ) => any > ) {
40- const exports = { } ;
41- Object . keys ( exportGetters ) . forEach ( ( key ) => {
42- if ( Object . prototype . hasOwnProperty . call ( exportGetters , key ) ) {
39+ registerModule (
40+ id : string ,
41+ esmExportGettersOrCjsExports : Record < string , ( ) => any > ,
42+ meta : { cjs ?: boolean } = { } ,
43+ ) {
44+ const exports = { }
45+ Object . keys ( esmExportGettersOrCjsExports ) . forEach ( ( key ) => {
46+ if (
47+ Object . prototype . hasOwnProperty . call ( esmExportGettersOrCjsExports , key )
48+ ) {
49+ if ( meta . cjs ) {
4350 Object . defineProperty ( exports , key , {
4451 enumerable : true ,
45- get : exportGetters [ key ] ,
46- } ) ;
47- }
48- } )
49- if ( this . modules [ id ] ) {
50- this . modules [ id ] = {
51- exports ,
52+ get : ( ) => esmExportGettersOrCjsExports [ key ] ,
53+ } )
54+ } else {
55+ Object . defineProperty ( exports , key , {
56+ enumerable : true ,
57+ get : esmExportGettersOrCjsExports [ key ] ,
58+ } )
5259 }
53- } else {
54- // If the module is not in the cache, we need to register it.
55- this . modules [ id ] = {
56- exports,
57- } ;
5860 }
59- }
60-
61- loadExports ( id : string ) {
62- const module = this . modules [ id ] ;
63- if ( module ) {
64- return module . exports ;
65- } else {
66- console . warn ( `Module ${ id } not found` ) ;
67- return { } ;
61+ } )
62+ if ( this . modules [ id ] ) {
63+ this . modules [ id ] = {
64+ exports ,
65+ }
66+ } else {
67+ // If the module is not in the cache, we need to register it.
68+ this . modules [ id ] = {
69+ exports ,
6870 }
6971 }
70-
71- // __esmMin
72- // createEsmInitializer = (fn, res) => () => (fn && (res = fn(fn = 0)), res)
73- // __commonJSMin
74- // createCjsInitializer = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports)
7572 }
76-
77- DevRuntime . getInstance ( ) ;
78-
79- // export function loadScript(url: string): void {
80- // const script = document.createElement('script');
81- // script.src = url;
82- // script.type = 'module';
83- // script.onerror = function () {
84- // console.error('Failed to load script: ' + url);
85- // }
86- // document.body.appendChild(script);
87- // }
88-
73+
74+ loadExports ( id : string ) {
75+ const module = this . modules [ id ]
76+ if ( module ) {
77+ return module . exports
78+ } else {
79+ console . warn ( `Module ${ id } not found` )
80+ return { }
81+ }
82+ }
83+
84+ // __esmMin
85+ // createEsmInitializer = (fn, res) => () => (fn && (res = fn(fn = 0)), res)
86+ // __commonJSMin
87+ // createCjsInitializer = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports)
88+ }
89+
90+ DevRuntime . getInstance ( )
91+
92+ // export function loadScript(url: string): void {
93+ // const script = document.createElement('script');
94+ // script.src = url;
95+ // script.type = 'module';
96+ // script.onerror = function () {
97+ // console.error('Failed to load script: ' + url);
98+ // }
99+ // document.body.appendChild(script);
100+ // }
0 commit comments