MCPcopy
hub / github.com/webpack/webpack / createEsmRunner

Method createEsmRunner

test/harness/runner/index.js:554–735  ·  view source on GitHub ↗

* @returns {(moduleInfo: ModuleInfo, context: RequireContext) => Promise<EXPECTED_ANY>} esm runner

()

Source from the content-addressed store, hash-verified

552 * @returns {(moduleInfo: ModuleInfo, context: RequireContext) => Promise<EXPECTED_ANY>} esm runner
553 */
554 createEsmRunner() {
555 const asModule = require("./asModule");
556
557 const createEsmContext = () =>
558 vm.createContext(
559 { ...this._moduleScope, ...this._esmContext },
560 {
561 name: "context for esm"
562 }
563 );
564
565 /** @type {vm.Context | null} */
566 let esmContext = null;
567
568 /** @type {Map<string, vm.SourceTextModule>} */
569 const esmCache = new Map();
570 const { category, name, round } = this.testMeta;
571
572 const testMetaStr = `${category}-${name}-${round || 0}`;
573 const appendTestMeta = (/** @type {string} */ identifier) =>
574 `${identifier}?${testMetaStr}`;
575
576 /**
577 * @param {string} identifier identifier
578 * @param {string} content content
579 * @returns {vm.SourceTextModule} SourceTextModule
580 */
581 const getModuleInstance = (identifier, content) => {
582 let instance = esmCache.get(identifier);
583 if (!instance) {
584 let moduleSource = content;
585 // Deno 2.8.3 hard-panics ("Module not found", bindings.rs) the moment
586 // `import.meta` is accessed inside a vm SourceTextModule (no
587 // initializeImportMeta shape avoids it). Rewrite the `import.meta`
588 // meta-property to a prepended object so the module evaluates; parse to
589 // only touch real syntax, never string/comment text. Node/Bun keep the
590 // initializeImportMeta callback below.
591 if (process.versions.deno && /\bimport\.meta\b/.test(content)) {
592 moduleSource = rewriteImportMeta(content, () => {
593 /** @type {Record<string, string>} */
594 const meta = { url: pathToFileURL(identifier).href };
595 if (this.hasNodeTarget()) {
596 meta.filename = identifier;
597 meta.dirname = path.dirname(identifier);
598 }
599 return meta;
600 });
601 }
602 instance = new vm.SourceTextModule(
603 moduleSource,
604 /** @type {EXPECTED_ANY} */ ({
605 identifier: appendTestMeta(identifier),
606 url: appendTestMeta(pathToFileURL(identifier).href),
607 context: esmContext,
608 initializeImportMeta: (
609 /** @type {EXPECTED_ANY} */ meta,
610 /** @type {EXPECTED_ANY} */ _module
611 ) => {

Callers 1

createModuleRunnersMethod · 0.95

Calls 3

resolveMethod · 0.65
requireFunction · 0.50
runFunction · 0.50

Tested by

no test coverage detected