-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathrollup.config.js
294 lines (269 loc) · 11.3 KB
/
rollup.config.js
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
import babel from '@rollup/plugin-babel'
import json from '@rollup/plugin-json'
import resolve from '@rollup/plugin-node-resolve'
import typescript from '@rollup/plugin-typescript'
import { dts } from 'rollup-plugin-dts'
import terser from '@rollup/plugin-terser'
import { visualizer } from 'rollup-plugin-visualizer'
import commonjs from '@rollup/plugin-commonjs'
import fs from 'fs'
import path from 'path'
// eslint-disable-next-line no-undef
const WRITE_MANGLED_PROPERTIES = process.env.WRITE_MANGLED_PROPERTIES
const nameCachePath = './terser-mangled-names.json'
let nameCache = {}
const plugins = (es5) => [
json(),
resolve({ browser: true }),
typescript({ sourceMap: true, outDir: './dist' }),
commonjs(),
babel({
extensions: ['.js', '.jsx', '.ts', '.tsx'],
babelHelpers: 'bundled',
plugins: [
'@babel/plugin-transform-nullish-coalescing-operator',
// Explicitly included so we transform 1 ** 2 to Math.pow(1, 2) for ES6 compatability
'@babel/plugin-transform-exponentiation-operator',
],
presets: [
[
'@babel/preset-env',
{
loose: true,
targets: es5
? [
'> 0.5%, last 2 versions, Firefox ESR, not dead',
'chrome > 62',
'firefox > 59',
'ios_saf >= 6.1',
'opera > 50',
'safari > 12',
'IE 11',
]
: [
'> 0.5%, last 2 versions, Firefox ESR, not dead',
'chrome > 62',
'firefox > 59',
'ios_saf >= 10.3',
'opera > 50',
'safari > 12',
],
},
],
],
}),
terser({
nameCache: WRITE_MANGLED_PROPERTIES ? nameCache : undefined, // using a shared nameCache leads to race conditions and broken builds, so don't use in general, only when writing the mangled names
toplevel: true,
compress: {
ecma: es5 ? 5 : 6,
},
format: {
comments: false,
},
mangle: es5
? true // Don't mangle properties in the es5 build, as it relies on helpers which don't work well with mangling.
: {
// Note:
// PROPERTY MANGLING CAN BREAK YOUR CODE
// But we use it anyway because it's incredible for bundle size, you just need to develop with it in mind.
// Any properties that start with _ will be mangled, which can be a problem if anything with that pattern is
// part of the public interface, or if any API responses we use matches that regex.
// Fix specific instances of this by adding the property to the reserved list.
properties: {
regex: /^_(?!_)/, // only mangle properties that start with a single _
reserved: [
// list any exceptions that shouldn't be mangled, and please add an explanation:
// referenced in snippet, MUST be preserved
'_i',
'__SV',
// used in decide request, MUST be preserved
'_',
// used in config
'_url',
'_batchKey',
'_noTruncate',
'_onCapture',
// part of setup/teardown code, preserve these out of caution
'_init',
'_dom_loaded',
'_execute_array',
'_handle_unload',
// playwright uses these
'_forceAllowLocalhostNetworkCapture',
'_is_bot',
'__ph_loaded',
'_sessionActivityTimestamp',
'_sessionStartTimestamp',
'_sessionTimeoutMs',
// set on global window object (the ones using __ are not mangled anyway BUT be abundantly cautious)
'_POSTHOG_REMOTE_CONFIG',
'__POSTHOG_INSTRUMENTED__',
'__PosthogExtensions__',
'__posthog_wrapped__',
'__Posthog__',
'_patchFetch',
'_patchXHR',
// part of the public API (none start with _ so are not mangled anyway BUT be abundantly cautious)
'capture',
'identify',
'alias',
'set',
'set_once',
'set_config',
'register',
'register_once',
'unregister',
'opt_out_capturing',
'has_opted_out_capturing',
'opt_in_capturing',
'reset',
'isFeatureEnabled',
'onFeatureFlags',
'getSurveys',
'getActiveMatchingSurveys',
'captureException',
'posthog',
'version',
// possibly used by naughty users - we should decide if we want make these part of the public API, but be cautious for now
'_isIdentified',
'_is_bot',
// URL parameters
'__posthog_debug',
// attribution params, not used in a way that would be mangled but be cautious
'_kx',
// used in rrweb source
'_rrweb',
'_root',
'_css',
'_opts',
'_cssText',
'__context',
'_mappings',
'_processor',
'_args',
'__ln',
'_unchangedStyles',
'__rrweb_original__',
'_Departed',
'_onload',
'_onclick',
'_oncontextmenu',
'_ondblclick',
'_onmousedown',
'_onmouseenter',
'_onmouseleave',
'_onmousemove',
'_onmouseout',
'_onmouseover',
// Helpers added by the e.g. es5 build. We don't use this, but they can be a starting point if we try to get the es5 build mangled in the future
'_invoke',
'__proto__',
'__await',
'_createClass',
'_classCallCheck',
'__esModule',
'__publicField2',
'__symbol__',
// found in terser-mangled-names.json and couldn't attribute source, so preserve out of caution,
'_sb',
'_mirror',
'_map',
],
},
},
}),
{
name: 'save-terser-mangled-names',
writeBundle() {
if (!WRITE_MANGLED_PROPERTIES) {
return
}
const names = Object.keys(nameCache.props.props).map((k) => {
// strip leading dollar to make operating on terser-mangled-names.json easier
if (!k.startsWith('$')) {
throw new Error('Unexpected format')
}
return k.substring(1)
})
names.sort()
// save the props section to a file
fs.writeFileSync(
nameCachePath,
JSON.stringify(
{
'//':
'THIS FILE IS AUTO_GENERATED BY rollup.config.js DO NOT EDIT IT DIRECTLY\n' +
'If a line has been added to this file after a build, it means that the terser mangler has added a new property to the list of mangled properties.\n' +
'CI will fail unless changes to this file are committed.\n' +
'Run a build with `WRITE_MANGLED_PROPERTIES=1 pnpm run build` and commit the new version of this file',
names,
},
null,
4
) + '\n'
)
},
},
]
const entrypoints = fs.readdirSync('./src/entrypoints')
const entrypointTargets = entrypoints.map((file) => {
const fileParts = file.split('.')
// pop the extension
fileParts.pop()
let format = fileParts[fileParts.length - 1]
// NOTE: Sadly we can't just use the file extensions as tsc won't compile things correctly
if (['cjs', 'es', 'iife'].includes(format)) {
fileParts.pop()
} else {
format = 'iife'
}
const fileName = fileParts.join('.')
const pluginsForThisFile = plugins(fileName.includes('es5'))
// we're allowed to console log in this file :)
// eslint-disable-next-line no-console
console.log(`Building ${fileName} in ${format} format`)
/** @type {import('rollup').RollupOptions} */
return {
input: `src/entrypoints/${file}`,
output: [
{
file: `dist/${fileName}.js`,
sourcemap: true,
format,
...(format === 'iife'
? {
name: 'posthog',
globals: {
preact: 'preact',
},
}
: {}),
...(format === 'cjs' ? { exports: 'auto' } : {}),
},
],
plugins: [...pluginsForThisFile, visualizer({ filename: `bundle-stats-${fileName}.html`, gzipSize: true })],
}
})
const typeTargets = entrypoints
.filter((file) => file.endsWith('.es.ts'))
.map((file) => {
const source = `./lib/src/entrypoints/${file.replace('.ts', '.d.ts')}`
/** @type {import('rollup').RollupOptions} */
return {
input: source,
output: [
{
dir: path.resolve('./dist'),
entryFileNames: file.replace('.es.ts', '.d.ts'),
},
],
plugins: [
json(),
dts({
exclude: [],
}),
],
}
})
export default [...entrypointTargets, ...typeTargets]