Skip to content

Commit 7f40e50

Browse files
authored
fix(build): Remove node code from CDN bundles (getsentry#4548)
This changes the way we substitute `true` for `__SENTRY_BROWSER_BUNDLE__`, the flag which enables us to treeshake anything Node-related out of CDN bundles. There are two main changes: - In the browser rollup config, the substitution is moved from the terser stage to an earlier stage, immediately after TS compilation, in order that the treeshaking we need now applies to all versions of the bundle, rather than just the minified ones. - The substitution is added to all other rollup configs. (Even though packages like `@sentry/tracing` and `@sentry/integrations` are cross-platform, in this case we know we're building CDN bundles (which are only relevant in a browser setting), so it's safe to restrict it.) For an example of the effect of these changes on a non-minified bundle, see the screenshots included in the PR description.
1 parent 2c47a8b commit 7f40e50

File tree

7 files changed

+118
-3
lines changed

7 files changed

+118
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
],
5252
"devDependencies": {
5353
"@google-cloud/storage": "^5.7.0",
54+
"@rollup/plugin-replace": "^3.0.1",
5455
"@size-limit/preset-small-lib": "^4.5.5",
5556
"@strictsoftware/typedoc-plugin-monorepo": "^0.3.1",
5657
"@types/chai": "^4.1.3",

packages/browser/rollup.config.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import typescript from 'rollup-plugin-typescript2';
33
import license from 'rollup-plugin-license';
44
import resolve from 'rollup-plugin-node-resolve';
55
import commonjs from 'rollup-plugin-commonjs';
6+
import replace from '@rollup/plugin-replace';
67

78
const commitHash = require('child_process')
89
.execSync('git rev-parse --short HEAD', { encoding: 'utf-8' })
@@ -13,7 +14,6 @@ const terserInstance = terser({
1314
// Tell env.ts that we're building a browser bundle and that we do not
1415
// want to have unnecessary debug functionality.
1516
global_defs: {
16-
__SENTRY_BROWSER_BUNDLE__: true,
1717
__SENTRY_NO_DEBUG__: false,
1818
},
1919
},
@@ -59,6 +59,16 @@ const plugins = [
5959
},
6060
include: ['*.ts+(|x)', '**/*.ts+(|x)', '../**/*.ts+(|x)'],
6161
}),
62+
replace({
63+
// don't replace `__placeholder__` where it's followed immediately by a single `=` (to prevent ending up
64+
// with something of the form `let "replacementValue" = "some assigned value"`, which would cause a
65+
// syntax error)
66+
preventAssignment: true,
67+
// the replacements to make
68+
values: {
69+
__SENTRY_BROWSER_BUNDLE__: true,
70+
},
71+
}),
6272
resolve({
6373
mainFields: ['module'],
6474
}),

packages/integrations/rollup.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { terser } from 'rollup-plugin-terser';
44
import typescript from 'rollup-plugin-typescript2';
55
import resolve from 'rollup-plugin-node-resolve';
66
import commonjs from 'rollup-plugin-commonjs';
7+
import replace from '@rollup/plugin-replace';
78

89
const terserInstance = terser({
910
mangle: {
@@ -38,6 +39,16 @@ const plugins = [
3839
},
3940
include: ['*.ts+(|x)', '**/*.ts+(|x)', '../**/*.ts+(|x)'],
4041
}),
42+
replace({
43+
// don't replace `__placeholder__` where it's followed immediately by a single `=` (to prevent ending up
44+
// with something of the form `let "replacementValue" = "some assigned value"`, which would cause a
45+
// syntax error)
46+
preventAssignment: true,
47+
// the replacements to make
48+
values: {
49+
__SENTRY_BROWSER_BUNDLE__: true,
50+
},
51+
}),
4152
resolve({
4253
mainFields: ['module'],
4354
}),

packages/tracing/rollup.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import typescript from 'rollup-plugin-typescript2';
33
import license from 'rollup-plugin-license';
44
import resolve from 'rollup-plugin-node-resolve';
55
import commonjs from 'rollup-plugin-commonjs';
6+
import replace from '@rollup/plugin-replace';
67

78
const commitHash = require('child_process')
89
.execSync('git rev-parse --short HEAD', { encoding: 'utf-8' })
@@ -46,6 +47,16 @@ const plugins = [
4647
},
4748
include: ['*.ts+(|x)', '**/*.ts+(|x)', '../**/*.ts+(|x)'],
4849
}),
50+
replace({
51+
// don't replace `__placeholder__` where it's followed immediately by a single `=` (to prevent ending up
52+
// with something of the form `let "replacementValue" = "some assigned value"`, which would cause a
53+
// syntax error)
54+
preventAssignment: true,
55+
// the replacements to make
56+
values: {
57+
__SENTRY_BROWSER_BUNDLE__: true,
58+
},
59+
}),
4960
resolve({
5061
mainFields: ['module'],
5162
}),

packages/vue/rollup.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import typescript from 'rollup-plugin-typescript2';
33
import license from 'rollup-plugin-license';
44
import resolve from 'rollup-plugin-node-resolve';
55
import commonjs from 'rollup-plugin-commonjs';
6+
import replace from '@rollup/plugin-replace';
67

78
const commitHash = require('child_process')
89
.execSync('git rev-parse --short HEAD', { encoding: 'utf-8' })
@@ -46,6 +47,16 @@ const plugins = [
4647
},
4748
include: ['*.ts+(|x)', '**/*.ts+(|x)', '../**/*.ts+(|x)'],
4849
}),
50+
replace({
51+
// don't replace `__placeholder__` where it's followed immediately by a single `=` (to prevent ending up
52+
// with something of the form `let "replacementValue" = "some assigned value"`, which would cause a
53+
// syntax error)
54+
preventAssignment: true,
55+
// the replacements to make
56+
values: {
57+
__SENTRY_BROWSER_BUNDLE__: true,
58+
},
59+
}),
4960
resolve({
5061
mainFields: ['module'],
5162
}),

packages/wasm/rollup.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { terser } from 'rollup-plugin-terser';
22
import typescript from 'rollup-plugin-typescript2';
33
import resolve from 'rollup-plugin-node-resolve';
44
import commonjs from 'rollup-plugin-commonjs';
5+
import replace from '@rollup/plugin-replace';
56

67
const terserInstance = terser({
78
mangle: {
@@ -36,6 +37,16 @@ const plugins = [
3637
},
3738
include: ['*.ts+(|x)', '**/*.ts+(|x)', '../**/*.ts+(|x)'],
3839
}),
40+
replace({
41+
// don't replace `__placeholder__` where it's followed immediately by a single `=` (to prevent ending up
42+
// with something of the form `let "replacementValue" = "some assigned value"`, which would cause a
43+
// syntax error)
44+
preventAssignment: true,
45+
// the replacements to make
46+
values: {
47+
__SENTRY_BROWSER_BUNDLE__: true,
48+
},
49+
}),
3950
resolve({
4051
mainFields: ['module'],
4152
}),

yarn.lock

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,6 +3088,23 @@
30883088
resolved "https://registry.yarnpkg.com/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
30893089
integrity sha1-p3c2C1s5oaLlEG+OhY8v0tBgxXA=
30903090

3091+
"@rollup/plugin-replace@^3.0.1":
3092+
version "3.0.1"
3093+
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-3.0.1.tgz#f774550f482091719e52e9f14f67ffc0046a883d"
3094+
integrity sha512-989J5oRzf3mm0pO/0djTijdfEh9U3n63BIXN5X7T4U9BP+fN4oxQ6DvDuBvFaHA6scaHQRclqmKQEkBhB7k7Hg==
3095+
dependencies:
3096+
"@rollup/pluginutils" "^3.1.0"
3097+
magic-string "^0.25.7"
3098+
3099+
"@rollup/pluginutils@^3.1.0":
3100+
version "3.1.0"
3101+
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.1.0.tgz#706b4524ee6dc8b103b3c995533e5ad680c02b9b"
3102+
integrity sha512-GksZ6pr6TpIjHm8h9lSQ8pi8BE9VeubNT0OMJ3B5uZJ8pz73NPiqOtCog/x2/QzM1ENChPKxMDhiQuRHsqc+lg==
3103+
dependencies:
3104+
"@types/estree" "0.0.39"
3105+
estree-walker "^1.0.1"
3106+
picomatch "^2.2.2"
3107+
30913108
"@rollup/pluginutils@^4.1.2":
30923109
version "4.1.2"
30933110
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-4.1.2.tgz#ed5821c15e5e05e32816f5fb9ec607cdf5a75751"
@@ -3559,6 +3576,11 @@
35593576
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.47.tgz#d7a51db20f0650efec24cd04994f523d93172ed4"
35603577
integrity sha512-c5ciR06jK8u9BstrmJyO97m+klJrrhCf9u3rLu3DEAJBirxRqSCvDQoYKmxuYwQI5SZChAWu+tq9oVlGRuzPAg==
35613578

3579+
"@types/estree@0.0.39":
3580+
version "0.0.39"
3581+
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
3582+
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==
3583+
35623584
"@types/estree@^0.0.50":
35633585
version "0.0.50"
35643586
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.50.tgz#1e0caa9364d3fccd2931c3ed96fdbeaa5d4cca83"
@@ -4466,11 +4488,32 @@ after@0.8.2:
44664488
resolved "https://registry.yarnpkg.com/after/-/after-0.8.2.tgz#fedb394f9f0e02aa9768e702bda23b505fae7e1f"
44674489
integrity sha1-/ts5T58OAqqXaOcCvaI7UF+ufh8=
44684490

4469-
agent-base@4, agent-base@5, agent-base@6, agent-base@^4.3.0, agent-base@^6.0.2, agent-base@~4.2.1:
4491+
agent-base@4, agent-base@^4.3.0:
4492+
version "4.3.0"
4493+
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.3.0.tgz#8165f01c436009bccad0b1d122f05ed770efc6ee"
4494+
integrity sha512-salcGninV0nPrwpGNn4VTXBb1SOuXQBiqbrNXoeizJsHrsL6ERFM2Ne3JUSBWRE6aeNJI2ROP/WEEIDUiDe3cg==
4495+
dependencies:
4496+
es6-promisify "^5.0.0"
4497+
4498+
agent-base@5:
44704499
version "5.1.1"
44714500
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-5.1.1.tgz#e8fb3f242959db44d63be665db7a8e739537a32c"
44724501
integrity sha512-TMeqbNl2fMW0nMjTEPOwe3J/PRFP4vqeoNuQMG0HlMrtm5QxKqdvAkZ1pRBQ/ulIyDD5Yq0nJ7YbdD8ey0TO3g==
44734502

4503+
agent-base@6, agent-base@^6.0.2:
4504+
version "6.0.2"
4505+
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77"
4506+
integrity sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==
4507+
dependencies:
4508+
debug "4"
4509+
4510+
agent-base@~4.2.1:
4511+
version "4.2.1"
4512+
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-4.2.1.tgz#d89e5999f797875674c07d87f260fc41e83e8ca9"
4513+
integrity sha512-JVwXMr9nHYTUXsBFKUqhJwvlcYU/blreOEUkhNR2eXZIvwd+c+o5V4MgDPKWnMS/56awN3TRzIP+KoPn+roQtg==
4514+
dependencies:
4515+
es6-promisify "^5.0.0"
4516+
44744517
agentkeepalive@^3.4.1:
44754518
version "3.5.2"
44764519
resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-3.5.2.tgz#a113924dd3fa24a0bc3b78108c450c2abee00f67"
@@ -9922,6 +9965,18 @@ es6-object-assign@^1.1.0:
99229965
resolved "https://registry.yarnpkg.com/es6-object-assign/-/es6-object-assign-1.1.0.tgz#c2c3582656247c39ea107cb1e6652b6f9f24523c"
99239966
integrity sha1-wsNYJlYkfDnqEHyx5mUrb58kUjw=
99249967

9968+
es6-promise@^4.0.3:
9969+
version "4.2.8"
9970+
resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-4.2.8.tgz#4eb21594c972bc40553d276e510539143db53e0a"
9971+
integrity sha512-HJDGx5daxeIvxdBxvG2cb9g4tEvwIk3i8+nhX0yGrYmZUzbkdg8QbDevheDB8gd0//uPj4c1EQua8Q+MViT0/w==
9972+
9973+
es6-promisify@^5.0.0:
9974+
version "5.0.0"
9975+
resolved "https://registry.yarnpkg.com/es6-promisify/-/es6-promisify-5.0.0.tgz#5109d62f3e56ea967c4b63505aef08291c8a5203"
9976+
integrity sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM=
9977+
dependencies:
9978+
es6-promise "^4.0.3"
9979+
99259980
escalade@^3.1.1:
99269981
version "3.1.1"
99279982
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
@@ -10247,6 +10302,11 @@ estree-walker@^0.6.0, estree-walker@^0.6.1:
1024710302
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362"
1024810303
integrity sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w==
1024910304

10305+
estree-walker@^1.0.1:
10306+
version "1.0.1"
10307+
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-1.0.1.tgz#31bc5d612c96b704106b477e6dd5d8aa138cb700"
10308+
integrity sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg==
10309+
1025010310
estree-walker@^2.0.1:
1025110311
version "2.0.2"
1025210312
resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-2.0.2.tgz#52f010178c2a4c117a7757cfe942adb7d2da4cac"
@@ -13394,7 +13454,7 @@ jest-environment-jsdom@^24.9.0:
1339413454
jest-util "^24.9.0"
1339513455
jsdom "^11.5.1"
1339613456

13397-
jest-environment-node@24, "jest-environment-node@>=24 <=26", jest-environment-node@^24.9.0:
13457+
"jest-environment-node@>=24 <=26", jest-environment-node@^24.9.0:
1339813458
version "24.9.0"
1339913459
resolved "https://registry.yarnpkg.com/jest-environment-node/-/jest-environment-node-24.9.0.tgz#333d2d2796f9687f2aeebf0742b519f33c1cbfd3"
1340013460
integrity sha512-6d4V2f4nxzIzwendo27Tr0aFm+IXWa0XEUnaH6nU0FMaozxovt+sfRvh4J47wL1OvF83I3SSTu0XK+i4Bqe7uA==

0 commit comments

Comments
 (0)