How to confirm that manual tree shaking variables are taking full effect? #18965
|
I recently came across this page that talks about tree shaking in Sentry: I'm using Webpack with Sentry 8.55.0 and I tried to set the recommended variables in my build, as it shows on the page: But I don't see almost any difference in the build output. What should I look for to validate that these variables are taking effect? Should entire internal Sentry modules get removed from the build? One thing I should mention is that I've confirmed that automatic tree shaking is already working. Interestingly, when I do this:
I see some tree shaking taking place. A bunch of Sentry modules now appear in 0 chunks in my build (as compared with using CJS But when I change it to explicit import only the specific functions I'm calling, like this:
There's way, way more tree shaking that happens. A huge number of Sentry modules are removed from the build when I do this. So clearly Webpack does a much better job if you are explicit about what is being imported. I'm wondering if that is already shaking out the modules that would be removed by the manual shaking using the If there was some way to know which modules should be removed with those variables, I can validate that as much tree shaking as possible is taking effect. Thanks! Best regards, |
Replies: 1 comment
|
Your instinct that automatic tree-shaking is doing most of the heavy lifting is correct, and it's worth separating what these two mechanisms are actually shaking. The To actually verify they took effect: build for production (minified) and grep the final bundle for the literal identifier strings: If the DefinePlugin substitution worked, that should return 0, the identifiers get replaced with the literal On your explicit-import finding: that's a separate and complementary mechanism, not evidence the DefinePlugin flags are redundant. Wildcard |
Your instinct that automatic tree-shaking is doing most of the heavy lifting is correct, and it's worth separating what these two mechanisms are actually shaking.
The
DefinePluginvariables (__SENTRY_DEBUG__,__SENTRY_TRACING__, etc.) don't remove anything by themselves, they turn internalif (__SENTRY_DEBUG__) { ... }-style branches inside Sentry's own source into dead code (if (false) { ... }). That dead branch only actually disappears from your bundle during minification, when Terser (or whatever minifier Webpack is using) runs its own dead-code elimination pass over the now-if (false)blocks. So checking the raw, unminified build output won't show a difference, these flags are invisib…