forked from mixpanel/mixpanel-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
437 lines (406 loc) · 11.7 KB
/
Copy pathrollup.config.mjs
File metadata and controls
437 lines (406 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import alias from '@rollup/plugin-alias';
import closureCompiler from '@ampproject/rollup-plugin-closure-compiler';
import commonjs from '@rollup/plugin-commonjs';
import fs from 'fs';
import path from 'path';
import esbuild from 'rollup-plugin-esbuild';
import nodeResolve from '@rollup/plugin-node-resolve';
import swc from '@rollup/plugin-swc';
import browserTestBuilds from './tests/browser/client/rollup.config.mjs';
// Capture the hash of async-loaded bundles so we can inject the correct filenames into the main build
const asyncBundleManifest = {};
const COMPILED_RRWEB_PATH = `build/rrweb-compiled.js`;
const BUNDLED_RRWEB_PATH = `build/rrweb-bundled.js`;
const ASYNC_MODULE_BUILD_PATH = `build/async-modules`;
const RECORDER_BUNDLE_NAME = `mixpanel_recorder`;
const RECORDER_MIN_BUNDLE_NAME = `mixpanel_recorder_min`;
const TARGETING_BUNDLE_NAME = `mixpanel_targeting`;
const TARGETING_MIN_BUNDLE_NAME = `mixpanel_targeting_min`;
// Delete output files at build start so build errors don't silently use stale files
let hasCleanedOnFull = false;
const cleanOnRebuild = () => {
let filesToClean = [];
return {
name: `clean-on-rebuild`,
options(opts) {
const outputs = Array.isArray(opts.output) ? opts.output : [opts.output];
filesToClean = outputs.flatMap(o => {
if (o.file) {
return [o.file];
}
if (o.dir && o.entryFileNames) {
if (!fs.existsSync(o.dir)) return [];
// match against regexes like mixpanel-recorder-[hash].js to find the actual output file(s) to clean up
const regex = new RegExp(`^` + o.entryFileNames.replace(/\./g, `\\.`).replace(`[hash]`, `.+`));
return fs.readdirSync(o.dir).filter(f => regex.test(f)).map(f => path.join(o.dir, f));
}
return [];
});
},
buildStart() {
if (process.env.FULL && !hasCleanedOnFull) {
if (fs.existsSync(`build`)) {
fs.rmSync(`build`, {recursive: true});
}
hasCleanedOnFull = true;
return;
}
for (const file of filesToClean) {
if (fs.existsSync(file)) {
fs.unlinkSync(file);
}
}
}
};
};
// Capture the resolved (hashed) output filenames from recorder/targeting builds.
// Stores them in `asyncBundleManifest` so `injectAsyncBundleNames` can inject them into the main build.
const writeToManifest = () => ({
name: `write-to-manifest`,
generateBundle(options, bundle) {
for (const chunk of Object.values(bundle)) {
if (chunk.type === `chunk`) {
asyncBundleManifest[options.name] = chunk.fileName;
}
}
}
});
// Replace __MP_*__ placeholders in src/config.js with build-time values.
// For the main library build (which runs after), they will be the real hashed filenames.
const injectAsyncBundleNames = () => ({
name: `inject-async-bundle-names`,
renderChunk(code, _chunk, outputOptions) {
const isMinified = outputOptions.file && outputOptions.file.endsWith(`.min.js`);
let recorderBundleName = RECORDER_BUNDLE_NAME;
let targetingBundleName = TARGETING_BUNDLE_NAME;
if (isMinified) {
recorderBundleName = RECORDER_MIN_BUNDLE_NAME;
targetingBundleName = TARGETING_MIN_BUNDLE_NAME;
}
if (!asyncBundleManifest[recorderBundleName] || !asyncBundleManifest[targetingBundleName]) {
throw new Error(`Async bundle names not found in manifest. Manifest contents: ${JSON.stringify(asyncBundleManifest)}`);
}
const replaced = code
.replace(`__MP_RECORDER_FILENAME__`, asyncBundleManifest[recorderBundleName])
.replace(`__MP_TARGETING_FILENAME__`, asyncBundleManifest[targetingBundleName]);
return { code: replaced, map: null };
}
});
const withBasePlugins = (builds) => builds.map((build) => {
return {
...build,
plugins: [
cleanOnRebuild(),
...(build.plugins || [])
]
};
});
const aliasRrweb = () => alias({
entries: [
{ find: /rrweb-entrypoint(?:\.js)?$/, replacement: COMPILED_RRWEB_PATH },
]
});
const copyTypes = () => ({
name: `copy-types`,
generateBundle(_options, bundle) {
try {
for (const builtFileName of Object.keys(bundle)) {
const buildDir = `build`;
const types = fs.readFileSync(`src/index.d.ts`, `utf8`);
const builtFile = path.basename(builtFileName, path.extname(builtFileName)) + `.d.ts`;
fs.writeFileSync(path.join(buildDir, builtFile), types);
}
} catch (err) {
console.warn(`Could not copy type files:`, err.message);
throw err;
}
}
});
const COMMON_CLOSURE_FLAGS = {
compilation_level: `ADVANCED_OPTIMIZATIONS`,
language_in: `ECMASCRIPT5`,
externs: [`src/externs.js`],
create_source_map: true,
};
const MINIFY = process.env.MINIFY || process.env.FULL;
/**
* Async bundles for the recorder and targeting modules.
*
* These are loaded on-demand by the main library at runtime via script injection.
* Filenames include a content hash so the main build can reference an exact, immutable
* bundle — guaranteeing the async module matches what the library expects.
* The hash is captured in `asyncBundleManifest` and injected into the main build
* via `injectAsyncBundleNames`.
*
* We also produce non-hashed "legacy" versions of each bundle for backward compatibility
* with customers who proxy specific filenames from the Mixpanel CDN.
*/
const ASYNC_BUNDLE_BUILDS = [
// Recorder bundle (wraps rrweb)
{
input: `src/recorder/index.js`,
output: [
{
dir: ASYNC_MODULE_BUILD_PATH,
entryFileNames: `mixpanel-recorder-[hash].js`,
name: RECORDER_BUNDLE_NAME,
format: `iife`,
},
{
file: `build/mixpanel-recorder.js`,
name: `legacy_recorder_bundle`,
format: `iife`,
},
...(MINIFY
? [
{
dir: ASYNC_MODULE_BUILD_PATH,
entryFileNames: `mixpanel-recorder-[hash].min.js`,
name: RECORDER_MIN_BUNDLE_NAME,
format: `iife`,
plugins: [esbuild({target: `es5`, minify: true, sourceMap: true})],
sourcemap: true,
},
{
file: `build/mixpanel-recorder.min.js`,
name: `legacy_recorder_min_bundle`,
format: `iife`,
plugins: [esbuild({target: `es5`, minify: true, sourceMap: true})],
sourcemap: true,
},
]
: []),
],
plugins: [aliasRrweb(), writeToManifest()],
},
// Targeting bundle (feature flags / experiments)
{
input: `src/targeting/index.js`,
output: [
{
dir: ASYNC_MODULE_BUILD_PATH,
entryFileNames: `mixpanel-targeting-[hash].js`,
name: TARGETING_BUNDLE_NAME,
format: `iife`,
},
{
file: `build/mixpanel-targeting.js`,
name: `legacy_targeting_bundle`,
format: `iife`,
},
...(MINIFY
? [
{
dir: ASYNC_MODULE_BUILD_PATH,
entryFileNames: `mixpanel-targeting-[hash].min.js`,
name: TARGETING_MIN_BUNDLE_NAME,
format: `iife`,
plugins: [esbuild({target: `es5`, minify: true, sourceMap: true})],
sourcemap: true,
},
{
file: `build/mixpanel-targeting.min.js`,
name: `legacy_targeting_min_bundle`,
format: `iife`,
plugins: [esbuild({target: `es5`, minify: true, sourceMap: true})],
sourcemap: true,
},
]
: []),
],
plugins: [commonjs(), nodeResolve({browser: true}), writeToManifest()],
},
];
// Main builds used to develop / iterate quickly
const MAIN_BUILDS = [
{
'input': `src/recorder/rrweb-entrypoint.js`,
'output': [
{
file: BUNDLED_RRWEB_PATH,
format: `es`,
}
],
plugins: [nodeResolve({browser: true})]
},
{
'input': BUNDLED_RRWEB_PATH,
'output': [
{
file: COMPILED_RRWEB_PATH,
format: `es`,
}
],
plugins: [swc({swc: {jsc: {target: `es5`}}})]
},
...ASYNC_BUNDLE_BUILDS,
// IIFE main mixpanel build
{
input: `src/loaders/loader-globals.js`,
output: [
{
file: `build/mixpanel.globals.js`,
name: `mixpanel`,
format: `iife`,
},
...(MINIFY
? [
{
file: `build/mixpanel.min.js`,
format: `iife`,
plugins: [closureCompiler(COMMON_CLOSURE_FLAGS)],
},
]
: []),
],
plugins: [
injectAsyncBundleNames(),
nodeResolve({
browser: true,
main: true,
jsnext: true,
})
]
},
];
const ALL_BUILDS = [
...MAIN_BUILDS,
// Minified snippets for loading mixpanel
{
input: `src/loaders/mixpanel-jslib-snippet.js`,
output: [
{
file: `build/mixpanel-jslib-snippet.min.js`,
plugins: [closureCompiler(COMMON_CLOSURE_FLAGS)]
},
{
file: `build/mixpanel-jslib-snippet.min.test.js`,
plugins: [closureCompiler({...COMMON_CLOSURE_FLAGS, define: `MIXPANEL_LIB_URL="../build/mixpanel.min.js"`})],
}
],
},
// IIFE mixpanel snippet loader
{
input: `src/loaders/mixpanel-js-wrapper.js`,
output: [
{
file: `build/mixpanel-js-wrapper.js`,
},
{
file: `build/mixpanel-js-wrapper.min.js`,
plugins: [closureCompiler(COMMON_CLOSURE_FLAGS)],
}
],
},
// IIFE mixpanel core with bundled recorder
{
input: `src/loaders/loader-globals-with-recorder.js`,
output: [
{
file: `build/mixpanel-with-recorder.js`,
name: `mixpanel`,
format: `iife`,
},
{
file: `build/mixpanel-with-recorder.min.js`,
name: `mixpanel`,
format: `iife`,
plugins: [esbuild({target: `es5`, minify: true, sourceMap: true})],
},
],
plugins: [
aliasRrweb(),
nodeResolve({
browser: true,
main: true,
jsnext: true,
}),
copyTypes(),
],
},
// Modules builds that are bundled with the recorder and targeting
{
input: `src/loaders/loader-module.js`,
output: [
{
file: `build/mixpanel.cjs.js`,
name: `mixpanel`,
format: `cjs`,
},
{
file: `build/mixpanel.amd.js`,
name: `mixpanel`,
format: `amd`,
},
{
file: `build/mixpanel.umd.js`,
name: `mixpanel`,
format: `umd`,
},
{
file: `build/mixpanel.module.js`,
name: `mixpanel`,
format: `es`,
},
],
plugins: [
commonjs(),
aliasRrweb(),
nodeResolve({
browser: true,
main: true,
jsnext: true,
}),
copyTypes(),
],
},
// Alternative CJS builds without recorder
{
input: `src/loaders/loader-module-core.js`,
output: [
{
file: `build/mixpanel-core.cjs.js`,
name: `mixpanel`,
format: `cjs`,
},
],
plugins: [
aliasRrweb(),
nodeResolve({
browser: true,
main: true,
jsnext: true,
}),
copyTypes(),
],
},
{
input: `src/loaders/loader-module-with-async-modules.js`,
output: [
{
file: `build/mixpanel-with-async-modules.cjs.js`,
name: `mixpanel`,
format: `cjs`,
},
// Backward compatibility: keep old output filename for existing users
{
file: `build/mixpanel-with-async-recorder.cjs.js`,
name: `mixpanel`,
format: `cjs`,
},
],
plugins: [
injectAsyncBundleNames(),
aliasRrweb(),
nodeResolve({
browser: true,
main: true,
jsnext: true,
}),
copyTypes(),
],
},
];
const srcBuilds = process.env.FULL ? ALL_BUILDS : MAIN_BUILDS;
const builds = [...srcBuilds, ...browserTestBuilds];
export default withBasePlugins(builds);