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

[tree-shaking bug] sideEffects doesn't seem to be working correctly #7635

Closed
7 tasks done
pd4d10 opened this issue Apr 7, 2022 · 10 comments · Fixed by rollup/rollup#4867
Closed
7 tasks done

[tree-shaking bug] sideEffects doesn't seem to be working correctly #7635

pd4d10 opened this issue Apr 7, 2022 · 10 comments · Fixed by rollup/rollup#4867
Labels
bug: upstream Bug in a dependency of Vite inconsistency Inconsistency between dev & build p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@pd4d10
Copy link
Contributor

pd4d10 commented Apr 7, 2022

Describe the bug

When importing the package lowlight, some parts of the code in files listed at sideEffects are removed incorrectly:

https://github.com/wooorm/lowlight/blob/0803fc3416cbdf59f204da243313e3315eb34617/lib/common.js#L1-L36

Reproduce steps

  1. Open the reproduction link below
  2. run npm run build
  3. Check if the JS output has these highlight.js languages code bundled in

Reproduction

https://stackblitz.com/edit/vitejs-vite-zyomf8?file=main.js

System Info

It has been reproduced at Stackblitz. So ignore this part.

Used Package Manager

npm

Logs

No response

Validations

@pd4d10
Copy link
Contributor Author

pd4d10 commented Apr 7, 2022

Additional information

These lines of code may be related:

const { sideEffects } = data
let hasSideEffects: (id: string) => boolean
if (typeof sideEffects === 'boolean') {
hasSideEffects = () => sideEffects
} else if (Array.isArray(sideEffects)) {
hasSideEffects = createFilter(sideEffects, null, { resolve: pkgDir })
} else {
hasSideEffects = () => true
}

But it seems to have done the right thing.

@sapphi-red
Copy link
Member

I think this behavior is correct.

  1. lowlight has index.js in main field.
  2. index.js has export {lowlight} from './lib/common.js'.
  3. lib/common.js includes imports from highlight.js and uses them.
  4. Also sideEffects field declares that lib/common.js has side-effect.

If you intend not to include highlight.js languages automatically I think you should import lowlight/lib/core.js instead of lowlight as it is written here.
https://github.com/wooorm/lowlight/tree/main#what-is-this

@pd4d10
Copy link
Contributor Author

pd4d10 commented Apr 7, 2022

If you intend not to include highlight.js languages automatically

On the contrary, we intend to include them. The problem is they are removed unexpectedly

@sapphi-red
Copy link
Member

Sorry I misread it.

@sapphi-red
Copy link
Member

I have found that changing either of below will make it not occur.

  1. change import {lowlight} from 'lowlight' to import {lowlight} from 'lowlight/lib/common'

  2. change export {lowlight} from './lib/common' to

    import {lowlight} from './lib/common'
    export {lowlight}

So I think it is related to re-exporting.

@sapphi-red
Copy link
Member

I was able to reproduct only with rollup.
Maybe it is a bug in rollup?
https://stackblitz.com/edit/node-lr98tg

@pd4d10
Copy link
Contributor Author

pd4d10 commented Apr 7, 2022

Thanks for investigating it!

I'm not sure if Rollup respects the sideEffects field. According to the code snippet in my previous comment here #7635 (comment), it seems that Vite has some specific logics?

@sapphi-red
Copy link
Member

@lukastaegert
Copy link

Fix at rollup/rollup#4867

@sapphi-red
Copy link
Member

Thanks!

Closing as rollup 3.17.2 that includes the fix was released.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite inconsistency Inconsistency between dev & build p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants