MCPcopy Index your code
hub / github.com/nodejs/node / compileSourceTextModule

Function compileSourceTextModule

lib/internal/modules/esm/utils.js:337–379  ·  view source on GitHub ↗

* Compile a SourceTextModule for the built-in ESM loader. Register it for default * source map and import.meta and dynamic import() handling if cascadedLoader is provided. * @param {string} url URL of the module. * @param {string} source Source code of the module. * @param {string} type Type of

(url, source, type, context = kEmptyObject)

Source from the content-addressed store, hash-verified

335 * @returns {ModuleWrap}
336 */
337function compileSourceTextModule(url, source, type, context = kEmptyObject) {
338 let hostDefinedOptions;
339 switch (type) {
340 case SourceTextModuleTypes.kFacade:
341 case SourceTextModuleTypes.kInternal:
342 hostDefinedOptions = undefined;
343 break;
344 case SourceTextModuleTypes.kEmbedder:
345 hostDefinedOptions = embedder_module_hdo;
346 break;
347 case SourceTextModuleTypes.kUser:
348 hostDefinedOptions = source_text_module_default_hdo;
349 break;
350 default:
351 assert.fail(`Unknown SourceTextModule type: ${type}`);
352 }
353
354 const wrap = new ModuleWrap(url, undefined, source, 0, 0, hostDefinedOptions);
355
356 if (type === SourceTextModuleTypes.kFacade) {
357 return wrap;
358 }
359
360 const { isMain } = context;
361 if (isMain) {
362 wrap.isMain = true;
363 }
364
365 // Cache the source map for the module if present.
366 if (wrap.sourceMapURL) {
367 maybeCacheSourceMap(url, source, wrap, false, wrap.sourceURL, wrap.sourceMapURL);
368 }
369
370 if (type === SourceTextModuleTypes.kEmbedder) {
371 // For embedder ESM, we also handle the linking and evaluation.
372 const requests = wrap.getModuleRequests();
373 const modules = requests.map(({ specifier }) => getBuiltinModuleWrapForEmbedder(specifier));
374 wrap.link(modules);
375 wrap.instantiate();
376 wrap.evaluate(-1, false);
377 }
378 return wrap;
379}
380
381const kImportInImportedESM = Symbol('kImportInImportedESM');
382const kImportInRequiredESM = Symbol('kImportInRequiredESM');

Callers 5

createDynamicModuleFunction · 0.85
createModuleWrapMethod · 0.85
importSyncForRequireMethod · 0.85
translators.jsFile · 0.85
embedderRunESMFunction · 0.85

Calls 7

maybeCacheSourceMapFunction · 0.85
mapMethod · 0.65
failMethod · 0.45
linkMethod · 0.45
instantiateMethod · 0.45
evaluateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…