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?**
Issue Analytics
- State:
- Created 3 years ago
- Reactions:9
- Comments:6 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
FYI, my app completely broke when built with
--prod
only using a package.json withsideEffects: 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:sideEffects:
field are only present in--prod
mode - so make sure you test in prod (obvious, but worth spelling out for some).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.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).main.ts
andpolyfills.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:main.ts
andpolyfills.ts
, or in a parent directory of that directory, then you need to mark them as side-effects files, like:Hope that helps.
Thanks for reporting this issue. This issue is now obsolete due to changes in the recent releases.