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.

Spell out tree-shaking, sideEffects: false, and polyfills

See original GitHub issue

📚 Docs or angular.io bug report

Description

To obtain the benefits of webpack tree-shaking, the recommendation is to correctly set the sideEffects: property in package.json files for libraries and apps.

In a recent Angular.io blog post about strict mode and improving build size, it’s recommended that sideEffects: false be set in the app package.json. Also, if you create a new app using ng g app --strict you’ll get this projects/app/src/app/project.json:

{
  "name": "app",
  "private": true,
  "description_1": "This is a special package.json file that is not used by package managers.",
  "description_2": "It is used to tell the tools and bundlers whether the code under this directory is free of code with non-local side-effect. Any code that does have non-local side-effects can't be well optimized (tree-shaken) and will result in unnecessary increased payload size.",
  "description_3": "It should be safe to set this option to 'false' for new applications, but existing code bases could be broken when built with the production config if the application code does contain non-local side-effects that the application depends on.",
  "description_4": "To learn more about this file see: https://angular.io/config/app-package-json.",
  "sideEffects": false
}

My experience with polyfills.ts is that it’s side-effect-ful (by definition it’s side-effect-ful, and it imports zone like so:

import 'zone.js/dist/zone';

Request

Please add documentation clarifying whether polyfills should be listed as a sideEffects file or not, and if not, why.

What’s the affected URL?**

https://angular.io/guide/strict-mode

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
johncrimcommented, May 19, 2022

FYI, my app completely broke when built with --prod only using a package.json with sideEffects: false - it wouldn’t start at all, no errors on the console. Based on my testing, I have found the following, which may be useful in the docs:

  • The impact of a sideEffects: field are only present in --prod mode - so make sure you test in prod (obvious, but worth spelling out for some).
  • The main.ts file is side-effect-ful, meaning you don’t want it tree-shaken. If it is tree-shaken the app won’t start.
  • The polyfills.ts file is also side-effect-ful, so you probably don’t want it tree-shaken. But I haven’t done much testing to verify that it doesn’t work when tree-shaken - so far it seems to work even when tree-shaken, but it could be working b/c it mostly references zone.js, and zone.js doesn’t have "sideEffects": false (I also created this issue for that).
  • If your app package.json is in a subdirectory of the directory containing main.ts and polyfills.ts (and it should also be in a directory containing all your other app typescript files), then you don’t need to exclude them from the “no side-effects” list, so you can use:
   "sideEffects": false
  • If your app package.json is in the directory containing main.ts and polyfills.ts, or in a parent directory of that directory, then you need to mark them as side-effects files, like:
  "sideEffects": [
    "main.ts",
    "polyfills.ts"
  ]

Hope that helps.

1reaction
alan-agius4commented, May 18, 2022

Thanks for reporting this issue. This issue is now obsolete due to changes in the recent releases.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Everything you never wanted to know about side effects
However, tree-shaking can only work effectively if it knows about side ... Since bundlers can't figure out side effects for themselves, ...
Read more >
Tree Shaking - webpack
A "side effect" is defined as code that performs a special behavior when imported, other than exposing one or more exports. An example...
Read more >
What Does Webpack 4 Expect From A Package With sideEffects
css' file from the bundle as part of the "tree-shaking" process if the npm-package has sideEffects: false flag. So, instead of writing ...
Read more >
Tree shaking and code splitting in webpack - LogRocket Blog
What is tree shaking? Tree shaking, also known as dead code elimination, is the practice of removing unused code in your production build....
Read more >
Tree-shaking in real world: what could go wrong? - Medium
The general idea is that any unused code without side-effects (i.e. modifying external variables or global state, polyfilling and stuff like ...
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