Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(build): Prevent unused utils code in integration bundles (#4547)
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