-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(build): Prevent Node's Buffer
module from being included in browser bundles
#3372
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 Node's Buffer
module from being included in browser bundles
#3372
Conversation
size-limit report
|
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.
I suggest renaming packages/node/src/string.ts
-> packages/node/src/base64.ts
since the contents there are only related to base64 conversion (and them end up with files with the same name in browser and node for ease of discoverability and navigating the code base)
packages/browser/src/helpers.ts
Outdated
* (This is here (and in the node package) rather than in @sentry/utils to prevent webpack from bundling the entire | ||
* Buffer module when creating SDK users' vendor bundles for the browser. Used in @sentry/utils.string.) |
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.
I'd avoid making certain cross-references that can get outdated. Comments are not good for that, and we have tools to figure out where a certain piece of code is used.
I'd also avoid putting such comments in the jsdoc of functions, since they are unrelated to how the function works and would end up polluting the generated documentation.
My suggestion is to have all related functions in a single file and have a single comment applying to the whole file.
File: packages/browser/src/base64.ts
// This file is here instead of in @sentry/utils such that users of the Browser SDK using webpack
// do not have Node's Buffer module unnecessarily included in their bundles.
// ...
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.
I think you're right about not polluting the JSDoc with it, but I kept a similar comment because, sure, you can search for something across packages, but who thinks to do that if you're just changing one test for whatever reason? This way, anyone changing things in one place knows immediately that it needs to be done elsewhere as well (even if that change is taking them out of sync for some reason, in which case they'll know to delete the comment in both places!).
I'm really puzzled by the bundle size gains in #3372 (comment), I wonder if we can trust that since this PR is not targeting master. What is the size comparison comparing? |
ba918c3
to
00cfee2
Compare
00cfee2
to
9228f1e
Compare
It's comparing to the branch, I believe. The whole reason for this PR is that this branch's changes had caused the package size to go way up; this brings it back down to baseline by fixing the cause of the increase. That's why you're seeing such a big percentage drop. (I tested it locally, and the numbers in the comment are correct.) |
40f66c4
to
a957d9d
Compare
This is the result of rebasing the feature branch for the initial implementation of `tracestate` header handling (which had grown very stale) on top of current `master`. That branch is going to get deleted, so for posterity, it included the following PRs (oldest -> newest): feat(tracing): Add dynamic sampling correlation context data to envelope header (#3062) chore(utils): Split browser/node compatibility utils into separate module (#3123) ref(tracing): Prework for initial `tracestate` implementation (#3242) feat(tracing): Add `tracestate` header to outgoing requests (#3092) ref(tracing): Rework tracestate internals to allow for third-party data (#3266) feat(tracing): Handle incoming tracestate data, allow for third-party data (#3275) chore(tracing): Various small fixes to first `tracestate` implementation (#3291) fix(tracing): Use `Request.headers.append` correctly (#3311) feat(tracing): Add user data to tracestate header (#3343) chore(various): Small fixes (#3368) fix(build): Prevent Node's `Buffer` module from being included in browser bundles (#3372) fix(tracing): Remove undefined tracestate data rather than setting it to `null` (#3373) More detail in the PR description.
This is the result of rebasing the feature branch for the initial implementation of `tracestate` header handling (which had grown very stale) on top of current `master`. That branch is going to get deleted, so for posterity, it included the following PRs (oldest -> newest): feat(tracing): Add dynamic sampling correlation context data to envelope header (#3062) chore(utils): Split browser/node compatibility utils into separate module (#3123) ref(tracing): Prework for initial `tracestate` implementation (#3242) feat(tracing): Add `tracestate` header to outgoing requests (#3092) ref(tracing): Rework tracestate internals to allow for third-party data (#3266) feat(tracing): Handle incoming tracestate data, allow for third-party data (#3275) chore(tracing): Various small fixes to first `tracestate` implementation (#3291) fix(tracing): Use `Request.headers.append` correctly (#3311) feat(tracing): Add user data to tracestate header (#3343) chore(various): Small fixes (#3368) fix(build): Prevent Node's `Buffer` module from being included in browser bundles (#3372) fix(tracing): Remove undefined tracestate data rather than setting it to `null` (#3373) More detail in the PR description.
This is the result of rebasing the feature branch for the initial implementation of `tracestate` header handling (which had grown very stale) on top of current `master`. That branch is going to get deleted, so for posterity, it included the following PRs (oldest -> newest): feat(tracing): Add dynamic sampling correlation context data to envelope header (#3062) chore(utils): Split browser/node compatibility utils into separate module (#3123) ref(tracing): Prework for initial `tracestate` implementation (#3242) feat(tracing): Add `tracestate` header to outgoing requests (#3092) ref(tracing): Rework tracestate internals to allow for third-party data (#3266) feat(tracing): Handle incoming tracestate data, allow for third-party data (#3275) chore(tracing): Various small fixes to first `tracestate` implementation (#3291) fix(tracing): Use `Request.headers.append` correctly (#3311) feat(tracing): Add user data to tracestate header (#3343) chore(various): Small fixes (#3368) fix(build): Prevent Node's `Buffer` module from being included in browser bundles (#3372) fix(tracing): Remove undefined tracestate data rather than setting it to `null` (#3373) More detail in the PR description.
This is the result of rebasing the feature branch for the initial implementation of `tracestate` header handling (which had grown very stale) on top of current `master`. That branch is going to get deleted, so for posterity, it included the following PRs (oldest -> newest): feat(tracing): Add dynamic sampling correlation context data to envelope header (#3062) chore(utils): Split browser/node compatibility utils into separate module (#3123) ref(tracing): Prework for initial `tracestate` implementation (#3242) feat(tracing): Add `tracestate` header to outgoing requests (#3092) ref(tracing): Rework tracestate internals to allow for third-party data (#3266) feat(tracing): Handle incoming tracestate data, allow for third-party data (#3275) chore(tracing): Various small fixes to first `tracestate` implementation (#3291) fix(tracing): Use `Request.headers.append` correctly (#3311) feat(tracing): Add user data to tracestate header (#3343) chore(various): Small fixes (#3368) fix(build): Prevent Node's `Buffer` module from being included in browser bundles (#3372) fix(tracing): Remove undefined tracestate data rather than setting it to `null` (#3373) More detail in the PR description.
NB: After it was opened, this PR was totally overhauled and simplified considerably. The original PR description is included below so that the review comments make any sense at all, but to be clear, none of it applies to the current implementation.
Prior to this PR, the fact that the Node
Buffer
class made a (naked) appearance in@sentry/utils
meant that webpack included it browser bundles*, noticeably increasing their size. This fixes that by changing allBuffer
references in@sentry/utils
(all two of them!) fromBuffer
toglobalObject.Buffer
, which it turns out is enough to convince webpack not to include it. (Who knew?) It does a similar thing foratob
andbtoa
in the browser, just for consistency. Other changes include:@sentry/browser
since the implementation of the base64 functions is different in browser than in node (otherwise none of this would be a problem!)*To be clear, in this case "browser bundles" means the bundles a SDK user would create were they using Sentry in their web app, not the bundles we serve from our CDN. Our bundles, because they're created with Rollup and not webpack, were already fine.
Original description (outdated):
This PR pulls the code which uses
Buffer
back into@sentry/node
(and does a similar thing foratob
andbtoa
in the browser), and refactors to use it from there. Specifically, it stashes the relevant functions onwindow.__SENTRY__.extensions
orglobal.__SENTRY__.extensions
on SDK startup, and then@sentry/utils
grabs them from there.Changes necessitated by the above:
Carrier
interface andgetMainCarrirer
function out of@sentry/hub
, into (respectively)@sentry/types
and@sentry/utils
. Both are still exported from@sentry/hub
for backwards compatibility.dom
testing environment where appropriate.@sentry/tracing
against Node 6.