-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR-URL: #39240 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]>
- Loading branch information
1 parent
341a999
commit a554821
Showing
8 changed files
with
425 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
const { getOptionValue } = require('internal/options'); | ||
const experimentalImportMetaResolve = | ||
getOptionValue('--experimental-import-meta-resolve'); | ||
const { PromisePrototypeThen, PromiseReject } = primordials; | ||
const asyncESM = require('internal/process/esm_loader'); | ||
|
||
function createImportMetaResolve(defaultParentUrl) { | ||
return async function resolve(specifier, parentUrl = defaultParentUrl) { | ||
return PromisePrototypeThen( | ||
asyncESM.esmLoader.resolve(specifier, parentUrl), | ||
({ url }) => url, | ||
(error) => ( | ||
error.code === 'ERR_UNSUPPORTED_DIR_IMPORT' ? | ||
error.url : PromiseReject(error)) | ||
); | ||
}; | ||
} | ||
|
||
function initializeImportMeta(meta, context) { | ||
const url = context.url; | ||
|
||
// Alphabetical | ||
if (experimentalImportMetaResolve) | ||
meta.resolve = createImportMetaResolve(url); | ||
meta.url = url; | ||
} | ||
|
||
module.exports = { | ||
initializeImportMeta | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Flags: --loader ./test/fixtures/es-module-loaders/mock-loader.mjs | ||
import '../common/index.mjs'; | ||
import assert from 'assert/strict'; | ||
|
||
// This is provided by test/fixtures/es-module-loaders/mock-loader.mjs | ||
import mock from 'node:mock'; | ||
|
||
mock('node:events', { | ||
EventEmitter: 'This is mocked!' | ||
}); | ||
|
||
// This resolves to node:events | ||
// It is intercepted by mock-loader and doesn't return the normal value | ||
assert.deepStrictEqual(await import('events'), Object.defineProperty({ | ||
__proto__: null, | ||
EventEmitter: 'This is mocked!' | ||
}, Symbol.toStringTag, { | ||
enumerable: false, | ||
value: 'Module' | ||
})); | ||
|
||
const mutator = mock('node:events', { | ||
EventEmitter: 'This is mocked v2!' | ||
}); | ||
|
||
// It is intercepted by mock-loader and doesn't return the normal value. | ||
// This is resolved separately from the import above since the specifiers | ||
// are different. | ||
const mockedV2 = await import('node:events'); | ||
assert.deepStrictEqual(mockedV2, Object.defineProperty({ | ||
__proto__: null, | ||
EventEmitter: 'This is mocked v2!' | ||
}, Symbol.toStringTag, { | ||
enumerable: false, | ||
value: 'Module' | ||
})); | ||
|
||
mutator.EventEmitter = 'This is mocked v3!'; | ||
assert.deepStrictEqual(mockedV2, Object.defineProperty({ | ||
__proto__: null, | ||
EventEmitter: 'This is mocked v3!' | ||
}, Symbol.toStringTag, { | ||
enumerable: false, | ||
value: 'Module' | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.