Skip to content

Commit 4805008

Browse files
committed
feat(browser): Separate dev/prod browser builds
1 parent 94190f8 commit 4805008

File tree

7 files changed

+70
-18
lines changed

7 files changed

+70
-18
lines changed

dev-packages/rollup-utils/bundleHelpers.mjs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
makeCleanupPlugin,
1212
makeCommonJSPlugin,
1313
makeIsDebugBuildPlugin,
14-
makeJsonPlugin,
1514
makeLicensePlugin,
1615
makeNodeResolvePlugin,
1716
makeRrwebBuildPlugin,
@@ -20,6 +19,7 @@ import {
2019
makeTerserPlugin,
2120
} from './plugins/index.mjs';
2221
import { mergePlugins } from './utils.mjs';
22+
import { makeProductionReplacePlugin } from './plugins/npmPlugins.mjs';
2323

2424
const BUNDLE_VARIANTS = ['.js', '.min.js', '.debug.min.js'];
2525

@@ -35,14 +35,13 @@ export function makeBaseBundleConfig(options) {
3535
excludeIframe: false,
3636
excludeShadowDom: false,
3737
});
38+
const productionReplacePlugin = makeProductionReplacePlugin();
3839

3940
// The `commonjs` plugin is the `esModuleInterop` of the bundling world. When used with `transformMixedEsModules`, it
4041
// will include all dependencies, imported or required, in the final bundle. (Without it, CJS modules aren't included
4142
// at all, and without `transformMixedEsModules`, they're only included if they're imported, not if they're required.)
4243
const commonJSPlugin = makeCommonJSPlugin({ transformMixedEsModules: true });
4344

44-
const jsonPlugin = makeJsonPlugin();
45-
4645
// used by `@sentry/browser`
4746
const standAloneBundleConfig = {
4847
output: {
@@ -119,7 +118,7 @@ export function makeBaseBundleConfig(options) {
119118
strict: false,
120119
esModule: false,
121120
},
122-
plugins: [sucrasePlugin, nodeResolvePlugin, cleanupPlugin],
121+
plugins: [productionReplacePlugin, sucrasePlugin, nodeResolvePlugin, cleanupPlugin],
123122
treeshake: 'smallest',
124123
};
125124

dev-packages/rollup-utils/npmHelpers.mjs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
makeCleanupPlugin,
1717
makeDebugBuildStatementReplacePlugin,
1818
makeNodeResolvePlugin,
19+
makeProductionReplacePlugin,
1920
makeRrwebBuildPlugin,
2021
makeSucrasePlugin,
2122
} from './plugins/index.mjs';
@@ -114,22 +115,47 @@ export function makeBaseNPMConfig(options = {}) {
114115
}
115116

116117
export function makeNPMConfigVariants(baseConfig, options = {}) {
117-
const { emitEsm = true, emitCjs = true } = options;
118+
const { emitEsm = true, emitCjs = true, splitDevProd = false } = options;
118119

119120
const variantSpecificConfigs = [];
120121

121122
if (emitCjs) {
122-
variantSpecificConfigs.push({ output: { format: 'cjs', dir: path.join(baseConfig.output.dir, 'cjs') } });
123+
if (splitDevProd) {
124+
variantSpecificConfigs.push({ output: { format: 'cjs', dir: path.join(baseConfig.output.dir, 'cjs/dev') } });
125+
variantSpecificConfigs.push({
126+
output: { format: 'cjs', dir: path.join(baseConfig.output.dir, 'cjs/prod') },
127+
plugins: [makeProductionReplacePlugin()]
128+
});
129+
} else {
130+
variantSpecificConfigs.push({ output: { format: 'cjs', dir: path.join(baseConfig.output.dir, 'cjs') } });
131+
}
123132
}
124133

125134
if (emitEsm) {
126-
variantSpecificConfigs.push({
127-
output: {
128-
format: 'esm',
129-
dir: path.join(baseConfig.output.dir, 'esm'),
130-
plugins: [makePackageNodeEsm()],
131-
},
132-
});
135+
if (splitDevProd) {
136+
variantSpecificConfigs.push({
137+
output: {
138+
format: 'esm',
139+
dir: path.join(baseConfig.output.dir, 'esm/dev'),
140+
plugins: [makePackageNodeEsm()],
141+
},
142+
});
143+
variantSpecificConfigs.push({
144+
output: {
145+
format: 'esm',
146+
dir: path.join(baseConfig.output.dir, 'esm/prod'),
147+
plugins: [makeProductionReplacePlugin(), makePackageNodeEsm()],
148+
},
149+
});
150+
} else {
151+
variantSpecificConfigs.push({
152+
output: {
153+
format: 'esm',
154+
dir: path.join(baseConfig.output.dir, 'esm'),
155+
plugins: [makePackageNodeEsm()],
156+
},
157+
});
158+
}
133159
}
134160

135161
return variantSpecificConfigs.map(variant => deepMerge(baseConfig, variant));

dev-packages/rollup-utils/plugins/npmPlugins.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@ export function makeDebugBuildStatementReplacePlugin() {
125125
});
126126
}
127127

128+
export function makeProductionReplacePlugin() {
129+
const pattern = /\/\* development-only \*\/[\s\S]*?\/\* end-development-only \*\//g;
130+
131+
function stripDevBlocks(code) {
132+
if (!code) return null;
133+
if (!pattern.test(code)) return null;
134+
const replaced = code.replace(pattern, '');
135+
return { code: replaced, map: null };
136+
}
137+
138+
return {
139+
name: 'remove-dev-mode-blocks',
140+
renderChunk(code) {
141+
return stripDevBlocks(code);
142+
},
143+
};
144+
}
145+
128146
/**
129147
* Creates a plugin to replace build flags of rrweb with either a constant (if passed true/false) or with a safe statement that:
130148
* a) evaluates to `true`

dev-packages/rollup-utils/utils.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function mergePlugins(pluginsA, pluginsB) {
2121
// here.
2222
// Additionally, the excludeReplay plugin must run before TS/Sucrase so that we can eliminate the replay code
2323
// before anything is type-checked (TS-only) and transpiled.
24-
const order = ['excludeReplay', 'typescript', 'sucrase', '...', 'terser', 'license', 'output-base64-worker-script'];
24+
const order = ['remove-dev-mode-blocks', 'excludeReplay', 'typescript', 'sucrase', '...', 'terser', 'license', 'output-base64-worker-script'];
2525
const sortKeyA = order.includes(a.name) ? a.name : '...';
2626
const sortKeyB = order.includes(b.name) ? b.name : '...';
2727

packages/browser/package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,23 @@
1212
"files": [
1313
"/build/npm"
1414
],
15-
"main": "build/npm/cjs/index.js",
16-
"module": "build/npm/esm/index.js",
15+
"main": "build/npm/cjs/prod/index.js",
16+
"module": "build/npm/esm/prod/index.js",
1717
"types": "build/npm/types/index.d.ts",
1818
"exports": {
1919
"./package.json": "./package.json",
2020
".": {
2121
"import": {
2222
"types": "./build/npm/types/index.d.ts",
23-
"default": "./build/npm/esm/index.js"
23+
"development": "./build/npm/esm/dev/index.js",
24+
"production": "./build/npm/esm/prod/index.js",
25+
"default": "./build/npm/esm/prod/index.js"
2426
},
2527
"require": {
2628
"types": "./build/npm/types/index.d.ts",
27-
"default": "./build/npm/cjs/index.js"
29+
"development": "./build/npm/cjs/dev/index.js",
30+
"production": "./build/npm/cjs/prod/index.js",
31+
"default": "./build/npm/cjs/prod/index.js"
2832
}
2933
}
3034
},

packages/browser/rollup.npm.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ export default makeNPMConfigVariants(
1616
},
1717
},
1818
}),
19+
{ splitDevProd: true }
1920
);

packages/browser/src/sdk.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { browserSessionIntegration } from './integrations/browsersession';
1515
import { globalHandlersIntegration } from './integrations/globalhandlers';
1616
import { httpContextIntegration } from './integrations/httpcontext';
1717
import { linkedErrorsIntegration } from './integrations/linkederrors';
18+
import { spotlightBrowserIntegration } from './integrations/spotlight';
1819
import { defaultStackParser } from './stack-parsers';
1920
import { makeFetchTransport } from './transports/fetch';
2021
import { checkAndWarnIfIsEmbeddedBrowserExtension } from './utils/detectBrowserExtension';
@@ -37,6 +38,9 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
3738
dedupeIntegration(),
3839
httpContextIntegration(),
3940
browserSessionIntegration(),
41+
/* development-only */
42+
spotlightBrowserIntegration(),
43+
/* end-development-only */
4044
];
4145
}
4246

0 commit comments

Comments
 (0)