GeezerVison is showing github.com — esm: refactor test-esm-loader-resolve-type · nodejs/node@0875867
File treeExpand file treeCollapse file tree Open diff view settings fixtures/es-module-loaders
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-loader-resolve-type.mjs +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 | { recursive: true } | 17 | 17 | ); | 18 | 18 |
| 19 | | - deepStrictEqual(await spawnPromisified( | | 19 | + const output = await spawnPromisified( | 20 | 20 | execPath, | 21 | 21 | [ | 22 | 22 | '--no-warnings', | @@ -29,15 +29,17 @@ try { | 29 | 29 | console.log(JSON.stringify({ before, after }));`, | 30 | 30 | ], | 31 | 31 | { cwd: base }, | 32 | | - ), { | | 32 | + ); | | 33 | + | | 34 | + deepStrictEqual(output, { | | 35 | + code: 0, | | 36 | + signal: null, | 33 | 37 | stderr: '', | 34 | 38 | stdout: JSON.stringify({ | 35 | 39 | before: { importedESM: 0, importedCJS: 0 }, | 36 | 40 | // Dynamic import in the eval script should increment ESM counter but not CJS counter | 37 | 41 | after: { importedESM: 1, importedCJS: 0 }, | 38 | 42 | }) + '\n', | 39 | | - code: 0, | 40 | | - signal: null, | 41 | 43 | }); | 42 | 44 | } finally { | 43 | 45 | await rm(base, { recursive: true, force: true }); | |
Collapse file Copy file name to clipboard +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/** @type {MessagePort} */ | | 2 | +let port; | | 3 | +export function initialize(data) { | | 4 | + port = data.port; | | 5 | +} | | 6 | + | | 7 | +export async function resolve(specifier, context, next) { | | 8 | + const nextResult = await next(specifier, context); | | 9 | + const { format } = nextResult; | | 10 | + | | 11 | + if (format === 'module' || specifier.endsWith('.mjs')) { | | 12 | + port.postMessage({ type: 'module' }); | | 13 | + } else if (format == null || format === 'commonjs') { | | 14 | + port.postMessage({ type: 'commonjs' }); | | 15 | + } | | 16 | + | | 17 | + return nextResult; | | 18 | +} |
Collapse file Copy file name to clipboard +23-37Lines changed: 23 additions & 37 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import * as fixtures from '../../common/fixtures.mjs'; | | 2 | +import { register } from 'node:module'; | | 3 | +import { MessageChannel } from 'node:worker_threads'; | | 4 | + | 1 | 5 | let importedESM = 0; | 2 | 6 | let importedCJS = 0; | | 7 | +export function getModuleTypeStats() { | | 8 | + return { importedESM, importedCJS }; | | 9 | +}; | 3 | 10 |
| 4 | | -export function globalPreload({ port }) { | 5 | | - port.on('message', (int32) => { | 6 | | - port.postMessage({ importedESM, importedCJS }); | 7 | | - Atomics.store(int32, 0, 1); | 8 | | - Atomics.notify(int32, 0); | 9 | | - }); | 10 | | - port.unref(); | 11 | | - return ` | 12 | | - const { receiveMessageOnPort } = getBuiltin('worker_threads'); | 13 | | - global.getModuleTypeStats = async function getModuleTypeStats() { | 14 | | - const sab = new SharedArrayBuffer(4); | 15 | | - const int32 = new Int32Array(sab); | 16 | | - port.postMessage(int32); | 17 | | - // Artificial timeout to keep the event loop alive. | 18 | | - // https://bugs.chromium.org/p/v8/issues/detail?id=13238 | 19 | | - // TODO(targos) Remove when V8 issue is resolved. | 20 | | - const timeout = setTimeout(() => { throw new Error('timeout'); }, 1_000); | 21 | | - await Atomics.waitAsync(int32, 0, 0).value; | 22 | | - clearTimeout(timeout); | 23 | | - return receiveMessageOnPort(port).message; | 24 | | - }; | 25 | | - `; | 26 | | -} | 27 | | - | 28 | | -export async function load(url, context, next) { | 29 | | - return next(url); | 30 | | -} | | 11 | +const { port1, port2 } = new MessageChannel(); | 31 | 12 |
| 32 | | -export async function resolve(specifier, context, next) { | 33 | | - const nextResult = await next(specifier, context); | 34 | | - const { format } = nextResult; | | 13 | +register(fixtures.fileURL('es-module-loaders/hook-resolve-type-loader.mjs'), { | | 14 | + data: { port: port2 }, | | 15 | + transferList: [port2], | | 16 | +}); | 35 | 17 |
| 36 | | - if (format === 'module' || specifier.endsWith('.mjs')) { | 37 | | - importedESM++; | 38 | | - } else if (format == null || format === 'commonjs') { | 39 | | - importedCJS++; | | 18 | +port1.on('message', ({ type }) => { | | 19 | + switch (type) { | | 20 | + case 'module': | | 21 | + importedESM++; | | 22 | + break; | | 23 | + case 'commonjs': | | 24 | + importedCJS++; | | 25 | + break; | 40 | 26 | } | | 27 | +}); | 41 | 28 |
| 42 | | - return nextResult; | 43 | | -} | 44 | | - | | 29 | +port1.unref(); | | 30 | +port2.unref(); |
|