forked from js-temporal/temporal-polyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
163 lines (153 loc) · 4.23 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
import commonjs from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import babel from '@rollup/plugin-babel';
import replace from '@rollup/plugin-replace';
import { terser } from 'rollup-plugin-terser';
import { env } from 'process';
import pkg from './package.json';
const isPlaygroundBuild = !!env.TEMPORAL_PLAYGROUND;
const isTest262Build = !!env.TEST262;
const isProduction = env.NODE_ENV === 'production';
const isTranspiledBuild = !!env.TRANSPILE;
const libName = 'temporal';
function withPlugins(
options = {
babelConfig: undefined,
optimize: false,
debugBuild: true
}
) {
const basePlugins = [
replace({ exclude: 'node_modules/**', 'globalThis.__debug__': options.debugBuild, preventAssignment: true }),
commonjs(),
nodeResolve({ preferBuiltins: false })
];
if (options.babelConfig) {
basePlugins.push(babel(options.babelConfig));
}
if (options.optimize) {
basePlugins.push(
terser({
keep_classnames: true,
keep_fnames: true,
ecma: 2015,
compress: {
keep_fargs: true,
keep_classnames: true,
keep_fnames: true
},
mangle: {
keep_classnames: true,
keep_fnames: true
}
})
);
}
return basePlugins;
}
const input = 'tsc-out/index.js';
const external = [
// Some dependencies (e.g. es-abstract) are imported using sub-paths, so the
// regex below will match these imports too
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {})
].map((dep) => new RegExp(dep + '*'));
function outputEntry(file, format) {
return {
name: libName,
file,
format,
exports: 'named',
sourcemap: true
};
}
const es5BundleBabelConfig = {
babelHelpers: 'bundled',
presets: [
[
'@babel/preset-env',
{
targets: '> 0.25%, not dead, ie 11'
}
]
]
};
let builds = [];
if (isTest262Build) {
builds = [
{
input: 'tsc-out/init.js',
output: {
name: libName,
file: 'dist/script.js',
format: 'iife',
sourcemap: true
},
plugins: withPlugins({
debugBuild: false, // Test262 tests don't pass in debug builds
optimize: isProduction,
babelConfig: isTranspiledBuild ? es5BundleBabelConfig : undefined
})
}
];
} else if (isPlaygroundBuild) {
builds = [
{
input: 'tsc-out/init.js',
output: {
name: libName,
file: 'dist/playground.cjs',
format: 'cjs',
exports: 'named',
sourcemap: true
},
plugins: withPlugins({
debugBuild: true
})
}
];
} else {
// Production / production-like builds
// - an ES2020 CJS bundle for "main"
// - an ES2020 ESM bundle for "module"
// Note that all dependencies are marked as external and won't be included in
// these bundles.
const modernBuildDef = {
input,
external,
output: [
// ESM bundle
outputEntry(pkg.module, 'es'),
// CJS bundle.
// Note that because package.json specifies "type":"module", the name of
// this file MUST end in ".cjs" in order to be treated as a CommonJS file.
outputEntry(pkg.main, 'cjs')
],
plugins: withPlugins({
debugBuild: !isProduction,
optimize: isProduction
// Here is where we could insert the JSBI -> native BigInt plugin if we
// could find a way to provide a separate bundle for modern browsers
// that can use native BigInt.
// Maybe use node's exports + a user-defined condition?
// https://nodejs.org/api/packages.html#resolving-user-conditions
})
};
// A legacy build that
// - bundles all our dependencies (big-integer) into this file
// - transpiles down to ES5
const legacyUMDBuildDef = {
input,
// UMD bundle for using in script tags, etc
// Note that some build systems don't like reading UMD files if they end in
// '.cjs', so this entry in package.json should end in a .js file extension.
output: [outputEntry(pkg.browser, 'umd')],
plugins: withPlugins({
debugBuild: !isProduction,
optimize: isProduction,
babelConfig: es5BundleBabelConfig
})
};
builds = [modernBuildDef, legacyUMDBuildDef];
}
export default builds;