vite-plugin/vite-plugin-commonjs

Issue Title: Failed to read named export from a cjs package

Closed this issue · 1 comments

I found I can only read the default export from a cjs package.

Reproduction:
https://github.com/Jinjiang/reproductions/tree/vite-plugin-commonjs-20230825

e.g.:

// // it works
// import { a } from 'local-debug/foo'

// it doens't work
const { a } = require('local-debug/foo')

console.log({ a })

Thanks.

After more digging, I found the require() statement would be converted into code like

import __vite__cjsImportX__foo from "foo";
const __CJS__import__X__ = __vite__cjsImportX__foo;
const x = __CJS__import__X__.default || __CJS__import__X__;

which would overwrite the named imports with the default import, and then cause the issue. (If I'm not wrong).

Do you think there is any better ways to support this case?

Thanks.