From 09830cf8a90e4ca64842c79697fcc91f491721d3 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Fri, 15 Jul 2022 15:00:09 -0700 Subject: [PATCH] fix(esm loader): support Node 18.6 --- packages/playwright-test/src/experimentalLoader.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/playwright-test/src/experimentalLoader.ts b/packages/playwright-test/src/experimentalLoader.ts index 3a033e05c8c87..5e81d9fd4e151 100644 --- a/packages/playwright-test/src/experimentalLoader.ts +++ b/packages/playwright-test/src/experimentalLoader.ts @@ -18,7 +18,9 @@ import fs from 'fs'; import url from 'url'; import { transformHook, resolveHook } from './transform'; -async function resolve(specifier: string, context: { parentURL: string }, defaultResolve: any) { +// Node < 18.6: defaultResolve takes 3 arguments. +// Node >= 18.6: nextResolve from the chain takes 2 arguments. +async function resolve(specifier: string, context: { parentURL?: string }, defaultResolve: Function) { if (context.parentURL && context.parentURL.startsWith('file://')) { const filename = url.fileURLToPath(context.parentURL); const resolved = resolveHook(filename, specifier); @@ -28,12 +30,15 @@ async function resolve(specifier: string, context: { parentURL: string }, defaul return defaultResolve(specifier, context, defaultResolve); } -async function load(moduleUrl: string, context: any, defaultLoad: any) { +// Node < 18.6: defaultLoad takes 3 arguments. +// Node >= 18.6: nextLoad from the chain takes 2 arguments. +async function load(moduleUrl: string, context: any, defaultLoad: Function) { if (moduleUrl.startsWith('file://') && (moduleUrl.endsWith('.ts') || moduleUrl.endsWith('.tsx'))) { const filename = url.fileURLToPath(moduleUrl); const code = fs.readFileSync(filename, 'utf-8'); const source = transformHook(code, filename, moduleUrl); - return { format: 'module', source }; + // shortCurcuit is required by Node >= 18.6 to designate no more loaders should be called. + return { format: 'module', source, shortCircuit: true }; } return defaultLoad(moduleUrl, context, defaultLoad); }