-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.base.js
341 lines (303 loc) · 8.8 KB
/
rollup.config.base.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
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
/* eslint-disable no-console,jsdoc/no-types */
import { execSync } from 'node:child_process';
import { basename } from 'node:path';
import { writeFileSync } from 'node:fs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import { dts } from 'rollup-plugin-dts';
import { visualizer } from 'rollup-plugin-visualizer';
import commonjs from '@rollup/plugin-commonjs';
import replace from '@rollup/plugin-replace';
import typescript from '@rollup/plugin-typescript';
import terser from '@rollup/plugin-terser';
import copy from 'rollup-plugin-copy';
const commitHash = (() => {
try {
return execSync('git rev-parse HEAD').toString().trim();
} catch (e) {
console.log('Failed to generate commit Hash: ', e);
return 'mock-commit-hash';
}
})();
export class Configuration {
static Format_ = { Es: 'es', Cjs: 'cjs', Iife: 'iife' };
static FileName_ = {
Min: 'index.min.js',
Debug: 'index.debug.js',
Diagnostics: 'index.diagnostics.js',
DiagnosticsStats: 'index.diagnostic-stats.html',
};
/**
*
* @param {{ name: string, input: string, version: string, packageName: string }} deps - required deps
* @param {{ folder?: string, experimental?: boolean, includeDiagnostics?: boolean }} [options] - optional options
*/
constructor(deps, options = {}) {
const { name, packageName, input, version } = deps;
const { folder = '', experimental = false, includeDiagnostics = false } = options;
/**
* package version is injected as global __VERSION property in the bundle
* package version is included in the license banner
*/
this.version_ = version;
/**
* global namespace for iife build
*/
this.name_ = name;
/**
* target input for the build
*/
this.input_ = input;
/**
* package name for license banner
*/
this.packageName_ = packageName;
/**
* optional
* experimental build will have __EXPERIMENTAL property as true in the bundle
*/
this.experimental_ = experimental;
/**
* optional
* when true, diagnostics bundles will be generated
*/
this.includeDiagnostics_ = includeDiagnostics;
/**
* optional
* sub folder in the dist.
*/
this.folder_ = folder;
this.externals_ = [];
this.globals_ = {};
this.bundleSize_ = {
files: [],
};
}
get input() {
return this.input_;
}
get external() {
return (id) => this.externals_.some((name) => id.startsWith(name));
}
get output() {
const licenseBanner = `/**
* @license
* ${this.packageName_} ${this.version_}
* Copyright Brightcove, Inc. <https://www.brightcove.com/>
* Available under Apache License Version 2.0
* <https://github.com/videojs/monorepo/blob/main/LICENSE>
*/`;
return [
/**
* es/index.debug.js
* es/index.diagnostics.js (optional)
* es/index.diagnostics-stats.html (optional)
* es/index.min.js
*/
this.createEsMinBundle_(),
this.createEsDebugBundle_(),
this.includeDiagnostics_ ? this.createEsDiagnosticsBundle_() : null,
/**
* cjs/index.debug.js
* cjs/index.diagnostics.js (optional)
* cjs/index.diagnostics-stats.html (optional)
* cjs/index.min.js
*/
this.createCjsMinBundle_(),
this.createCjsDebugBundle_(),
this.includeDiagnostics_ ? this.createCjsDiagnosticsBundle_() : null,
/**
* iife/index.debug.js
* iife/index.diagnostics.js (optional)
* iife/index.diagnostics-stats.html (optional)
* iife/index.min.js
*/
this.createIifeMinBundle_(),
this.createIifeDebugBundle_(),
this.includeDiagnostics_ ? this.createIifeDiagnosticsBundle_() : null,
]
.filter((output) => output !== null)
.map((output) => {
output.banner = licenseBanner;
return output;
});
}
createEsMinBundle_() {
const { FileName_, Format_ } = Configuration;
return {
file: this.withBasePath_(`${Format_.Es}/${FileName_.Min}`),
format: Configuration.Format_.Es,
exports: 'auto',
plugins: [
terser({
keep_classnames: true,
keep_fnames: true,
}),
],
};
}
createEsDebugBundle_() {
const { FileName_, Format_ } = Configuration;
return {
file: this.withBasePath_(`${Format_.Es}/${FileName_.Debug}`),
format: Configuration.Format_.Es,
exports: 'auto',
sourcemap: 'inline',
};
}
createEsDiagnosticsBundle_() {
const { FileName_, Format_ } = Configuration;
return {
file: this.withBasePath_(`${Format_.Es}/${FileName_.Diagnostics}`),
format: Configuration.Format_.Es,
exports: 'auto',
plugins: [
visualizer({
gzipSize: true,
open: false,
filename: this.withBasePath_(`${Format_.Es}/${FileName_.DiagnosticsStats}`),
}),
],
};
}
createCjsMinBundle_() {
const { FileName_, Format_ } = Configuration;
return {
file: this.withBasePath_(`${Format_.Cjs}/${FileName_.Min}`),
format: Configuration.Format_.Cjs,
exports: 'auto',
plugins: [
terser({
keep_classnames: true,
keep_fnames: true,
}),
],
};
}
createCjsDebugBundle_() {
const { FileName_, Format_ } = Configuration;
return {
file: this.withBasePath_(`${Format_.Cjs}/${FileName_.Debug}`),
format: Configuration.Format_.Cjs,
exports: 'auto',
sourcemap: 'inline',
};
}
createCjsDiagnosticsBundle_() {
const { FileName_, Format_ } = Configuration;
return {
file: this.withBasePath_(`${Format_.Cjs}/${FileName_.Diagnostics}`),
format: Configuration.Format_.Cjs,
exports: 'auto',
plugins: [
visualizer({
gzipSize: true,
open: false,
filename: this.withBasePath_(`${Format_.Cjs}/${FileName_.DiagnosticsStats}`),
}),
],
};
}
createIifeMinBundle_() {
const { FileName_, Format_ } = Configuration;
return {
file: this.withBasePath_(`${Format_.Iife}/${FileName_.Min}`),
format: Configuration.Format_.Iife,
globals: this.globals_,
name: this.name_,
plugins: [
terser({
keep_classnames: true,
keep_fnames: true,
}),
],
};
}
createIifeDebugBundle_() {
const { FileName_, Format_ } = Configuration;
return {
file: this.withBasePath_(`${Format_.Iife}/${FileName_.Debug}`),
format: Configuration.Format_.Iife,
globals: this.globals_,
name: this.name_,
sourcemap: 'inline',
};
}
createIifeDiagnosticsBundle_() {
const { FileName_, Format_ } = Configuration;
return {
file: this.withBasePath_(`${Format_.Iife}/${FileName_.Diagnostics}`),
format: Configuration.Format_.Iife,
globals: this.globals_,
name: this.name_,
plugins: [
visualizer({
gzipSize: true,
open: false,
filename: this.withBasePath_(`${Format_.Iife}/${FileName_.DiagnosticsStats}`),
}),
],
};
}
withBasePath_(filename) {
if (this.folder_) {
return `dist/${this.folder_}/${filename}`;
}
return `dist/${filename}`;
}
get plugins() {
const bundleSize = this.bundleSize_;
const withBasePath = this.withBasePath_.bind(this);
return [
nodeResolve(),
commonjs(),
replace({
preventAssignment: true,
values: {
__COMMIT_HASH: JSON.stringify(commitHash),
__VERSION: JSON.stringify(this.version_),
__EXPERIMENTAL: this.experimental_,
},
}),
typescript(),
// custom plugin to generate bundlesize config
{
name: 'rollup-plugin-bundle-size',
generateBundle(options, bundle) {
const file = basename(options.file);
const size = bundle[file].code.length;
const maxSize = Math.ceil(size / 1024);
bundleSize.files.push({
path: options.file,
maxSize,
compression: 'none',
});
console.log(`Generate bundle ${options.file} → ~${maxSize} Kb.`);
},
closeBundle() {
writeFileSync(withBasePath('bundlesize.json'), JSON.stringify(bundleSize, null, 2));
},
},
// each sub package must place rollup.config.js at it's own root
copy({
targets: [{ src: '../../LICENSE', dest: 'dist' }],
}),
];
}
get rawRollupConfig() {
return {
input: this.input,
output: this.output,
plugins: this.plugins,
external: this.external,
};
}
}
export class DtsConfiguration extends Configuration {
get output() {
return [{ file: this.withBasePath_('types/index.d.ts'), format: 'es' }];
}
get plugins() {
// each sub-package must have its own ts config:
return [dts({ tsconfig: './tsconfig.json' })];
}
}