perf: Add sideEffects declaration to package.json
#1276
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR improves tree shaking when using the ES Module builds of the library. Currently, bundlers struggle to detect the fact that all of the exported components and hooks are side effect free, and can thus be tree shaken if not used explicitly. This leads to bundles that contain dead code for components that will never be rendered.
A minimal example can be found HERE: running
npm run buildand looking inside the resulting index.js bundle, we can see that it also contains code for the Icon and SearchBox components which are not used in App.tsx.Done
sideEffects: ["**/*.scss"]to package.jsonQA
In the minimal example above (HERE), open a new terminal and run
npm run build; take note of the size of the index.js bundle. Run the commandnode patch.mjs(it adds thesideEffectsproperty to this library's package.json), then runnpm run buildagain; the size of index.js should be considerably lower.QA in your project
In your project, build your bundles using the current version of the library (make sure you import the components from "@canonical/react-components/dist/esm", otherwise you will consume the CJS build which can not be tree shaken). Take note of the total bundle size.
Add
"sideEffects": ["**/*.scss"]tonode_modules/@canonical/react-components/package.jsonand run the build command of your project again. The resulting bundle size should be smaller (if you use ALL components in this library it will remain the same).