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/rollup tree shakes lib/common.js #3827

Closed
Trinovantes opened this issue Jul 26, 2023 · 2 comments · Fixed by #3828
Closed

vite/rollup tree shakes lib/common.js #3827

Trinovantes opened this issue Jul 26, 2023 · 2 comments · Fixed by #3828
Labels
bug help welcome Could use help from community parser

Comments

@Trinovantes
Copy link
Contributor

Describe the issue/behavior that seems buggy

I investigated a few issues (e.g. highlightjs/vue-plugin#44) reported in the vue-plugin repo where production builds are stripping out import 'highlight.js/lib/common'; side effect imports

Turns out vite (which uses rollup internally) tree shakes side effect imports if the files themselves are not marked as side effect in package.json

Sample Code or Instructions to Reproduce

npm create vite@latest
npm install highlight.js

Then inside src/main.ts, add:

import 'highlight.js/lib/common'
import hljs from 'highlight.js/lib/core'
console.log(hljs.listLanguages())

In debug builds npm run dev, it should list all languages registered inside highlight.js/lib/common

However in production builds npm run build && npm run preview, it will print an empty array.

Expected behavior

Should print all languages in both debug/production builds

Additional context

This package.json should be updated to:

  "sideEffects": [
    "./es/common.js",
    "./lib/common.js",
    "*.css",
    "*.scss"
  ],
@joshgoebel
Copy link
Member

What if you instead do:

import hljs from 'highlight.js/lib/common'

Does that work?

@Trinovantes
Copy link
Contributor Author

That works as well

However, it'll only work as long as the hljs variable is used/referenced the current file. I still think it's a good idea to mark that file as side effect to prevent production builds from "randomly" breaking if that variable ever stops being referenced.

joshgoebel pushed a commit that referenced this issue Aug 4, 2023
vite/rollup will treeshake side effect imports (whereas webpack will not) if the files themselves are not marked in package.json

Resolves #3827
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help welcome Could use help from community parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants