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

Code before:

  const filename = url.fileURLToPath(moduleUrl);
  // 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 during import() are source-mapped.
  if (transformed.serializedCache) {
    if (legacyWaitForSourceMaps)
      await transport?.send('pushToCompilationCache', { cache: transformed.serializedCache });
    else
      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: transformed.code,
    shortCircuit: true,
  };
}

let transport: PortTransport | undefined;
let legacyWaitForSourceMaps = false;

function initialize(data: { port: MessagePort }) {
  transport = createTransport(data?.port);
  legacyWaitForSourceMaps = !!process.env.PLAYWRIGHT_WAIT_FOR_SOURCE_MAPS;
}

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

    if (method === 'setTransformConfig') {
      setTransformConfig(params.config);
      return;
    }

    if (method === 'addToCompilationCache') {
      addToCompilationCache(params.cache);
      return;
    }


Review comment (suggestion):
```suggestion
    source: originalModuleUrl.endsWith(esmPreflightExtension) ? `` : transformed.code,
```

Code after:
  const moduleUrl = originalModuleUrl.replace(esmPreflightExtension, '');
  // 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);
  // 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: originalModuleUrl.endsWith(esmPreflightExtension) ? `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;
    }

    if (method === 'setTransformConfig') {
      setTransformConfig(params.config);
      return;
    }

    if (method === 'addToCompilationCache') {
      addToCompilationCache(params.cache);
      return;