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

[Vite 3] CJS Bug #8880

Closed
7 tasks done
brillout opened this issue Jul 1, 2022 · 12 comments
Closed
7 tasks done

[Vite 3] CJS Bug #8880

brillout opened this issue Jul 1, 2022 · 12 comments
Labels
p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)

Comments

@brillout
Copy link
Contributor

brillout commented Jul 1, 2022

Describe the bug

In some scenarios Vite seems to not be able to handle CJS dependencies.

This only happens with Vite 3.

Reproduction

https://github.com/brillout/vite-3-cjs-bug

Additional Context

vikepress includes .ts, .css, and .svg files: that's because Node.js cannot handle import './someStyles.css' nor import emojiUrl from './someEmoji.svg', which means that vikepress cannot be externalized. That's why vikepress publishes its source files to npm.

Note that including source files is also a common practice in SvelteKit's ecosystem.

System Info

System:
    OS: Linux 5.10 Debian GNU/Linux 10 (buster) 10 (buster)
    CPU: (2) x64 Intel(R) Celeron(R) N4020 CPU @ 1.10GHz
    Memory: 509.13 MB / 2.71 GB
    Container: Yes
    Shell: 5.0.3 - /bin/bash
  Binaries:
    Node: 18.0.0 - ~/.config/nvm/versions/node/v18.0.0/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.6.0 - ~/.config/nvm/versions/node/v18.0.0/bin/npm
  Browsers:
    Firefox: 97.0.1
  npmPackages:
    vite: ^2.8.4 => 3.0.0-beta.5

Used Package Manager

pnpm

Logs

No response

Validations

@brillout brillout mentioned this issue Jul 1, 2022
7 tasks
@sapphi-red sapphi-red added the p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority) label Jul 3, 2022
@brillout
Copy link
Contributor Author

brillout commented Jul 6, 2022

This bug also happens with 3.0.0-beta.7. The reproduction is updated and now uses 3.0.0-beta.7.

@patak-dev
Copy link
Member

Is vikepress a CJS dependency? If that is the case, in SSR it needs to be optimized and then you can't use vite features in it.
If vikepress would be a ESM dep, then you can mark it as noExternal in the same way and it won't be optimized by default, so you can use vite features.

I think that the only way to have a CJS dependency that needs vite features is to set legacy.buildPluginCommonjs so esbuild optimization is disabled. Or you could keep optimization in general, and add @rollup/plugin-commonjs passing an include targeting only vikepress, so the rest of the deps are still bundled more closely to dev.

@brillout
Copy link
Contributor Author

brillout commented Jul 6, 2022

VikePress is ESM. It is vite-plugin-ssr which is CJS; I think the problem here is that Vite doesn't externalize vite-plugin-ssr but it should.

@patak-dev
Copy link
Member

So vite-plugin-ssr is CJS, but it is a pure lib without using Vite features, no?
If vikepress is noExternal and ESM, then it shouldn't be optimized... and it should discover vite-plugin-ssr and externalize it by default. And you even set it manually in vite-plugin-ssr as external.
I'll check what is going on.

@brillout
Copy link
Contributor Author

brillout commented Jul 6, 2022

Yes that's my analysis as well.

@stefanvanherwijnen
Copy link
Contributor

I am having a similar issue where packages, even when defined in noExternal end up in node_modules/.vite/deps_ssr. For me it seems to have started with beta.7.

@patak-dev
Copy link
Member

Packages that are marked as noExternal will end up in node_modules/.vite/deps_ssr if the package is CJS. If it is ESM, by default they shouldn't be there or there is a bug.
But when they are CJS, we need to pre-bundle them if we want to avoid using plugin-commonjs

@stefanvanherwijnen
Copy link
Contributor

Makes sense. The package is not a type: module but does have a module entry. However, I am aliasing it to src directly which has ES syntax, but vite seems to ignore that for ssr and uses the ptebundle instead.

@patak-dev
Copy link
Member

You can try to place it in ssr.optimizeDeps.exclude. If it is aliased though, right now it shouldn't optimize it. Interested in a minimal reproduction if you could create one @stefanvanherwijnen

@stefanvanherwijnen
Copy link
Contributor

Thanks, I didn't know about ssr.optimizeDeps. I'll see if I can reproduce the issue in a MWE.

@patak-dev
Copy link
Member

@stefanvanherwijnen we're thinking about waiting until Vite 4 to enable ssr and build deps optimization so we can release v3 sooner. If you want to test, this PR may be what ends up reaching stable:

@brillout brillout mentioned this issue Jul 7, 2022
7 tasks
@brillout
Copy link
Contributor Author

brillout commented Jul 8, 2022

Fixed in beta.9 🎉.

@brillout brillout closed this as completed Jul 8, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jul 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)
Projects
None yet
Development

No branches or pull requests

4 participants