-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Improve tree-shaking of external libraries #17767
Comments
Update: This can be worked around using webpack plugin. See ckeditor/ckeditor5-dev#1060. |
Fix (tests): Disable tree-shaking in webpack. See ckeditor/ckeditor5#17767.
We've decided to implement this improvement in two steps. In the first step, we will add the |
…down-plugin Other (markdown-gfm): Add `sideEffects` field to allow tree-shaking of external libraries. See #17767.
Other (ui): Add `sideEffects` field to allow tree-shaking of external libraries. See #17767.
The change in The same change in |
Issue
As described in #16395, we use a few external libraries that are not tree-shakeable. This unnecessarily increases the bundle size in projects that don't make use of them. This includes:
ui
— depends oncolor-convert
andcolor-name
, which are only needed with plugins using color picker.markdown-gfm
— depends onmarked
andturndown
, which are only needed when the Markdown plugin is used.real-time-collaboration
— depends onsocket.io
,protobufjs
, and many others, which are only needed when RTC features are used.Our code in these packages is already fully tree-shakeable, but because these dependencies are not, they are kept in the bundle. The only way to prevent these dependencies from always being bundled that I'm aware of (except for replacing these libraries with others that are tree-shakeable) is to add
sideEffects
field topackage.json
files of our packages depending on these libraries.The
sideEffects
fieldThe
sideEffects
field in apackage.json
file indicates whether the entire package or specific files within it have side effects when imported, which helps bundlers with tree-shaking to remove unused code more effectively.Solution
If a file with side effects is not added to the
sideEffects
array, it may be tree-shaken away, causing issues or breaking the editor entirely. That is why I would avoid adding this field to all of our packages, but only as a last resort to these that have non-tree-shakeable external libraries. We also have to be careful to include in the array all the files that have side effects like:*.css
filesbuild/**/*.js
files (build and translations)dist/translations/*.umd.js
The result
Adding these fields to
ui
,markdown-gfm
, andreal-time-collaboration
reduces the bundle size by up to 140 KiB in Vite/Rollup and 130 KiB in esbuild. The following, darkened dependencies were removed from Rollup and esbuild builds with the following code:Testing
To make sure that this change doesn't introduce any regressions, we need to test all three packages mentioned above and see if they work properly in old and new installation methods. While unlikely, it's possible that too much code will be tree-shaken away, breaking these features or their styles.
The text was updated successfully, but these errors were encountered: