| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 42693aa commit 0d2921f
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -40,14 +40,20 @@ async function importModuleDynamicallyCallback(wrap, specifier, assertions) { | |||
| 40 | 40 | }; | |
| 41 | 41 | ||
| 42 | 42 | const esmLoader = new ESMLoader(); | |
| 43 | - | ||
| 44 | 43 | exports.esmLoader = esmLoader; | |
| 45 | 44 | ||
| 45 | + // Module.runMain() causes loadESM() to re-run (which it should do); however, this should NOT cause | ||
| 46 | + // ESM to be re-initialised; doing so causes duplicate custom loaders to be added to the public | ||
| 47 | + // esmLoader. | ||
| 48 | + let isESMInitialized = false; | ||
| 49 | + | ||
| 46 | 50 | /** | |
| 47 | 51 | * Causes side-effects: user-defined loader hooks are added to esmLoader. | |
| 48 | 52 | * @returns {void} | |
| 49 | 53 | */ | |
| 50 | 54 | async function initializeLoader() { | |
| 55 | + if (isESMInitialized) { return; } | ||
| 56 | + | ||
| 51 | 57 | const { getOptionValue } = require('internal/options'); | |
| 52 | 58 | const customLoaders = getOptionValue('--experimental-loader'); | |
| 53 | 59 | ||
@@ -75,6 +81,8 @@ async function initializeLoader() { | |||
| 75 | 81 | // Hooks must then be added to external/public loader | |
| 76 | 82 | // (so they're triggered in userland) | |
| 77 | 83 | await esmLoader.addCustomLoaders(keyedExportsList); | |
| 84 | + | ||
| 85 | + isESMInitialized = true; | ||
| 78 | 86 | } | |
| 79 | 87 | ||
| 80 | 88 | exports.loadESM = async function loadESM(callback) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,30 @@ | |||
| 1 | + import '../common/index.mjs'; | ||
| 2 | + import * as fixtures from '../common/fixtures.mjs'; | ||
| 3 | + import assert from 'node:assert'; | ||
| 4 | + import { spawnSync } from 'node:child_process'; | ||
| 5 | + | ||
| 6 | + | ||
| 7 | + { // Verify unadulterated source is loaded when there are no loaders | ||
| 8 | + const { status, stderr, stdout } = spawnSync( | ||
| 9 | + process.execPath, | ||
| 10 | + [ | ||
| 11 | + '--loader', | ||
| 12 | + fixtures.fileURL('es-module-loaders', 'loader-resolve-passthru.mjs'), | ||
| 13 | + '--no-warnings', | ||
| 14 | + fixtures.path('es-modules', 'runmain.mjs'), | ||
| 15 | + ], | ||
| 16 | + { encoding: 'utf8' }, | ||
| 17 | + ); | ||
| 18 | + | ||
| 19 | + // Length minus 1 because the first match is the needle. | ||
| 20 | + const resolveHookRunCount = (stdout.match(/resolve passthru/g)?.length ?? 0) - 1; | ||
| 21 | + | ||
| 22 | + assert.strictEqual(stderr, ''); | ||
| 23 | + /** | ||
| 24 | + * resolveHookRunCount = 2: | ||
| 25 | + * 1. fixtures/…/runmain.mjs | ||
| 26 | + * 2. node:module (imported by fixtures/…/runmain.mjs) | ||
| 27 | + */ | ||
| 28 | + assert.strictEqual(resolveHookRunCount, 2); | ||
| 29 | + assert.strictEqual(status, 0); | ||
| 30 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,7 @@ | |||
| 1 | + import { runMain } from 'node:module'; | ||
| 2 | + | ||
| 3 | + try { await import.meta.resolve('doesnt-matter.mjs') } catch {} | ||
| 4 | + | ||
| 5 | + runMain(); | ||
| 6 | + | ||
| 7 | + try { await import.meta.resolve('doesnt-matter.mjs') } catch {} | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments