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

Code before:
  [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, '');

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

  const filename = url.fileURLToPath(moduleUrl);
  if (!isPreflight) {
    // Bail out for wasm, json, etc.
    if (!kSupportedFormats.has(context.format))
      return defaultLoad(moduleUrl, context, defaultLoad);

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

  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 });

  // 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: isPreflight ? `void 0;` : transformed.code,
    shortCircuit: true,
  };
}

let transport: PortTransport | undefined;

function initialize(data: { port: MessagePort }) {
  transport = createTransport(data?.port);
}

function createTransport(port: MessagePort) {
  return new PortTransport(port, async (method, params) => {
    if (method === 'setSingleTSConfig') {
      setSingleTSConfig(params.tsconfig);
      return;

Review comment (suggestion):
I'd skip this for preflights that we want to bail on, e.g. from node_modules. Since we are not supposed to transform them, we don't need to prepopulate source mapes.

I'd make a custom `defaultLoad` function that would return `void 0` for preflights or call the actual `defaultLoad` for non-preflights.

Code after:
  [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.
  // shortCircuit is required by Node >= 18.6 to designate no more loaders should be called.
  return {
    format: kSupportedFormats.get(context.format) || (fileIsModule(originalFilename) ? 'module' : 'commonjs'),
    source: isPreflight ? `void 0;` : transformed.code,
    shortCircuit: true,
  };
}

let transport: PortTransport | undefined;

function initialize(data: { port: MessagePort }) {
  transport = createTransport(data?.port);
}

function createTransport(port: MessagePort) {
  return new PortTransport(port, async (method, params) => {
    if (method === 'setSingleTSConfig') {
      setSingleTSConfig(params.tsconfig);
      return;