Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Jinjiang opened this issue Aug 25, 2023 · 1 comment · Fixed by #35
Closed

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

Jinjiang opened this issue Aug 25, 2023 · 1 comment · Fixed by #35

Comments

@Jinjiang
Copy link
Collaborator

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.

@Jinjiang
Copy link
Collaborator Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants