-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Mark shared packages as side effects free #18365
Conversation
🦋 Changeset detectedLatest commit: bb38e4c The changes in this PR will be included in the next version bump. This PR includes changesets to release 14 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@nickrum arent most packages of ours side effect free? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With additional context from #11329, this PR does seems to resolve the inefficient tree shaking when this sideEffects
property was not present. Here are some observed outcome:
Sample file used
import { defineEndpoint } from '@directus/extensions-sdk';
export default defineEndpoint((router) => {
router.get('/', (_req, res) => res.send('Hello, World!'));
});
Before this PR
- Filesize: 225 kb
- Code: (omitted since it's a tad large, but I can share it if deemed necessary)
After this PR
- Filesize: 1 kb
- Code:
"use strict";require("axios");var e=e=>{e.get("/",((e,r)=>r.send("Hello, World!")))};module.exports=e;
That being said, @nickrum as seen by the require('axios')
in the "After this PR" result, do we want to (or can we?) mark the composables
package as sideEffects free as well? As the axios seems to be included due to use-items
composable if I'm not mistaken, but it shouldn't be present in this case. A quick test I did by adding sideEffects: false
to the composable package seems to make sure the built file turns out to be the following:
"use strict";var e=e=>{e.get("/",((e,l)=>l.send("Hello, World!")))};module.exports=e;
but I'm not sure if that's too naive.
I'm pretty sure this is due to the I think the solution here might be to bundle these packages themselves as well, but we could look at that in a later step.
I'm pretty sure they are and for the moment should be declared as such. For example the |
@paescuj thoughts on removing those index files? DX is a tad less nice, but at the same time it makes it super obvious where imports are coming from so maybe it's a win-win? |
Thought about this as well, but I'm a little put off by the idea of having to update all imports every time we rename or relocate a file (may not happen often, but is definitely a possibility). Also don't know if we really need this, but at the moment we have the control over which modules we actually want to export. If we want to keep this, we would have to explicitly specify all modules to export in the |
Yeah now that I'm awake and thinking about this better it's not the right solution. We'd still want people to be able to do things like |
Yes, I think so too 👍 For now, let's additionally mark Edit: And as correctly pointed out by @br41nslug also |
That
Wouldn't that mean that most of the bigger js libraries wouldn't be tree-shakable? AFAIK rollup does treeshaking in multiple passes, one of which specifically includes re-exports, and this little test shows that rollup is perfectly capable of tree-shaking re-exported values. |
Yes, I was wondering the same! I just remembered reading that somewhere. Probably the heuristics are much better nowadays. I'm honestly glad if this is not the problem 😄
Yes, this is very likely the case 🕵️ 👍 |
* Mark the constant and utils packages as side effects free * Add changeset * mark composables as side effect free * mark exceptions package as side effects free * updated changeset --------- Co-authored-by: Brainslug <tim@brainslug.nl>
API extensions using Typescript import one of the
define*()
from@directus/extensions-sdk
. Rollup's tree-shaking heuristics aren't prefect. So in order for this import to be properly tree-shaken away by rollup, the package from which the function is imported (@directus/utils
) must be marked as side effects free. Due to reasons not clear to me,@directus/constants
also needs to be marked as side effects free.This was also present in the old
@directus/shared
package, so this PR essentially only reverts to the previous behavior.