MCPcopy
hub / github.com/webpack/webpack / getModuleInstance

Method getModuleInstance

test/harness/runner/index.js:581–660  ·  view source on GitHub ↗
(identifier, content)

Source from the content-addressed store, hash-verified

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 ) => {
612 meta.url = pathToFileURL(identifier).href;
613
614 if (this.hasNodeTarget()) {
615 meta.filename = identifier;
616 meta.dirname = path.dirname(identifier);
617 }
618 },
619 importModuleDynamically: async (
620 /** @type {string} */ specifier,
621 /** @type {vm.Module} */ module,
622 /** @type {EXPECTED_ANY} */ importAttributes
623 ) => {
624 const normalizedSpecifier = specifier.startsWith("file:")
625 ? `./${path.relative(
626 path.dirname(identifier),
627 fileURLToPath(specifier)
628 )}`
629 : specifier.replace(
630 /https:\/\/example.com\/public\/path\//,
631 "./"
632 );
633
634 const res = await this.require(
635 path.dirname(identifier),
636 normalizedSpecifier,
637 {
638 esmReturnStatus: ESModuleStatus.Evaluated

Callers

nothing calls this directly

Calls 6

hasNodeTargetMethod · 0.95
requireMethod · 0.95
getMethod · 0.45
testMethod · 0.45
replaceMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected