Skip to content

Commit

Permalink
fix(build): Prevent unused utils code in integration bundles (#4547)
Browse files Browse the repository at this point in the history
Even without a plugin like `terser`, Rollup by default will treeshake code[1] it sees isn't being used. That said, by default it will err on the side of caution[2], 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[3], 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:

> ls -l packages/integrations/build/*.min.js
-rw-r--r--  1 Katie  staff   4577 Feb 10 14:34 packages/integrations/build/angular.min.js
-rw-r--r--  1 Katie  staff   4667 Feb 10 14:34 packages/integrations/build/captureconsole.min.js
-rw-r--r--  1 Katie  staff   3815 Feb 10 14:34 packages/integrations/build/debug.min.js
-rw-r--r--  1 Katie  staff   4590 Feb 10 14:34 packages/integrations/build/dedupe.min.js
-rw-r--r--  1 Katie  staff   4022 Feb 10 14:34 packages/integrations/build/ember.min.js
-rw-r--r--  1 Katie  staff   8560 Feb 10 14:34 packages/integrations/build/extraerrordata.min.js
-rw-r--r--  1 Katie  staff  40177 Feb 10 14:34 packages/integrations/build/offline.min.js
-rw-r--r--  1 Katie  staff   4508 Feb 10 14:34 packages/integrations/build/reportingobserver.min.js
-rw-r--r--  1 Katie  staff   5772 Feb 10 14:34 packages/integrations/build/rewriteframes.min.js
-rw-r--r--  1 Katie  staff    923 Feb 10 14:27 packages/integrations/build/sessiontiming.min.js
-rw-r--r--  1 Katie  staff    934 Feb 10 14:27 packages/integrations/build/transaction.min.js
-rw-r--r--  1 Katie  staff   7806 Feb 10 14:34 packages/integrations/build/vue.min.js

After:

> ls -l packages/integrations/build/*.min.js
-rw-r--r--  1 Katie  staff   2985 Feb 10 14:26 packages/integrations/build/angular.min.js
-rw-r--r--  1 Katie  staff   2159 Feb 10 14:26 packages/integrations/build/captureconsole.min.js
-rw-r--r--  1 Katie  staff   2227 Feb 10 14:26 packages/integrations/build/debug.min.js
-rw-r--r--  1 Katie  staff   2998 Feb 10 14:26 packages/integrations/build/dedupe.min.js
-rw-r--r--  1 Katie  staff   2430 Feb 10 14:27 packages/integrations/build/ember.min.js
-rw-r--r--  1 Katie  staff   6972 Feb 10 14:27 packages/integrations/build/extraerrordata.min.js
-rw-r--r--  1 Katie  staff  38576 Feb 10 14:27 packages/integrations/build/offline.min.js
-rw-r--r--  1 Katie  staff   1877 Feb 10 14:27 packages/integrations/build/reportingobserver.min.js
-rw-r--r--  1 Katie  staff   2847 Feb 10 14:27 packages/integrations/build/rewriteframes.min.js
-rw-r--r--  1 Katie  staff    923 Feb 10 14:27 packages/integrations/build/sessiontiming.min.js
-rw-r--r--  1 Katie  staff    934 Feb 10 14:27 packages/integrations/build/transaction.min.js
-rw-r--r--  1 Katie  staff   6848 Feb 10 14:27 packages/integrations/build/vue.min.js

[1] https://rollupjs.org/guide/en/#tree-shaking
[2] https://rollupjs.org/guide/en/#tree-shaking-doesnt-seem-to-be-working
[3] https://github.com/rollup/rollup/tree/master/src/ast
  • Loading branch information
lobsterkatie committed Feb 11, 2022
1 parent 7f40e50 commit d33b1fc
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/browser/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const bundleConfig = {
banner: `/*! @sentry/browser <%= pkg.version %> (${commitHash}) | https://github.com/getsentry/sentry-javascript */`,
}),
],
treeshake: 'smallest',
};

export default [
Expand Down
1 change: 1 addition & 0 deletions packages/integrations/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function loadAllIntegrations() {
strict: false,
},
plugins: build.plugins,
treeshake: 'smallest',
})),
);
});
Expand Down
1 change: 1 addition & 0 deletions packages/tracing/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const bundleConfig = {
banner: `/*! @sentry/tracing & @sentry/browser <%= pkg.version %> (${commitHash}) | https://github.com/getsentry/sentry-javascript */`,
}),
],
treeshake: 'smallest',
};

export default [
Expand Down
1 change: 1 addition & 0 deletions packages/vue/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const bundleConfig = {
banner: `/*! @sentry/vue <%= pkg.version %> (${commitHash}) | https://github.com/getsentry/sentry-javascript */`,
}),
],
treeshake: 'smallest',
};

export default [
Expand Down
1 change: 1 addition & 0 deletions packages/wasm/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function loadAllIntegrations() {
strict: false,
},
plugins: build.plugins,
treeshake: 'smallest',
});
});
return builds;
Expand Down

0 comments on commit d33b1fc

Please sign in to comment.