Skip to content

Commit 46f03cf

Browse files
committed
chore: it builds :O
1 parent ed441be commit 46f03cf

File tree

12 files changed

+233
-320
lines changed

12 files changed

+233
-320
lines changed

dev-packages/rollup-utils/bundleHelpers.mjs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,46 @@
33
*/
44

55
import { builtinModules } from 'module';
6-
6+
import path from 'node:path';
7+
import fs from 'node:fs';
78
import deepMerge from 'deepmerge';
89

910
import {
1011
makeBrowserBuildPlugin,
11-
makeCleanupPlugin,
1212
makeIsDebugBuildPlugin,
13-
makeLicensePlugin,
1413
makeRrwebBuildPlugin,
1514
makeSetSDKSourcePlugin,
16-
makeSucrasePlugin,
15+
makeBannerOptions,
1716
makeTerserPlugin,
1817
} from './plugins/index.mjs';
1918
import { mergePlugins } from './utils.mjs';
20-
import { makeProductionReplacePlugin } from './plugins/npmPlugins.mjs';
2119

2220
const BUNDLE_VARIANTS = ['.js', '.min.js', '.debug.min.js'];
2321

22+
const packageDotJSON = JSON.parse(fs.readFileSync(path.resolve(process.cwd(), './package.json'), { encoding: 'utf8' }));
23+
2424
export function makeBaseBundleConfig(options) {
25-
const { bundleType, entrypoints, licenseTitle, outputFileBase, packageSpecificConfig, sucrase } = options;
25+
const { bundleType, entrypoints, licenseTitle, outputFileBase, packageSpecificConfig } = options;
2626

27-
const sucrasePlugin = makeSucrasePlugin({}, sucrase);
28-
const cleanupPlugin = makeCleanupPlugin();
2927
const markAsBrowserBuildPlugin = makeBrowserBuildPlugin(true);
30-
const licensePlugin = makeLicensePlugin(licenseTitle);
28+
const banner = makeBannerOptions(licenseTitle, packageDotJSON.version);
3129
const rrwebBuildPlugin = makeRrwebBuildPlugin({
3230
excludeIframe: false,
3331
excludeShadowDom: false,
3432
});
35-
const productionReplacePlugin = makeProductionReplacePlugin();
3633

3734
// used by `@sentry/browser`
3835
const standAloneBundleConfig = {
3936
output: {
37+
banner,
4038
format: 'iife',
4139
name: 'Sentry',
4240
intro: () => {
4341
return 'exports = window.Sentry || {};';
4442
},
4543
},
4644
context: 'window',
47-
plugins: [rrwebBuildPlugin, markAsBrowserBuildPlugin, licensePlugin],
45+
plugins: [rrwebBuildPlugin, markAsBrowserBuildPlugin],
4846
};
4947

5048
// used by `@sentry/wasm` & pluggable integrations from core/browser (bundles which need to be combined with a stand-alone SDK bundle)
@@ -55,7 +53,7 @@ export function makeBaseBundleConfig(options) {
5553
format: 'cjs',
5654

5755
// code to add before the CJS wrapper
58-
banner: '(function (__window) {',
56+
banner: `${banner}\n(function (__window) {`,
5957

6058
// code to add just inside the CJS wrapper, before any of the wrapped code
6159
intro: 'var exports = {};',
@@ -78,14 +76,15 @@ export function makeBaseBundleConfig(options) {
7876
// code to add after the CJS wrapper
7977
footer: '}(window));',
8078
},
81-
plugins: [rrwebBuildPlugin, markAsBrowserBuildPlugin, licensePlugin],
79+
plugins: [rrwebBuildPlugin, markAsBrowserBuildPlugin],
8280
};
8381

8482
const workerBundleConfig = {
8583
output: {
84+
banner,
8685
format: 'esm',
8786
},
88-
plugins: [makeTerserPlugin(), licensePlugin],
87+
plugins: [makeTerserPlugin()],
8988
// Don't bundle any of Node's core modules
9089
external: builtinModules,
9190
};
@@ -110,7 +109,6 @@ export function makeBaseBundleConfig(options) {
110109
strict: false,
111110
esModule: false,
112111
},
113-
plugins: [sucrasePlugin, cleanupPlugin],
114112
treeshake: 'smallest',
115113
};
116114

@@ -144,25 +142,35 @@ export function makeBundleConfigVariants(baseConfig, options = {}) {
144142
const terserPlugin = makeTerserPlugin();
145143
const setSdkSourcePlugin = makeSetSDKSourcePlugin('cdn');
146144

145+
const baseOutput = baseConfig.output;
146+
if (!baseOutput || Array.isArray(baseOutput)) {
147+
throw new Error('Base config must have a single output object');
148+
}
149+
150+
const baseOutputEntryFileNames = baseOutput.entryFileNames;
151+
if (typeof baseOutputEntryFileNames !== 'function') {
152+
throw new Error('Base config must have a function for entryFileNames');
153+
}
154+
147155
// The additional options to use for each variant we're going to create.
148156
const variantSpecificConfigMap = {
149157
'.js': {
150158
output: {
151-
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.js`,
159+
entryFileNames: chunkInfo => `${baseOutputEntryFileNames(chunkInfo)}.js`,
152160
},
153161
plugins: [includeDebuggingPlugin, setSdkSourcePlugin],
154162
},
155163

156164
'.min.js': {
157165
output: {
158-
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.min.js`,
166+
entryFileNames: chunkInfo => `${baseOutputEntryFileNames(chunkInfo)}.min.js`,
159167
},
160168
plugins: [stripDebuggingPlugin, setSdkSourcePlugin, terserPlugin],
161169
},
162170

163171
'.debug.min.js': {
164172
output: {
165-
entryFileNames: chunkInfo => `${baseConfig.output.entryFileNames(chunkInfo)}.debug.min.js`,
173+
entryFileNames: chunkInfo => `${baseOutputEntryFileNames(chunkInfo)}.debug.min.js`,
166174
},
167175
plugins: [includeDebuggingPlugin, setSdkSourcePlugin, terserPlugin],
168176
},
@@ -172,10 +180,12 @@ export function makeBundleConfigVariants(baseConfig, options = {}) {
172180
if (!BUNDLE_VARIANTS.includes(variant)) {
173181
throw new Error(`Unknown bundle variant requested: ${variant}`);
174182
}
183+
175184
return deepMerge(baseConfig, variantSpecificConfigMap[variant], {
176185
// Merge the plugin arrays and make sure the end result is in the correct order. Everything else can use the
177186
// default merge strategy.
178187
customMerge: key => (key === 'plugins' ? mergePlugins : undefined),
179188
});
180189
});
181190
}
191+
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
// TODO Is this necessary?
2-
import * as plugins from './plugins/index.mjs';
3-
export { plugins };
4-
1+
export * from './plugins/index.mjs';
52
export * from './bundleHelpers.mjs';
63
export * from './npmHelpers.mjs';
74
export { insertAt } from './utils.mjs';
5+

dev-packages/rollup-utils/npmHelpers.mjs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ import deepMerge from 'deepmerge';
1313

1414
import { defineConfig } from 'rollup';
1515
import {
16-
makeCleanupPlugin,
1716
makeDebugBuildStatementReplacePlugin,
1817
makeProductionReplacePlugin,
1918
makeRrwebBuildPlugin,
20-
makeSucrasePlugin,
2119
} from './plugins/index.mjs';
2220
import { makePackageNodeEsm } from './plugins/make-esm-plugin.mjs';
2321
import { mergePlugins } from './utils.mjs';
@@ -33,13 +31,10 @@ export function makeBaseNPMConfig(options = {}) {
3331
entrypoints = ['src/index.ts'],
3432
hasBundles = false,
3533
packageSpecificConfig = {},
36-
sucrase = {},
3734
bundledBuiltins = [],
3835
} = options;
3936

40-
const sucrasePlugin = makeSucrasePlugin({}, sucrase);
4137
const debugBuildStatementReplacePlugin = makeDebugBuildStatementReplacePlugin();
42-
const cleanupPlugin = makeCleanupPlugin();
4338
const rrwebBuildPlugin = makeRrwebBuildPlugin({
4439
excludeShadowDom: undefined,
4540
excludeIframe: undefined,
@@ -62,9 +57,6 @@ export function makeBaseNPMConfig(options = {}) {
6257
preset: 'es2015',
6358
},
6459

65-
// don't add `"use strict"` to the top of cjs files
66-
strict: false,
67-
6860
// do TS-3.8-style exports
6961
// exports.dogs = are.great
7062
// rather than TS-3.9-style exports
@@ -73,12 +65,6 @@ export function makeBaseNPMConfig(options = {}) {
7365
// get: () => are.great,
7466
// });
7567
externalLiveBindings: false,
76-
77-
// Don't call `Object.freeze` on the results of `import * as someModule from '...'`
78-
// (We don't need it, so why waste the bytes?)
79-
freeze: false,
80-
81-
interop: 'esModule',
8268
},
8369

8470
treeshake: {
@@ -92,7 +78,7 @@ export function makeBaseNPMConfig(options = {}) {
9278
},
9379
},
9480

95-
plugins: [sucrasePlugin, debugBuildStatementReplacePlugin, rrwebBuildPlugin, cleanupPlugin],
81+
plugins: [debugBuildStatementReplacePlugin, rrwebBuildPlugin],
9682

9783
// don't include imported modules from outside the package in the final output
9884
external: [
@@ -109,20 +95,30 @@ export function makeBaseNPMConfig(options = {}) {
10995
});
11096
}
11197

98+
// TODO: Instead of runtime checks, we should use TypeScript to ensure the base config is valid.
11299
export function makeNPMConfigVariants(baseConfig, options = {}) {
113100
const { emitEsm = true, emitCjs = true, splitDevProd = false } = options;
101+
const baseOutput = baseConfig.output;
102+
if (!baseOutput || Array.isArray(baseOutput)) {
103+
throw new Error('Base config must have a single output object');
104+
}
105+
106+
const baseOutputDir = baseOutput.dir;
107+
if (typeof baseOutputDir !== 'string') {
108+
throw new Error('Base config must have a string for dir');
109+
}
114110

115111
const variantSpecificConfigs = [];
116112

117113
if (emitCjs) {
118114
if (splitDevProd) {
119-
variantSpecificConfigs.push({ output: { format: 'cjs', dir: path.join(baseConfig.output.dir, 'cjs/dev') } });
115+
variantSpecificConfigs.push({ output: { format: 'cjs', dir: path.join(baseOutputDir, 'cjs/dev') } });
120116
variantSpecificConfigs.push({
121-
output: { format: 'cjs', dir: path.join(baseConfig.output.dir, 'cjs/prod') },
117+
output: { format: 'cjs', dir: path.join(baseOutputDir, 'cjs/prod') },
122118
plugins: [makeProductionReplacePlugin()],
123119
});
124120
} else {
125-
variantSpecificConfigs.push({ output: { format: 'cjs', dir: path.join(baseConfig.output.dir, 'cjs') } });
121+
variantSpecificConfigs.push({ output: { format: 'cjs', dir: path.join(baseOutputDir, 'cjs') } });
126122
}
127123
}
128124

@@ -131,22 +127,22 @@ export function makeNPMConfigVariants(baseConfig, options = {}) {
131127
variantSpecificConfigs.push({
132128
output: {
133129
format: 'esm',
134-
dir: path.join(baseConfig.output.dir, 'esm/dev'),
130+
dir: path.join(baseOutputDir, 'esm/dev'),
135131
plugins: [makePackageNodeEsm()],
136132
},
137133
});
138134
variantSpecificConfigs.push({
139135
output: {
140136
format: 'esm',
141-
dir: path.join(baseConfig.output.dir, 'esm/prod'),
137+
dir: path.join(baseOutputDir, 'esm/prod'),
142138
plugins: [makeProductionReplacePlugin(), makePackageNodeEsm()],
143139
},
144140
});
145141
} else {
146142
variantSpecificConfigs.push({
147143
output: {
148144
format: 'esm',
149-
dir: path.join(baseConfig.output.dir, 'esm'),
145+
dir: path.join(baseOutputDir, 'esm'),
150146
plugins: [makePackageNodeEsm()],
151147
},
152148
});

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

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,20 @@
1010

1111
import * as childProcess from 'child_process';
1212

13-
import replace from '@rollup/plugin-replace';
13+
import { replacePlugin } from 'rolldown/plugins';
1414
import terser from '@rollup/plugin-terser';
15-
import license from 'rollup-plugin-license';
1615

1716
/**
1817
* Create a plugin to add an identification banner to the top of stand-alone bundles.
1918
*
2019
* @param title The title to use for the SDK, if not the package name
20+
* @param version The version of the SDK
2121
* @returns An instance of the `rollup-plugin-license` plugin
2222
*/
23-
export function makeLicensePlugin(title) {
23+
export function makeBannerOptions(title, version) {
2424
const commitHash = childProcess.execSync('git rev-parse --short HEAD', { encoding: 'utf-8' }).trim();
2525

26-
const plugin = license({
27-
banner: {
28-
content: `/*! <%= data.title %> <%= pkg.version %> (${commitHash}) | https://github.com/getsentry/sentry-javascript */`,
29-
data: { title },
30-
},
31-
});
32-
33-
// give it a nicer name for later, when we'll need to sort the plugins
34-
plugin.name = 'license';
35-
36-
return plugin;
26+
return `/*! ${title} ${version} (${commitHash}) | https://github.com/getsentry/sentry-javascript */`;
3727
}
3828

3929
/**
@@ -44,28 +34,32 @@ export function makeLicensePlugin(title) {
4434
* 'false`
4535
*/
4636
export function makeIsDebugBuildPlugin(includeDebugging) {
47-
return replace({
48-
// TODO `preventAssignment` will default to true in version 5.x of the replace plugin, at which point we can get rid
49-
// of this. (It actually makes no difference in this case whether it's true or false, since we never assign to
50-
// `__SENTRY_DEBUG__`, but if we don't give it a value, it will spam with warnings.)
51-
preventAssignment: true,
52-
values: {
37+
return replacePlugin(
38+
{
5339
// Flags in current package
54-
__DEBUG_BUILD__: includeDebugging,
40+
__DEBUG_BUILD__: JSON.stringify(includeDebugging),
5541
// Flags in built monorepo dependencies, from which the bundle pulls
56-
__SENTRY_DEBUG__: includeDebugging,
42+
__SENTRY_DEBUG__: JSON.stringify(includeDebugging),
5743
},
58-
});
44+
{
45+
// TODO `preventAssignment` will default to true in version 5.x of the replace plugin, at which point we can get rid
46+
// of this. (It actually makes no difference in this case whether it's true or false, since we never assign to
47+
// `__SENTRY_DEBUG__`, but if we don't give it a value, it will spam with warnings.)
48+
preventAssignment: true,
49+
},
50+
);
5951
}
6052

6153
export function makeSetSDKSourcePlugin(sdkSource) {
62-
return replace({
63-
preventAssignment: false,
64-
delimiters: ['', ''],
65-
values: {
54+
return replacePlugin(
55+
{
6656
'/* __SENTRY_SDK_SOURCE__ */': `return ${JSON.stringify(sdkSource)};`,
6757
},
68-
});
58+
{
59+
preventAssignment: false,
60+
delimiters: ['', ''],
61+
},
62+
);
6963
}
7064

7165
/**
@@ -75,13 +69,15 @@ export function makeSetSDKSourcePlugin(sdkSource) {
7569
* @returns An instance of the `replace` plugin to do the replacement of the magic string with `true` or 'false`
7670
*/
7771
export function makeBrowserBuildPlugin(isBrowserBuild) {
78-
return replace({
79-
// TODO This will be the default in the next version of the `replace` plugin
80-
preventAssignment: true,
81-
values: {
82-
__SENTRY_BROWSER_BUNDLE__: isBrowserBuild,
72+
return replacePlugin(
73+
{
74+
__SENTRY_BROWSER_BUNDLE__: JSON.stringify(!!isBrowserBuild),
8375
},
84-
});
76+
{
77+
// TODO This will be the default in the next version of the `replace` plugin
78+
preventAssignment: true,
79+
},
80+
);
8581
}
8682

8783
// `terser` options reference: https://github.com/terser/terser#api-reference
@@ -144,3 +140,4 @@ export function makeTerserPlugin() {
144140
},
145141
});
146142
}
143+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './bundlePlugins.mjs';
22
export * from './npmPlugins.mjs';
3+

dev-packages/rollup-utils/plugins/make-esm-plugin.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ export function makePackageNodeEsm() {
3333
},
3434
};
3535
}
36+

0 commit comments

Comments
 (0)