question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

webpack 4 tree-shaking style sideEffects: Fix

See original GitHub issue

What problem does this feature solve?

Essentially it’s an instruction to the vue-loader to import and use the style from the same file (recursively). The resulting value of style is {}, but it **tricks webpack into thinking the style in this file is not a side-effect.

What does the proposed API look like?

– file: Palette.vue —

<script>
import style from './Palette.vue?vue&type=style&index=0&module=true&lang=stylus&'

export default {
   style,
   ...
}
</script>
<style scoped lang="stylus">
...
</style>

It could be possible to append a similar import to the front of the script section automatically in the vue-loader, thereby making the style mandatory .

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Ruandoucommented, Aug 21, 2021

How would you apply sideEffects: true to css? like, there is no file with the extension *.css, right? only the .vue 🤔

https://github.com/vuejs/vue-loader/issues/1435#issuecomment-869074949

1reaction
thelonecabbagecommented, Apr 2, 2019

It is the same issue reported in: https://github.com/vuejs/vue-loader/issues/1435 https://github.com/vuejs/vue-loader/issues/1295 And documented at the bottom of this page: https://vue-loader.vuejs.org/guide/#manual-setup

General: The problem is if you are writing libraries, and exposing SFCs the CSS is not included in the resulting tree-shaken result. Despite what the docs above say CSS included in a .vue file SHOULD NOT be side effects.

It can be “fixed”

  • by setting sideEffects: true,
  • for *.css, *.vue

but this results i turning off tree shaking for the larger part of the library

Another solution is to import each .vue file individually, instead of exporting them as named exports. But this makes using the library more difficult and clunky.

The PR I posted isn’t ideal. But it does demonstrate one way to register the styles as mandatory only when their associated VUE files get used.

If there is a better way to attach the <style>s in a vue file to the export default script object then it would be great. (maybe in a way that is similar to the templates?)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tree Shaking - webpack
Clarifying tree shaking and sideEffects ... The sideEffects and usedExports (more known as tree shaking) optimizations are two different things. sideEffects is ...
Read more >
Tree shaking and code splitting in webpack - LogRocket Blog
Here, we'll explain tree shaking and code splitting in webpack and discuss how to combine them for the most optimal bundle possible.
Read more >
How to Fully Optimize Webpack 4 Tree Shaking | by Craig Miller
A file with side effects should NOT be tree-shaken, as this will damage the application as a whole. The designers of Webpack, ...
Read more >
How I fixed webpack tree shaking in 3 easy steps
Tree shaking is an important step in Webpack bundling to get rid of unused stuff. This should help describe how to easily get...
Read more >
Tree Shaking - SurviveJS
For tools like webpack to allow tree shake npm packages, you should generate a build that has transpiled everything else except the ES2015...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found