@@ -13,12 +13,18 @@ const {
13
13
} ,
14
14
} = internalBinding ( 'util' ) ;
15
15
const {
16
+ source_text_module_default_hdo,
16
17
vm_dynamic_import_default_internal,
17
18
vm_dynamic_import_main_context_default,
18
19
vm_dynamic_import_missing_flag,
19
20
vm_dynamic_import_no_callback,
20
21
} = internalBinding ( 'symbols' ) ;
21
22
23
+ const { ModuleWrap } = internalBinding ( 'module_wrap' ) ;
24
+ const {
25
+ maybeCacheSourceMap,
26
+ } = require ( 'internal/source_map/source_map_cache' ) ;
27
+
22
28
const {
23
29
ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG ,
24
30
ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING ,
@@ -167,28 +173,55 @@ function registerModule(referrer, registry) {
167
173
moduleRegistries . set ( idSymbol , registry ) ;
168
174
}
169
175
176
+ /**
177
+ * Proxy the import meta handling to the default loader for source text modules.
178
+ * @param {Record<string, string | Function> } meta - The import.meta object to initialize.
179
+ * @param {ModuleWrap } wrap - The ModuleWrap of the SourceTextModule where `import.meta` is referenced.
180
+ */
181
+ function defaultInitializeImportMetaForModule ( meta , wrap ) {
182
+ const cascadedLoader = require ( 'internal/modules/esm/loader' ) . getOrInitializeCascadedLoader ( ) ;
183
+ return cascadedLoader . importMetaInitialize ( meta , { url : wrap . url } ) ;
184
+ }
185
+
170
186
/**
171
187
* Defines the `import.meta` object for a given module.
172
188
* @param {symbol } symbol - Reference to the module.
173
189
* @param {Record<string, string | Function> } meta - The import.meta object to initialize.
190
+ * @param {ModuleWrap } wrap - The ModuleWrap of the SourceTextModule where `import.meta` is referenced.
174
191
*/
175
- function initializeImportMetaObject ( symbol , meta ) {
176
- if ( moduleRegistries . has ( symbol ) ) {
177
- const { initializeImportMeta, callbackReferrer } = moduleRegistries . get ( symbol ) ;
178
- if ( initializeImportMeta !== undefined ) {
179
- meta = initializeImportMeta ( meta , callbackReferrer ) ;
180
- }
192
+ function initializeImportMetaObject ( symbol , meta , wrap ) {
193
+ if ( symbol === source_text_module_default_hdo ) {
194
+ defaultInitializeImportMetaForModule ( meta , wrap ) ;
195
+ return ;
196
+ }
197
+ const data = moduleRegistries . get ( symbol ) ;
198
+ assert ( data , `import.meta registry not found for ${ wrap . url } ` ) ;
199
+ const { initializeImportMeta, callbackReferrer } = data ;
200
+ if ( initializeImportMeta !== undefined ) {
201
+ meta = initializeImportMeta ( meta , callbackReferrer ) ;
181
202
}
182
203
}
183
204
184
205
/**
185
- * Proxy the dynamic import to the default loader.
206
+ * Proxy the dynamic import handling to the default loader for source text modules.
207
+ * @param {string } specifier - The module specifier string.
208
+ * @param {Record<string, string> } attributes - The import attributes object.
209
+ * @param {string|null|undefined } referrerName - name of the referrer.
210
+ * @returns {Promise<import('internal/modules/esm/loader.js').ModuleExports> } - The imported module object.
211
+ */
212
+ function defaultImportModuleDynamicallyForModule ( specifier , attributes , referrerName ) {
213
+ const cascadedLoader = require ( 'internal/modules/esm/loader' ) . getOrInitializeCascadedLoader ( ) ;
214
+ return cascadedLoader . import ( specifier , referrerName , attributes ) ;
215
+ }
216
+
217
+ /**
218
+ * Proxy the dynamic import to the default loader for classic scripts.
186
219
* @param {string } specifier - The module specifier string.
187
220
* @param {Record<string, string> } attributes - The import attributes object.
188
221
* @param {string|null|undefined } referrerName - name of the referrer.
189
222
* @returns {Promise<import('internal/modules/esm/loader.js').ModuleExports> } - The imported module object.
190
223
*/
191
- function defaultImportModuleDynamically ( specifier , attributes , referrerName ) {
224
+ function defaultImportModuleDynamicallyForScript ( specifier , attributes , referrerName ) {
192
225
const parentURL = normalizeReferrerURL ( referrerName ) ;
193
226
const cascadedLoader = require ( 'internal/modules/esm/loader' ) . getOrInitializeCascadedLoader ( ) ;
194
227
return cascadedLoader . import ( specifier , parentURL , attributes ) ;
@@ -208,12 +241,16 @@ async function importModuleDynamicallyCallback(referrerSymbol, specifier, attrib
208
241
// and fall back to the default loader.
209
242
if ( referrerSymbol === vm_dynamic_import_main_context_default ) {
210
243
emitExperimentalWarning ( 'vm.USE_MAIN_CONTEXT_DEFAULT_LOADER' ) ;
211
- return defaultImportModuleDynamically ( specifier , attributes , referrerName ) ;
244
+ return defaultImportModuleDynamicallyForScript ( specifier , attributes , referrerName ) ;
212
245
}
213
246
// For script compiled internally that should use the default loader to handle dynamic
214
247
// import, proxy the request to the default loader without the warning.
215
248
if ( referrerSymbol === vm_dynamic_import_default_internal ) {
216
- return defaultImportModuleDynamically ( specifier , attributes , referrerName ) ;
249
+ return defaultImportModuleDynamicallyForScript ( specifier , attributes , referrerName ) ;
250
+ }
251
+ // For SourceTextModules compiled internally, proxy the request to the default loader.
252
+ if ( referrerSymbol === source_text_module_default_hdo ) {
253
+ return defaultImportModuleDynamicallyForModule ( specifier , attributes , referrerName ) ;
217
254
}
218
255
219
256
if ( moduleRegistries . has ( referrerSymbol ) ) {
@@ -286,6 +323,29 @@ async function initializeHooks() {
286
323
return hooks ;
287
324
}
288
325
326
+ /**
327
+ * Compile a SourceTextModule for the built-in ESM loader. Register it for default
328
+ * source map and import.meta and dynamic import() handling if cascadedLoader is provided.
329
+ * @param {string } url URL of the module.
330
+ * @param {string } source Source code of the module.
331
+ * @param {typeof import('./loader.js').ModuleLoader|undefined } cascadedLoader If provided,
332
+ * register the module for default handling.
333
+ * @returns {ModuleWrap }
334
+ */
335
+ function compileSourceTextModule ( url , source , cascadedLoader ) {
336
+ const hostDefinedOption = cascadedLoader ? source_text_module_default_hdo : undefined ;
337
+ const wrap = new ModuleWrap ( url , undefined , source , 0 , 0 , hostDefinedOption ) ;
338
+
339
+ if ( ! cascadedLoader ) {
340
+ return wrap ;
341
+ }
342
+ // Cache the source map for the module if present.
343
+ if ( wrap . sourceMapURL ) {
344
+ maybeCacheSourceMap ( url , source , null , false , undefined , wrap . sourceMapURL ) ;
345
+ }
346
+ return wrap ;
347
+ }
348
+
289
349
module . exports = {
290
350
registerModule,
291
351
initializeESM,
@@ -294,4 +354,5 @@ module.exports = {
294
354
getConditionsSet,
295
355
loaderWorkerId : 'internal/modules/esm/worker' ,
296
356
forceDefaultLoader,
357
+ compileSourceTextModule,
297
358
} ;
0 commit comments