Repository: nodejs/node
File: lib/internal/modules/esm/utils.js (JavaScript)

Code before:
 * @param {boolean} [shouldSpawnLoaderHookWorker] Whether the custom loader worker
 *   should be spawned later.
 */
function initializeESM(shouldSpawnLoaderHookWorker = true) {
  _shouldSpawnLoaderHookWorker = shouldSpawnLoaderHookWorker;
  initializeDefaultConditions();
  // Setup per-realm callbacks that locate data or callbacks that we keep
  // track of for different ESM modules.
  setInitializeImportMetaObjectCallback(initializeImportMetaObject);
  setImportModuleDynamicallyCallback(importModuleDynamicallyCallback);
}

/**
 * Determine whether the custom loader worker should be spawned when initializing
 * the singleton ESM loader.
 * @returns {boolean}
 */
function shouldSpawnLoaderHookWorker() {
  return _shouldSpawnLoaderHookWorker;
}

const SourceTextModuleTypes = {
  kInternal: 'internal',  // TODO(joyeecheung): support internal ESM.
  kEmbedder: 'embedder',  // Embedder ESM, also used by SEA
  kUser: 'user', // User-land ESM
  kDynamic: 'dynamic',  // Currently only used by the facade that proxies WASM module import/exports.
};

/**
 * 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 the source text module, one of SourceTextModuleTypes.
 * @param {{ isMain?: boolean }|undefined} context - context object containing module metadata.
 * @returns {ModuleWrap}
 */
function compileSourceTextModule(url, source, type, context = kEmptyObject) {
  let hostDefinedOptions;
  switch (type) {
    case SourceTextModuleTypes.kDynamic:
    case SourceTextModuleTypes.kInternal:
      hostDefinedOptions = undefined;
      break;
    case SourceTextModuleTypes.kEmbedder:
      hostDefinedOptions = embedder_module_hdo;
      break;
    case SourceTextModuleTypes.kUser:
      hostDefinedOptions = source_text_module_default_hdo;
      break;
    default:

Review comment (suggestion):
I feel like the term `dynamic` in ESM could be related to the dynamic import calls. Given that this creates a module with a facade, and this is also not a `synthetic` module, maybe we should rename it to a facade module.

Nevertheless, this is not a blocker for this PR.

Code after:
 * @param {boolean} [shouldSpawnLoaderHookWorker] Whether the custom loader worker
 *   should be spawned later.
 */
function initializeESM(shouldSpawnLoaderHookWorker = true) {
  _shouldSpawnLoaderHookWorker = shouldSpawnLoaderHookWorker;
  initializeDefaultConditions();
  // Setup per-realm callbacks that locate data or callbacks that we keep
  // track of for different ESM modules.
  setInitializeImportMetaObjectCallback(initializeImportMetaObject);
  setImportModuleDynamicallyCallback(importModuleDynamicallyCallback);
}

/**
 * Determine whether the custom loader worker should be spawned when initializing
 * the singleton ESM loader.
 * @returns {boolean}
 */
function shouldSpawnLoaderHookWorker() {
  return _shouldSpawnLoaderHookWorker;
}

const SourceTextModuleTypes = {
  kInternal: 'internal',  // TODO(joyeecheung): support internal ESM.
  kEmbedder: 'embedder',  // Embedder ESM, also used by SEA
  kUser: 'user', // User-land ESM
  kFacade: 'facade',  // Currently only used by the facade that proxies WASM module import/exports.
};

/**
 * 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 the source text module, one of SourceTextModuleTypes.
 * @param {{ isMain?: boolean }|undefined} context - context object containing module metadata.
 * @returns {ModuleWrap}
 */
function compileSourceTextModule(url, source, type, context = kEmptyObject) {
  let hostDefinedOptions;
  switch (type) {
    case SourceTextModuleTypes.kFacade:
    case SourceTextModuleTypes.kInternal:
      hostDefinedOptions = undefined;
      break;
    case SourceTextModuleTypes.kEmbedder:
      hostDefinedOptions = embedder_module_hdo;
      break;
    case SourceTextModuleTypes.kUser:
      hostDefinedOptions = source_text_module_default_hdo;
      break;
    default: