-
Notifications
You must be signed in to change notification settings - Fork 135
/
rollup.config.js
128 lines (118 loc) · 4.02 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
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'
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',
{
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({
toplevel: true,
compress: {
// 5 is the default if unspecified
ecma: es5 ? 5 : 6,
},
}),
]
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` })],
}
})
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]