@remix-run/assets 0.4.4 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +322 -56
  2. package/dist/assets.d.ts +2 -1
  3. package/dist/assets.d.ts.map +1 -1
  4. package/dist/assets.js +2 -2
  5. package/dist/lib/access.d.ts +6 -2
  6. package/dist/lib/access.d.ts.map +1 -1
  7. package/dist/lib/access.js +300 -5
  8. package/dist/lib/asset-server.d.ts +118 -5
  9. package/dist/lib/asset-server.d.ts.map +1 -1
  10. package/dist/lib/asset-server.js +260 -24
  11. package/dist/lib/compilation-error.d.ts +1 -1
  12. package/dist/lib/compilation-error.d.ts.map +1 -1
  13. package/dist/lib/file-matcher.js +1 -1
  14. package/dist/lib/files/compiler.d.ts.map +1 -1
  15. package/dist/lib/files/compiler.js +7 -6
  16. package/dist/lib/files/config.js +1 -1
  17. package/dist/lib/hmr.d.ts +37 -0
  18. package/dist/lib/hmr.d.ts.map +1 -0
  19. package/dist/lib/hmr.js +357 -0
  20. package/dist/lib/injected-packages.d.ts.map +1 -1
  21. package/dist/lib/injected-packages.js +36 -13
  22. package/dist/lib/loaders.d.ts +57 -0
  23. package/dist/lib/loaders.d.ts.map +1 -0
  24. package/dist/lib/loaders.js +1 -0
  25. package/dist/lib/module-store.d.ts +24 -0
  26. package/dist/lib/module-store.d.ts.map +1 -1
  27. package/dist/lib/module-store.js +136 -11
  28. package/dist/lib/routes.js +1 -1
  29. package/dist/lib/scripts/compiler.d.ts +24 -1
  30. package/dist/lib/scripts/compiler.d.ts.map +1 -1
  31. package/dist/lib/scripts/compiler.js +183 -29
  32. package/dist/lib/scripts/conditions.d.ts +2 -0
  33. package/dist/lib/scripts/conditions.d.ts.map +1 -0
  34. package/dist/lib/scripts/conditions.js +1 -0
  35. package/dist/lib/scripts/emit.d.ts +3 -0
  36. package/dist/lib/scripts/emit.d.ts.map +1 -1
  37. package/dist/lib/scripts/emit.js +24 -5
  38. package/dist/lib/scripts/resolve.d.ts +4 -0
  39. package/dist/lib/scripts/resolve.d.ts.map +1 -1
  40. package/dist/lib/scripts/resolve.js +70 -5
  41. package/dist/lib/scripts/transform.d.ts +9 -0
  42. package/dist/lib/scripts/transform.d.ts.map +1 -1
  43. package/dist/lib/scripts/transform.js +222 -18
  44. package/dist/lib/source-maps.js +1 -1
  45. package/dist/lib/styles/compiler.d.ts +14 -1
  46. package/dist/lib/styles/compiler.d.ts.map +1 -1
  47. package/dist/lib/styles/compiler.js +78 -14
  48. package/dist/lib/styles/emit.js +4 -4
  49. package/dist/lib/styles/resolve.d.ts.map +1 -1
  50. package/dist/lib/styles/resolve.js +8 -7
  51. package/dist/lib/styles/transform.js +3 -3
  52. package/dist/lib/target.d.ts +1 -1
  53. package/dist/lib/target.d.ts.map +1 -1
  54. package/dist/lib/watch.js +1 -1
  55. package/dist/types/hmr.d.ts +36 -0
  56. package/package.json +17 -11
  57. package/src/assets.ts +2 -1
  58. package/src/lib/access.ts +386 -5
  59. package/src/lib/asset-server.ts +429 -18
  60. package/src/lib/compilation-error.ts +1 -0
  61. package/src/lib/files/compiler.ts +7 -3
  62. package/src/lib/hmr.ts +397 -0
  63. package/src/lib/injected-packages.ts +41 -11
  64. package/src/lib/loaders.ts +80 -0
  65. package/src/lib/module-store.ts +190 -9
  66. package/src/lib/scripts/compiler.ts +248 -24
  67. package/src/lib/scripts/conditions.ts +1 -0
  68. package/src/lib/scripts/emit.ts +50 -5
  69. package/src/lib/scripts/resolve.ts +124 -2
  70. package/src/lib/scripts/transform.ts +290 -19
  71. package/src/lib/styles/compiler.ts +108 -8
  72. package/src/lib/styles/emit.ts +1 -1
  73. package/src/lib/styles/resolve.ts +11 -7
  74. package/src/types/hmr.d.ts +36 -0
package/src/lib/hmr.ts ADDED
@@ -0,0 +1,397 @@
1
+ /**
2
+ * Messages consumed by the browser HMR client.
3
+ *
4
+ * Server runtimes use `server:update` to request reconciliation with freshly rendered server
5
+ * output. Asset tooling uses `browser:update` for updates accepted by JavaScript or CSS boundaries,
6
+ * and falls back to `browser:reload` when the page must reload.
7
+ */
8
+ export type HmrPayload =
9
+ | {
10
+ /** Indicates that server-rendered output may have changed. */
11
+ type: 'server:update'
12
+ }
13
+ | {
14
+ /** JavaScript and CSS modules that the browser should update. */
15
+ updates: Array<
16
+ | {
17
+ /** Importing module whose dependency-accept handler accepts this update. */
18
+ acceptedPath?: string
19
+ /** Public URL of the changed JavaScript module. */
20
+ path: string
21
+ /** Identifies a JavaScript module update. */
22
+ type: 'js'
23
+ }
24
+ | {
25
+ /** Public URL of the changed stylesheet. */
26
+ path: string
27
+ /** Identifies a stylesheet update. */
28
+ type: 'css'
29
+ }
30
+ >
31
+ /** Update time used to bypass browser module and stylesheet caches. */
32
+ timestamp: number
33
+ /** Indicates that the listed browser modules should update in place. */
34
+ type: 'browser:update'
35
+ }
36
+ | {
37
+ /** Indicates that no HMR boundary accepted the change and the page must reload. */
38
+ type: 'browser:reload'
39
+ }
40
+
41
+ export function createHmrClientSource(options: { eventPathname: string }): string {
42
+ return `
43
+ const contexts = new Map()
44
+ const dataByPath = new Map()
45
+
46
+ class RemixHmrContext {
47
+ constructor(path) {
48
+ this.path = path
49
+ this.data = dataByPath.get(path) ?? {}
50
+ this.acceptCallbacks = []
51
+ this.acceptDependencyCallbacks = []
52
+ this.disposeCallbacks = []
53
+ this.customEventCallbacks = new Map()
54
+ this.invalidated = false
55
+ this.updating = false
56
+ dataByPath.set(path, this.data)
57
+ }
58
+
59
+ accept(deps, callback) {
60
+ if (typeof deps === 'string') {
61
+ this.acceptDependencyCallbacks.push({
62
+ deps: [normalizeAcceptedDependency(this.path, deps)],
63
+ callback: callback ?? (() => {}),
64
+ })
65
+ return
66
+ }
67
+
68
+ if (Array.isArray(deps)) {
69
+ this.acceptDependencyCallbacks.push({
70
+ deps: deps.map((dep) => normalizeAcceptedDependency(this.path, dep)),
71
+ callback: callback ?? (() => {}),
72
+ })
73
+ return
74
+ }
75
+
76
+ this.acceptCallbacks.push(deps ?? (() => {}))
77
+ }
78
+
79
+ dispose(callback) {
80
+ this.disposeCallbacks.push(callback)
81
+ }
82
+
83
+ invalidate(message) {
84
+ this.invalidated = true
85
+ if (this.updating) {
86
+ if (message) console.debug(message)
87
+ return
88
+ }
89
+ if (message) console.debug(message)
90
+ reloadPage()
91
+ }
92
+
93
+ on(event, callback) {
94
+ let callbacks = this.customEventCallbacks.get(event)
95
+ if (!callbacks) {
96
+ callbacks = []
97
+ this.customEventCallbacks.set(event, callbacks)
98
+ }
99
+ callbacks.push(callback)
100
+ }
101
+ }
102
+
103
+ export function createHotContext(path) {
104
+ let context = new RemixHmrContext(path)
105
+ contexts.set(path, context)
106
+ return context
107
+ }
108
+
109
+ let connected = false
110
+ let reconnectPending = false
111
+ let pageReloadTimer
112
+ let failedJavaScriptUpdates = new Map()
113
+ let stylesheetUpdatePromise = Promise.resolve()
114
+
115
+ let events = new EventSource(${JSON.stringify(options.eventPathname)})
116
+
117
+ events.onopen = () => {
118
+ console.debug('[remix] HMR connected')
119
+ if (reconnectPending) {
120
+ reconnectPending = false
121
+ console.log('[remix] HMR reconnected')
122
+ handleReconnect().catch((error) => {
123
+ console.error('[remix] HMR reconnect recovery failed', error)
124
+ reloadPage()
125
+ })
126
+ }
127
+ connected = true
128
+ }
129
+
130
+ events.onerror = () => {
131
+ if (!connected) return
132
+ connected = false
133
+ reconnectPending = true
134
+ console.log('[remix] HMR connection lost, retrying...')
135
+ }
136
+
137
+ events.onmessage = (event) => {
138
+ let payload = JSON.parse(event.data)
139
+ handlePayload(payload).catch((error) => {
140
+ console.error('[remix] HMR update failed', error)
141
+ if (payload.type !== 'browser:update' || payload.updates.some((update) => update.type !== 'js')) {
142
+ reloadPage()
143
+ }
144
+ })
145
+ }
146
+
147
+ async function handlePayload(payload) {
148
+ if (payload.type === 'browser:reload') {
149
+ reloadPage()
150
+ return
151
+ }
152
+
153
+ if (payload.type === 'server:update') {
154
+ await retryFailedJavaScriptUpdates(payload)
155
+ await dispatchCustomEvent(payload.type, payload)
156
+ return
157
+ }
158
+
159
+ if (payload.type === 'browser:update') {
160
+ for (let update of payload.updates) {
161
+ if (update.type === 'css') {
162
+ let updated = await queueStylesheetUpdate(update.path, payload.timestamp)
163
+ if (updated) console.debug('[remix] HMR updated stylesheet', update.path)
164
+ continue
165
+ }
166
+
167
+ try {
168
+ let updated = await updateJavaScriptModule(
169
+ update.path,
170
+ update.acceptedPath ?? update.path,
171
+ payload.timestamp,
172
+ )
173
+ failedJavaScriptUpdates.delete(update.path)
174
+ if (updated) console.debug('[remix] HMR accepted update', update.path)
175
+ } catch (error) {
176
+ failedJavaScriptUpdates.set(update.path, update.acceptedPath ?? update.path)
177
+ throw error
178
+ }
179
+ }
180
+ }
181
+ }
182
+
183
+ async function handleReconnect() {
184
+ let data = { timestamp: Date.now() }
185
+ await reloadCurrentStylesheets(data)
186
+ await retryFailedJavaScriptUpdates(data)
187
+ }
188
+
189
+ async function retryFailedJavaScriptUpdates(data) {
190
+ if (failedJavaScriptUpdates.size === 0) return
191
+
192
+ let timestamp = getTimestamp(data)
193
+ for (let [path, acceptedPath] of Array.from(failedJavaScriptUpdates)) {
194
+ try {
195
+ let updated = await updateJavaScriptModule(path, acceptedPath, timestamp)
196
+ failedJavaScriptUpdates.delete(path)
197
+ if (updated) console.debug('[remix] HMR recovered update', path)
198
+ } catch (error) {
199
+ console.error('[remix] HMR recovery update failed', error)
200
+ }
201
+ }
202
+ }
203
+
204
+ async function reloadCurrentStylesheets(data) {
205
+ let timestamp = getTimestamp(data)
206
+ let paths = new Set()
207
+ for (let link of document.querySelectorAll('link[rel="stylesheet"]')) {
208
+ if (link.dataset.remixHmrStylesheet === 'true') continue
209
+ let url = new URL(link.href)
210
+ if (url.origin !== location.origin) continue
211
+ paths.add(url.pathname)
212
+ }
213
+
214
+ for (let path of paths) {
215
+ await queueStylesheetUpdate(path, timestamp)
216
+ }
217
+ }
218
+
219
+ function getTimestamp(data) {
220
+ if (data && typeof data === 'object' && typeof data.timestamp === 'number') {
221
+ return data.timestamp
222
+ }
223
+
224
+ return Date.now()
225
+ }
226
+
227
+ async function updateJavaScriptModule(path, acceptedPath, timestamp) {
228
+ let previousContext = contexts.get(path)
229
+ if (!previousContext) return false
230
+
231
+ let isSelfUpdate = path === acceptedPath
232
+ let dependencyCallbacks = getAcceptDependencyCallbacks(previousContext, acceptedPath)
233
+
234
+ if (
235
+ isSelfUpdate ? previousContext.acceptCallbacks.length === 0 : dependencyCallbacks.length === 0
236
+ ) {
237
+ console.log('[remix] HMR no accept handler, reloading page', path)
238
+ reloadPage()
239
+ return false
240
+ }
241
+
242
+ if (isSelfUpdate) {
243
+ for (let callback of previousContext.disposeCallbacks) {
244
+ await callback(previousContext.data)
245
+ }
246
+
247
+ let updatedModule = await import(withTimestamp(path, timestamp))
248
+ previousContext.invalidated = false
249
+ previousContext.updating = true
250
+ try {
251
+ for (let callback of previousContext.acceptCallbacks) {
252
+ await callback(updatedModule)
253
+ }
254
+ } finally {
255
+ previousContext.updating = false
256
+ }
257
+ if (previousContext.invalidated) {
258
+ await propagateInvalidatedJavaScriptModule(path, timestamp)
259
+ }
260
+ return true
261
+ }
262
+
263
+ let acceptedContext = contexts.get(acceptedPath)
264
+ if (acceptedContext) {
265
+ for (let callback of acceptedContext.disposeCallbacks) {
266
+ await callback(acceptedContext.data)
267
+ }
268
+ }
269
+
270
+ let updatedModule = await import(withTimestamp(acceptedPath, timestamp))
271
+ previousContext.invalidated = false
272
+ previousContext.updating = true
273
+ try {
274
+ for (let { deps, callback } of dependencyCallbacks) {
275
+ if (deps.length === 1) {
276
+ await callback(updatedModule)
277
+ } else {
278
+ await callback(deps.map((dep) => (dep === acceptedPath ? updatedModule : undefined)))
279
+ }
280
+ }
281
+ } finally {
282
+ previousContext.updating = false
283
+ }
284
+ if (previousContext.invalidated) {
285
+ await propagateInvalidatedJavaScriptModule(path, timestamp)
286
+ }
287
+ return true
288
+ }
289
+
290
+ async function propagateInvalidatedJavaScriptModule(path, timestamp) {
291
+ let updated = false
292
+ for (let [importerPath, importerContext] of contexts) {
293
+ if (importerPath === path) continue
294
+ let callbacks = getAcceptDependencyCallbacks(importerContext, path)
295
+ if (callbacks.length === 0) continue
296
+
297
+ for (let callback of importerContext.disposeCallbacks) {
298
+ await callback(importerContext.data)
299
+ }
300
+
301
+ let updatedModule = await import(withTimestamp(path, timestamp))
302
+ importerContext.invalidated = false
303
+ importerContext.updating = true
304
+ try {
305
+ for (let { deps, callback } of callbacks) {
306
+ if (deps.length === 1) {
307
+ await callback(updatedModule)
308
+ } else {
309
+ await callback(deps.map((dep) => (dep === path ? updatedModule : undefined)))
310
+ }
311
+ }
312
+ } finally {
313
+ importerContext.updating = false
314
+ }
315
+ updated = true
316
+
317
+ if (importerContext.invalidated) {
318
+ reloadPage()
319
+ return
320
+ }
321
+ }
322
+
323
+ if (!updated) reloadPage()
324
+ }
325
+
326
+ function getAcceptDependencyCallbacks(context, acceptedPath) {
327
+ return context.acceptDependencyCallbacks.filter(({ deps }) => deps.includes(acceptedPath))
328
+ }
329
+
330
+ function normalizeAcceptedDependency(importerPath, dep) {
331
+ if (dep.startsWith('/')) return dep
332
+ return new URL(dep, new URL(importerPath, window.location.href)).pathname
333
+ }
334
+
335
+ async function dispatchCustomEvent(event, data) {
336
+ for (let context of contexts.values()) {
337
+ let callbacks = context.customEventCallbacks.get(event) ?? []
338
+ for (let callback of callbacks) {
339
+ await callback(data)
340
+ }
341
+ }
342
+ }
343
+
344
+ async function reloadStylesheet(path, timestamp) {
345
+ let links = document.querySelectorAll('link[rel="stylesheet"]')
346
+ let updates = []
347
+ for (let link of links) {
348
+ let url = new URL(link.href)
349
+ if (url.pathname !== path) continue
350
+ if (link.dataset.remixHmrStylesheet === 'true') continue
351
+
352
+ updates.push(loadStylesheet(link, path, timestamp))
353
+ }
354
+
355
+ if (updates.length === 0) return false
356
+ return (await Promise.all(updates)).some(Boolean)
357
+ }
358
+
359
+ async function queueStylesheetUpdate(path, timestamp) {
360
+ let update = stylesheetUpdatePromise.then(() => reloadStylesheet(path, timestamp))
361
+ stylesheetUpdatePromise = update.catch(() => {})
362
+ return update
363
+ }
364
+
365
+ function loadStylesheet(link, path, timestamp) {
366
+ return new Promise((resolve) => {
367
+ let next = link.cloneNode()
368
+ next.dataset.remixHmrStylesheet = 'true'
369
+ next.href = withTimestamp(path, timestamp)
370
+ next.onload = () => {
371
+ delete next.dataset.remixHmrStylesheet
372
+ link.remove()
373
+ resolve(true)
374
+ }
375
+ next.onerror = () => {
376
+ next.remove()
377
+ resolve(false)
378
+ }
379
+ link.after(next)
380
+ })
381
+ }
382
+
383
+ function withTimestamp(path, timestamp) {
384
+ let url = new URL(path, location.href)
385
+ url.searchParams.set('t', String(timestamp))
386
+ return url.pathname + url.search
387
+ }
388
+
389
+ function reloadPage() {
390
+ if (pageReloadTimer) clearTimeout(pageReloadTimer)
391
+ pageReloadTimer = setTimeout(() => {
392
+ console.debug('[remix] HMR reloading page')
393
+ window.location.href = window.location.href
394
+ }, 20)
395
+ }
396
+ `.trimStart()
397
+ }
@@ -9,6 +9,8 @@ type ResolvedInjectedPackage = {
9
9
  }
10
10
 
11
11
  const injectedPackageNames = ['@oxc-project/runtime'] as const
12
+ const authoredInjectedPackageNames = ['@oxc-project/runtime'] as const
13
+ const generatedInjectedPackageSpecifiers = [] as const
12
14
  const injectedPackagesBasePath = '/__@remix/injected'
13
15
 
14
16
  const resolvedInjectedPackages = new Map<string, ResolvedInjectedPackage>()
@@ -32,18 +34,25 @@ export function getInjectedPackageRouteConfigs(): {
32
34
  }[] {
33
35
  return injectedPackageNames.map((packageName) => {
34
36
  let { packageRoot } = getResolvedInjectedPackage(packageName)
37
+ let { filePattern, routeRoot } = getInjectedPackageRoute(packageRoot, packageName)
35
38
 
36
39
  return {
37
40
  fileMap: {
38
- [getInjectedPackageRoutePattern(packageName)]: `${packageName}/*path`,
41
+ [getInjectedPackageRoutePattern(packageName)]: filePattern,
39
42
  },
40
- rootDir: getInjectedPackageRouteRoot(packageRoot, packageName),
43
+ rootDir: routeRoot,
41
44
  }
42
45
  })
43
46
  }
44
47
 
45
48
  export function getInjectedPackageNameForSpecifier(specifier: string): string | null {
46
- for (let packageName of injectedPackageNames) {
49
+ for (let injectedSpecifier of generatedInjectedPackageSpecifiers) {
50
+ if (specifier === injectedSpecifier) {
51
+ return getPackageName(injectedSpecifier)
52
+ }
53
+ }
54
+
55
+ for (let packageName of authoredInjectedPackageNames) {
47
56
  if (specifier === packageName || specifier.startsWith(`${packageName}/`)) {
48
57
  return packageName
49
58
  }
@@ -53,19 +62,22 @@ export function getInjectedPackageNameForSpecifier(specifier: string): string |
53
62
  }
54
63
 
55
64
  export function mayContainInjectedPackageSpecifier(sourceText: string): boolean {
56
- return injectedPackageNames.some((packageName) => sourceText.includes(packageName))
65
+ return authoredInjectedPackageNames.some((packageName) => sourceText.includes(packageName))
57
66
  }
58
67
 
59
68
  export function maskAuthoredInjectedPackageSpecifier(specifier: string): string | null {
60
- let packageName = getInjectedPackageNameForSpecifier(specifier)
61
- if (!packageName) return null
69
+ for (let packageName of authoredInjectedPackageNames) {
70
+ if (specifier !== packageName && !specifier.startsWith(`${packageName}/`)) continue
62
71
 
63
- let maskedPackageName = getMaskedInjectedPackageName(packageName)
64
- return `${maskedPackageName}${specifier.slice(packageName.length)}`
72
+ let maskedPackageName = getMaskedInjectedPackageName(packageName)
73
+ return `${maskedPackageName}${specifier.slice(packageName.length)}`
74
+ }
75
+
76
+ return null
65
77
  }
66
78
 
67
79
  export function restoreAuthoredInjectedPackageSpecifier(specifier: string): string | null {
68
- for (let packageName of injectedPackageNames) {
80
+ for (let packageName of authoredInjectedPackageNames) {
69
81
  let maskedPackageName = getMaskedInjectedPackageName(packageName)
70
82
  if (specifier === maskedPackageName) {
71
83
  return packageName
@@ -82,6 +94,11 @@ function getMaskedInjectedPackageName(packageName: string): string {
82
94
  return `~${packageName.slice(1)}`
83
95
  }
84
96
 
97
+ function getPackageName(specifier: string): string {
98
+ let parts = specifier.split('/')
99
+ return parts[0]?.startsWith('@') ? `${parts[0]}/${parts[1]}` : (parts[0] ?? specifier)
100
+ }
101
+
85
102
  export function getInjectedPackageImporterPath(): string {
86
103
  return normalizeFilePath(fileURLToPath(import.meta.url))
87
104
  }
@@ -106,12 +123,25 @@ function getInjectedPackageRoutePattern(packageName: string): string {
106
123
  return `${injectedPackagesBasePath}/${packageName}/*path`
107
124
  }
108
125
 
109
- function getInjectedPackageRouteRoot(packageRoot: string, packageName: string): string {
126
+ function getInjectedPackageRoute(
127
+ packageRoot: string,
128
+ packageName: string,
129
+ ): { filePattern: string; routeRoot: string } {
130
+ if (!packageRoot.endsWith(`/${packageName}`)) {
131
+ return {
132
+ filePattern: `${packageRoot.slice(getFilePathDirectory(packageRoot).length + 1)}/*path`,
133
+ routeRoot: getFilePathDirectory(packageRoot),
134
+ }
135
+ }
136
+
110
137
  let routeRoot = packageRoot
111
138
 
112
139
  for (let _segment of packageName.split('/')) {
113
140
  routeRoot = getFilePathDirectory(routeRoot)
114
141
  }
115
142
 
116
- return routeRoot
143
+ return {
144
+ filePattern: `${packageName}/*path`,
145
+ routeRoot,
146
+ }
117
147
  }
@@ -0,0 +1,80 @@
1
+ type ModuleFloat16Array = typeof globalThis extends {
2
+ Float16Array: { prototype: infer array }
3
+ }
4
+ ? array
5
+ : never
6
+
7
+ type ModuleTypedArray =
8
+ | Uint8Array
9
+ | Uint8ClampedArray
10
+ | Uint16Array
11
+ | Uint32Array
12
+ | Int8Array
13
+ | Int16Array
14
+ | Int32Array
15
+ | BigUint64Array
16
+ | BigInt64Array
17
+ | ModuleFloat16Array
18
+ | Float32Array
19
+ | Float64Array
20
+
21
+ type ModuleLoadSource = string | ArrayBuffer | ModuleTypedArray
22
+
23
+ /**
24
+ * Synchronously loads a module or delegates to the next configured loader.
25
+ *
26
+ * Loaders follow Node's synchronous `load` hook signature and chaining contract. The asset server
27
+ * passes them JavaScript after its TypeScript/JavaScript transform and before HMR analysis and
28
+ * minification. Each loader must delegate to `nextLoad` or return `shortCircuit: true`.
29
+ *
30
+ * @param url Resolved `file:` URL for the module being compiled.
31
+ * @param context Format, conditions, attributes, and public asset URL for this load.
32
+ * @param nextLoad Next loader in the chain, ending with the asset server's compiled JavaScript.
33
+ * @returns A Node-compatible load result containing ES module source.
34
+ */
35
+ export type ModuleLoader = (
36
+ url: string,
37
+ context: ModuleLoadContext,
38
+ nextLoad: NextModuleLoader,
39
+ ) => ModuleLoadResult
40
+
41
+ /**
42
+ * Continues loading through the remaining loader chain.
43
+ *
44
+ * Omitted context fields retain their current values.
45
+ *
46
+ * @param url Current module URL. Loaders cannot change this URL.
47
+ * @param context Context fields to override for the remainder of this load.
48
+ * @returns The source produced by the remaining loaders or the asset compiler.
49
+ */
50
+ export type NextModuleLoader = (
51
+ url: string,
52
+ context?: Partial<ModuleLoadContext>,
53
+ ) => ModuleLoadResult
54
+
55
+ /** State passed through a module loader chain. */
56
+ export interface ModuleLoadContext {
57
+ /** Package export conditions used while compiling this module. */
58
+ conditions: string[]
59
+ /** Current module format. Asset-server loaders must ultimately return `'module'`. */
60
+ format: string | null | undefined
61
+ /** Import attributes supplied by the previous loader. Authored import attributes are unsupported. */
62
+ importAttributes: Record<string, string | undefined>
63
+ /**
64
+ * Stable public URL path used to request this module from the asset server.
65
+ *
66
+ * Unlike Node's standard load context, this field is provided so loaders can use the browser
67
+ * module identity rather than its private filesystem URL.
68
+ */
69
+ moduleUrl?: string
70
+ }
71
+
72
+ /** Source returned by a module loader. */
73
+ export interface ModuleLoadResult {
74
+ /** Loaded module format. Must be `'module'` for asset-server scripts. */
75
+ format: string | null | undefined
76
+ /** Whether this result intentionally ends the loader chain without calling `nextLoad`. */
77
+ shortCircuit?: boolean
78
+ /** Compiled JavaScript source, optionally rewritten by the loader. */
79
+ source?: ModuleLoadSource
80
+ }