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

namespace import would be transformed to void 0 #15553

Closed
7 tasks done
maelon opened this issue Jan 10, 2024 · 10 comments
Closed
7 tasks done

namespace import would be transformed to void 0 #15553

maelon opened this issue Jan 10, 2024 · 10 comments

Comments

@maelon
Copy link

maelon commented Jan 10, 2024

Describe the bug

image My project need antd v3,but console report this error when my vite devserve runs in development. image The antd source code path is node_modules/antd/es/local-provider/index.js. image The transformed code is 'void 0'

Reproduction

https://stackblitz.com/edit/vitejs-vite-jdrder

Steps to reproduce

No response

System Info

System:
    OS: macOS 11.6.7
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 87.24 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 18.19.0 - ~/.nvm/versions/node/v18.19.0/bin/node
    Yarn: 1.22.21 - ~/.nvm/versions/node/v18.19.0/bin/yarn
    pnpm: 8.10.5 - ~/.nvm/versions/node/v18.19.0/bin/pnpm
    Watchman: 2023.09.04.00 - /usr/local/bin/watchman
  Browsers:
    Chrome: 120.0.6099.199
    Edge: 120.0.2210.121
    Safari: 15.5
  npmPackages:
    @vitejs/plugin-legacy: ^5.2.0 => 5.2.0 
    @vitejs/plugin-react: ^4.2.1 => 4.2.1 
    vite: ^5.0.11 => 5.0.11

Used Package Manager

npm

Logs

No response

Validations

Copy link

stackblitz bot commented Jan 10, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@maelon maelon changed the title import start would be transformed to void 0 namespace import would be transformed to void 0 Jan 10, 2024
@bluwy
Copy link
Member

bluwy commented Jan 10, 2024

The repro isn't working correctly for me, but this seems similar to #15542

@maelon
Copy link
Author

maelon commented Jan 11, 2024

The repro isn't working correctly for me, but this seems similar to #15542

This case is default in namespace import,moment be transformed to { default: ... },so moment.locale be transformed to void 0

@maelon
Copy link
Author

maelon commented Jan 11, 2024

I find the answer, config add this option mainFields: ['browser', 'module'],because moment uses jsnext

@XiSenao
Copy link
Collaborator

XiSenao commented Jan 18, 2024

It seems to be related to the interop of esbuild. In the pre-build phase, vite confirms the ESM module of moment through the jsnext:main field. esbuild loads esm modules using namespace import and obtains values are all { default: module }. If mainFields: ['main'] is specified, it will change vite's behavior and obtain moment's CJS module. esbuild loads moment (CJS) modules in ant (ESM) module through namespace import, which will additionally extend object(including Array) properties, so moment.locale can be used normally.

Explanation:
Specifying mainFields: ['browser', 'module'] may not have an effect on the moment module. Finally, it will fallback to the main field.

@NhanHoang8195
Copy link

NhanHoang8195 commented Jan 18, 2024

I find the answer, config add this option mainFields: ['browser', 'module'],because moment uses jsnext

I have similar problem with moment library.

I import the moment library like this

import moment, {parseZone} from 'moment',

I got an error in start dev server and build production.

It raised an error: RollupError: "parseZone" is not exported by "node_modules/moment/dist/moment.js"

I tried your solution and it works now.

FYI @XiSenao

@XiSenao
Copy link
Collaborator

XiSenao commented Jan 18, 2024

Thank you for your feedback!

This is because the esm module of moment only provides default exports, so using named imports in vite will cause exceptions. As for the feasible reason you mentioned, it actually guides vite to load moment's CJS module, and there will be interop between vite and pre-built artifacts. I think this seems to be a problem with the package itself rather than an issue with vite.

@NhanHoang8195
Copy link

Thanks for your explanation.

But with the same code, I ran with webpack (from CRA) and it worked normally, so I think that there may be a problem from vite transformation.

@XiSenao
Copy link
Collaborator

XiSenao commented Jan 19, 2024

Because webpack loads the CJS module of moment, it is similar to setting resolve.mainFields = ['main'] in vite configuration. The product corresponding to the main field of moment is fine. What I want to point out is that the ESM product corresponding to the jsnext:main field of moment does not seem to be compliant.

@XiSenao
Copy link
Collaborator

XiSenao commented Jan 29, 2024

I will temporarily close this issue. The esm module corresponding to the jsnext:main field of moment only provides default exports, so when imported using namespace import, the object structure is { default: module }. If you feel that this issue still needs to be reopened and valuable information provided, I will reopen it.

@XiSenao XiSenao closed this as completed Jan 29, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Feb 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants