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

"is not exported" when importing CJS library from worker, but not outside worker #18789

Closed
7 tasks done
jakebailey opened this issue Nov 26, 2024 · 4 comments · Fixed by #18793
Closed
7 tasks done

"is not exported" when importing CJS library from worker, but not outside worker #18789

jakebailey opened this issue Nov 26, 2024 · 4 comments · Fixed by #18793
Labels
p4-important Violate documented behavior or significantly improves performance (priority) regression The issue only appears after a new release

Comments

@jakebailey
Copy link

jakebailey commented Nov 26, 2024

Describe the bug

I tried to upgrade https://github.com/jakebailey/esbuild-playground to vite v6, but hit an issue with a CJS library I use in a worker. It looks like vite (or rollup?) can't find one of the exports, while vite v5 can.

Notably, doing the same import but not in a worker seems to operate correctly. It also works fine in dev mode.

I've created a minimal repro on stackblitz, included below, along with logs that are just in that stackblitz.

Reproduction

https://stackblitz.com/edit/vitejs-vite-cc7pyv?file=src%2Fmain.ts&view=editor

Steps to reproduce

  • npm install
  • npm run build

System Info

System:
    OS: Linux 6.11 Arch Linux
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
    Memory: 10.12 GB / 15.41 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 22.10.0 - /run/user/1000/fnm_multishells/582535_1732647819061/bin/node
    pnpm: 9.12.2 - /run/user/1000/fnm_multishells/582535_1732647819061/bin/pnpm
    bun: 1.1.37 - /usr/bin/bun
  npmPackages:
    @vitejs/plugin-react: ^4.3.3 => 4.3.3 
    vite: ^5.4.10 => 5.4.10

Used Package Manager

npm

Logs

Click to expand!
> vite-typescript-starter@0.0.0 build
> tsc && vite build

vite v6.0.0 building for production...
✓ 7 modules transformed.
x Build failed in 752ms
error during build:
[vite:worker] src/worker.ts (1:9): "createFileSystem" is not exported by "node_modules/wasi-js/dist/index.js", imported by "src/worker.ts".
file: /home/projects/vitejs-vite-cc7pyv/src/worker.ts?worker:1:9

1: import { createFileSystem } from 'wasi-js';
            ^
2: 
3: createFileSystem([

    at getRollupError (file:///home/projects/vitejs-vite-cc7pyv/node_modules/rollup/dist/es/shared/parseAst.js:558:41)
    at Module.error (file:///home/projects/vitejs-vite-cc7pyv/node_modules/rollup/dist/es/shared/parseAst.js:554:42)
    at Module.error (file:///home/projects/vitejs-vite-cc7pyv/node_modules/rollup/dist/es/shared/node-entry.js:15618:29)
    at Module.traceVariable (file:///home/projects/vitejs-vite-cc7pyv/node_modules/rollup/dist/es/shared/node-entry.js:16067:29)
    at ModuleScope.findVariable (file:///home/projects/vitejs-vite-cc7pyv/node_modules/rollup/dist/es/shared/node-entry.js:13850:39)
    at Identifier.bind (file:///home/projects/vitejs-vite-cc7pyv/node_modules/rollup/dist/es/shared/node-entry.js:5096:40)
    at CallExpression.bind (file:///home/projects/vitejs-vite-cc7pyv/node_modules/rollup/dist/es/shared/node-entry.js:2683:23)
    at CallExpression.bind (file:///home/projects/vitejs-vite-cc7pyv/node_modules/rollup/dist/es/shared/node-entry.js:11314:15)
    at ExpressionStatement.bind (file:///home/projects/vitejs-vite-cc7pyv/node_modules/rollup/dist/es/shared/node-entry.js:2683:23)
    at Program.bind (file:///home/projects/vitejs-vite-cc7pyv/node_modules/rollup/dist/es/shared/node-entry.js:2679:28)

Validations

@luisfagottani
Copy link

I had this same issue with an worker as well.

error during build:
[vite:worker-import-meta-url] node_modules/styled-components/dist/styled-components.browser.esm.js (4:12): "useRef" is not exported by "node_modules/react/index.js", imported by "node_modules/styled-components/dist/styled-components.browser.esm.js".
file: ...functions.ts:4:12

@hi-ogawa
Copy link
Collaborator

It looks like this is because perEnvironmentPlugin's applyToEnvironment is not resolved for worker build.

perEnvironmentPlugin('commonjs', (environment) => {

@hi-ogawa hi-ogawa added p4-important Violate documented behavior or significantly improves performance (priority) regression The issue only appears after a new release and removed pending triage labels Nov 27, 2024
@cpojer
Copy link
Contributor

cpojer commented Nov 27, 2024

I'm running into the same issue with the fastpriorityqueue npm package, which is using CJS. When I'm building my client which pulls this file in in a worker, Vite errors with:

error during build:
[vite-plugin-pwa:build] athena/Radius.tsx (1:7): "default" is not exported by "node_modules/.pnpm/fastpriorityqueue@0.7.5/node_modules/fastpriorityqueue/FastPriorityQueue.js", imported by "athena/Radius.tsx".
file: /home/runner/work/athena-crisis-universe/athena-crisis-universe/hera/workers/gameAction.tsx?worker:1:7

The code for this is open source at https://github.com/nkzw-tech/athena-crisis but this particular build step is not. If you'd like to build a repro, I recommend creating a vite.config.js that bundles files in the hera folder. Happy to help.

@jakebailey
Copy link
Author

Seems like this is all good in vite 6.0.1. Thank you!!

@github-actions github-actions bot locked and limited conversation to collaborators Dec 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p4-important Violate documented behavior or significantly improves performance (priority) regression The issue only appears after a new release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants