Skip to content
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

fix(build): Prevent unused utils code in integration bundles #4547

Merged
merged 2 commits into from Feb 11, 2022

Conversation

lobsterkatie
Copy link
Member

@lobsterkatie lobsterkatie commented Feb 11, 2022

Even without a plugin like terser, Rollup by default will treeshake code it sees isn't being used. That said, by default it will err on the side of caution, to make sure it doesn't break anyone's code.

One of the conservative choices it makes out of the box is to assume that any module with side effects ought to be retained, even if none of the code it contains is used. Some of the things it considers side effects make sense (console.log(), for example), but others are much less obvious (and, frankly, a little hard to understand), among them accessing process and using SomeClass.prototype.someMethod.call(someInstance, ...args) rather than someInstance.someMethod(...args). (Disclaimer: These were discovered through repeated... and repeated... and repeated trial and error. The truth lies somewhere in rollup's ast code, but in my testing they consistently were the two things which prevented treeshaking.) As it happens, we have the proverbial perfect storm in our utils package, in the form of isNodeEnv(), which has not one but both of those things.

In any case, that's a lot of preamble to say that in @sentry/utils, the index file imports from time.ts,time.ts includes an IIFE which calls getGlobalObject(), and getGlobalObject() in turn calls isNodeEnv(). Thus, importing anything at all from @sentry/utils has meant that the entirety of time.ts, global.js, and node.js had to come along for the ride in any bundle we create, regardless of their use (or lack thereof). In particular, even though none of our integrations use any of the code in time.ts, it was being included all but two integration bundles, because they all use something else from @sentry/utils.

Fortunately, rollup provides an option, treeshake.moduleSideEffects : false, to only consider actual code use when deciding which modules to include in a bundle, regardless of whatever side effects that module may or may not generate. This PR uses the more general tresshake: "smallest" (which includes setting moduleSideEffects to false) in order to eliminate the incorrect inclusion of time.ts in the integration bundles.

Before:

image

After:

image

UPDATE: This turns out to explain the process access being considered a side effect. It's also overridden by treeshake: "smallest".

@lobsterkatie lobsterkatie force-pushed the kmclb-fix-isNodeEnv-broken-treeshaking branch from f26bf03 to 20e651a Compare February 11, 2022 07:18
@github-actions
Copy link
Contributor

size-limit report

Path Base Size (487945f) Current Size Change
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 19.73 KB 19.73 KB -0.01% 🔽
@sentry/browser - ES5 CDN Bundle (minified) 63.06 KB 63.06 KB 0%
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 18.44 KB 18.44 KB -0.01% 🔽
@sentry/browser - ES6 CDN Bundle (minified) 56.5 KB 56.5 KB 0%
@sentry/browser - Webpack (gzipped + minified) 22.16 KB 22.16 KB 0%
@sentry/browser - Webpack (minified) 75.87 KB 75.87 KB 0%
@sentry/react - Webpack (gzipped + minified) 22.19 KB 22.19 KB 0%
@sentry/nextjs Client - Webpack (gzipped + minified) 46.27 KB 46.27 KB 0%
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 28.3 KB 28.3 KB -0.01% 🔽

@lobsterkatie lobsterkatie merged commit d33b1fc into master Feb 11, 2022
@lobsterkatie lobsterkatie deleted the kmclb-fix-isNodeEnv-broken-treeshaking branch February 11, 2022 14:50
@AbhiPrasad AbhiPrasad added this to the Treeshaking / Bundle Size milestone Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants