Repository: vitejs/vite
File: packages/vite/src/node/optimizer/esbuildDepPlugin.ts (TypeScript)

Code before:

      build.onResolve({ filter }, (args) => {
        // preserve `require` for node because it's more accurate than converting it to import
        if (args.kind === 'require-call' && platform !== 'node') {
          return {
            path: args.path,
            namespace: cjsExternalFacadeNamespace,
          }
        }

        return {
          path: args.path,
          external: true,
        }
      })

      build.onLoad(
        { filter: /.*/, namespace: cjsExternalFacadeNamespace },
        (args) => ({
          contents: `\
import * as m from ${JSON.stringify(nonFacadePrefix + args.path)};
if (typeof m.default === 'function' || (typeof m.default === 'object' && m.default !== null)) {
  module.exports = Object.assign(m.default, m);
} else {
  module.exports = { ...m };
}`,
        }),
      )
    },
  }
}


Review comment (suggestion):
The current behavior is aligned with [`requireReturnsDefault: false` of the commonjs plugin](https://github.com/rollup/plugins/blob/e1a5ef99f1578eb38a8c87563cb9651db228f3bd/packages/commonjs/README.md#requirereturnsdefault). I'm a bit worried about breaking users that rely on the current behavior.

I think we can have a special handling for node builtin modules here. I guess that would probably cover most cases.
This code would work for that.
```js
import * as m from ${JSON.stringify(nonFacadePrefix + args.path)};
if (typeof m.default === 'function') {
  module.exports = m.default;
} else {
  module.exports = { ...m };
}
```
I confirmed this covers Node's builtin modules with the following script.
```js
import module from 'node:module'

const require = module.createRequire(import.meta.url)

for (const mod of module.builtinModules) {
  const esmExport = await import(mod)
  const moduleExports = require(mod)

  if (typeof esmExport.default === 'function') {
    console.assert(esmExport.default === moduleExports, mod)
  } else {
    const esmList = Object.keys(esmExport).filter((key) => key !== 'default')
    const cjsList = Object.keys(moduleExports)
    const diffOnlyEsm = esmList.filter((key) => !cjsList.includes(key))
    const diffOnlyCjs = cjsList.filter((key) => !esmList.includes(key))
    console.assert(
      diffOnlyEsm.length === 0 && diffOnlyCjs.length === 0,
      mod,
      diffOnlyEsm
        .map((key) => `+ ${key}`)
        .concat(diffOnlyCjs.map((key) => `- ${key}`)),
    )
  }
}
```

Code after:
        }
      })

      build.onResolve({ filter }, (args) => {
        // preserve `require` for node because it's more accurate than converting it to import
        if (args.kind === 'require-call' && platform !== 'node') {
          return {
            path: args.path,
            namespace: cjsExternalFacadeNamespace,
          }
        }

        return {
          path: args.path,
          external: true,
        }
      })

      build.onLoad(
        { filter: /.*/, namespace: cjsExternalFacadeNamespace },
        (args) => ({
          contents: `\
import * as m from ${JSON.stringify(nonFacadePrefix + args.path)};
module.exports = ${isNodeBuiltin(args.path) ? 'm.default' : '{ ...m }'};
`,
        }),
      )
    },
  }
}