Repository: microsoft/playwright
File: packages/playwright/src/transform/esmLoader.ts (TypeScript)

Code before:
  // Note: we collect dependencies here that will be sent to the main thread
  // (and optionally runner process) after the loading finishes.
  if (result?.url && result.url.startsWith('file://'))
    currentFileDepsCollector()?.add(url.fileURLToPath(result.url));

  if (originalSpecifier.endsWith(esmPreflightExtension))
    result.url = result.url + esmPreflightExtension;
  return result;
}

// non-js files have undefined
// some js files have null
// {module/commonjs}-typescript are changed to {module,commonjs} because we handle typescript ourselves
const kSupportedFormats = new Map([
  ['commonjs', 'commonjs'],
  ['module', 'module'],
  ['commonjs-typescript', 'commonjs'],
  ['module-typescript', 'module'],
  [null, null],
  [undefined, undefined]
]);

// Node < 18.6: defaultLoad takes 3 arguments.
// Node >= 18.6: nextLoad from the chain takes 2 arguments.
async function load(originalModuleUrl: string, context: { format?: string }, defaultLoad: Function) {
  const isPreflight = originalModuleUrl.endsWith(esmPreflightExtension);
  const moduleUrl = originalModuleUrl.replace(esmPreflightExtension, '');

  const bail =
    !kSupportedFormats.has(context.format) || // Bail out for wasm, json, etc.
    !moduleUrl.startsWith('file://');         // Bail out for built-in modules

  if (!bail) {
    const filename = url.fileURLToPath(moduleUrl);
    // Bail for node_modules.
    if (shouldTransform(filename)) {
      const code = fs.readFileSync(filename, 'utf-8');
      const transformed = transformHook(code, filename, moduleUrl);

      // Flush the source maps to the main thread, so that errors after import() are source-mapped.
      if (transformed.serializedCache)
        transport?.post('pushToCompilationCache', { cache: transformed.serializedCache });

      if (!isPreflight) {
        // Output format is required, so we determine it manually when unknown.
        // shortCircuit is required by Node >= 18.6 to designate no more loaders should be called.
        return {
          format: kSupportedFormats.get(context.format) || (fileIsModule(filename) ? 'module' : 'commonjs'),
          source: transformed.code,
          shortCircuit: true,
        };

Review comment (suggestion):
Let me offer an alternative as I think this change is somewhat missing the point. The like 65 in the old code is offender, it needs to go.

Code after:
  // Note: we collect dependencies here that will be sent to the main thread
  // (and optionally runner process) after the loading finishes.
  if (result?.url && result.url.startsWith('file://'))
    currentFileDepsCollector()?.add(url.fileURLToPath(result.url));

  if (originalSpecifier.endsWith(esmPreflightExtension))
    result.url = result.url + esmPreflightExtension;
  return result;
}

// non-js files have undefined
// some js files have null
// {module/commonjs}-typescript are changed to {module,commonjs} because we handle typescript ourselves
const kSupportedFormats = new Map([
  ['commonjs', 'commonjs'],
  ['module', 'module'],
  ['commonjs-typescript', 'commonjs'],
  ['module-typescript', 'module'],
  [null, null],
  [undefined, undefined]
]);

// Node < 18.6: defaultLoad takes 3 arguments.
// Node >= 18.6: nextLoad from the chain takes 2 arguments.
async function load(moduleUrl: string, context: { format?: string }, defaultLoad: Function) {
  // Bail out for wasm, json, etc.
  if (!kSupportedFormats.has(context.format))
    return defaultLoad(moduleUrl, context, defaultLoad);

  // Bail for built-in modules.
  if (!moduleUrl.startsWith('file://'))
    return defaultLoad(moduleUrl, context, defaultLoad);

  const filename = url.fileURLToPath(moduleUrl);
  const isPreflight = moduleUrl.endsWith(esmPreflightExtension);

  // Bail for node_modules.
  if (!shouldTransform(filename))
    return defaultLoad(moduleUrl, context, defaultLoad);

  const originalModuleUrl = isPreflight ? moduleUrl.slice(0, -esmPreflightExtension.length) : moduleUrl;
  const originalFilename = isPreflight ? url.fileURLToPath(originalModuleUrl) : filename;

  const code = fs.readFileSync(originalFilename, 'utf-8');
  const transformed = transformHook(code, originalFilename, originalModuleUrl);

  // Flush the source maps to the main thread, so that errors after import() are source-mapped.
  if (transformed.serializedCache)
    transport?.post('pushToCompilationCache', { cache: transformed.serializedCache });

  // Output format is required, so we determine it manually when unknown.