|
1 | 1 | import { createHotContext } from './client' |
2 | 2 |
|
3 | | -class DevRuntime { |
4 | | - modules: Record<string, { exports: any }> = {} |
| 3 | +declare const __FULL_BUNDLE_MODE__: boolean |
5 | 4 |
|
6 | | - static getInstance() { |
7 | | - // @ts-expect-error __rolldown_runtime__ |
8 | | - let instance = globalThis.__rolldown_runtime__ |
9 | | - if (!instance) { |
10 | | - instance = new DevRuntime() |
| 5 | +if (__FULL_BUNDLE_MODE__) { |
| 6 | + class DevRuntime { |
| 7 | + modules: Record<string, { exports: any }> = {} |
| 8 | + |
| 9 | + static getInstance() { |
11 | 10 | // @ts-expect-error __rolldown_runtime__ |
12 | | - globalThis.__rolldown_runtime__ = instance |
| 11 | + let instance = globalThis.__rolldown_runtime__ |
| 12 | + if (!instance) { |
| 13 | + instance = new DevRuntime() |
| 14 | + // @ts-expect-error __rolldown_runtime__ |
| 15 | + globalThis.__rolldown_runtime__ = instance |
| 16 | + } |
| 17 | + return instance |
13 | 18 | } |
14 | | - return instance |
15 | | - } |
16 | 19 |
|
17 | | - createModuleHotContext(moduleId: string) { |
18 | | - return createHotContext(moduleId) |
19 | | - } |
| 20 | + createModuleHotContext(moduleId: string) { |
| 21 | + return createHotContext(moduleId) |
| 22 | + } |
20 | 23 |
|
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 | | - } |
| 24 | + applyUpdates(_boundaries: string[]) { |
| 25 | + // |
| 26 | + } |
38 | 27 |
|
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) { |
50 | | - Object.defineProperty(exports, key, { |
51 | | - enumerable: true, |
52 | | - get: () => esmExportGettersOrCjsExports[key], |
53 | | - }) |
54 | | - } else { |
55 | | - Object.defineProperty(exports, key, { |
56 | | - enumerable: true, |
57 | | - get: esmExportGettersOrCjsExports[key], |
58 | | - }) |
| 28 | + registerModule( |
| 29 | + id: string, |
| 30 | + esmExportGettersOrCjsExports: Record<string, () => any>, |
| 31 | + meta: { cjs?: boolean } = {}, |
| 32 | + ) { |
| 33 | + const exports = {} |
| 34 | + Object.keys(esmExportGettersOrCjsExports).forEach((key) => { |
| 35 | + if ( |
| 36 | + Object.prototype.hasOwnProperty.call( |
| 37 | + esmExportGettersOrCjsExports, |
| 38 | + key, |
| 39 | + ) |
| 40 | + ) { |
| 41 | + if (meta.cjs) { |
| 42 | + Object.defineProperty(exports, key, { |
| 43 | + enumerable: true, |
| 44 | + get: () => esmExportGettersOrCjsExports[key], |
| 45 | + }) |
| 46 | + } else { |
| 47 | + Object.defineProperty(exports, key, { |
| 48 | + enumerable: true, |
| 49 | + get: esmExportGettersOrCjsExports[key], |
| 50 | + }) |
| 51 | + } |
| 52 | + } |
| 53 | + }) |
| 54 | + if (this.modules[id]) { |
| 55 | + this.modules[id] = { |
| 56 | + exports, |
| 57 | + } |
| 58 | + } else { |
| 59 | + // If the module is not in the cache, we need to register it. |
| 60 | + this.modules[id] = { |
| 61 | + exports, |
59 | 62 | } |
60 | | - } |
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, |
70 | 63 | } |
71 | 64 | } |
72 | | - } |
73 | 65 |
|
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 {} |
| 66 | + loadExports(id: string) { |
| 67 | + const module = this.modules[id] |
| 68 | + if (module) { |
| 69 | + return module.exports |
| 70 | + } else { |
| 71 | + console.warn(`Module ${id} not found`) |
| 72 | + return {} |
| 73 | + } |
81 | 74 | } |
| 75 | + |
| 76 | + // __esmMin |
| 77 | + // createEsmInitializer = (fn, res) => () => (fn && (res = fn(fn = 0)), res) |
| 78 | + // __commonJSMin |
| 79 | + // createCjsInitializer = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports) |
82 | 80 | } |
83 | 81 |
|
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) |
| 82 | + DevRuntime.getInstance() |
88 | 83 | } |
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