Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(esm loader): support Node 18.6 #15730

Merged
merged 1 commit into from Jul 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/playwright-test/src/experimentalLoader.ts
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down