-
Notifications
You must be signed in to change notification settings - Fork 30
/
rollup.config.mjs
123 lines (116 loc) · 2.87 KB
/
rollup.config.mjs
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
import fs from 'fs';
import path from 'path';
import url from 'url';
import typescript from '@rollup/plugin-typescript';
import inject from '@rollup/plugin-inject';
import strip from '@rollup/plugin-strip';
import replace from '@rollup/plugin-replace';
import terser from '@rollup/plugin-terser';
const dirname = url.fileURLToPath(new URL('.', import.meta.url));
const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
const debug = false;
const banner = `
/**
* @license
* ${pkg.name} v${pkg.version}
* Copyright 2024 Mattias Buelens, Diwank Singh Tomer and other contributors.
* This code is released under the MIT license.
* SPDX-License-Identifier: MIT
*/
`.trim();
const keepNames = [
// Class names
'ReadableStream',
'ReadableStreamDefaultController',
'ReadableByteStreamController',
'ReadableStreamBYOBRequest',
'ReadableStreamDefaultReader',
'ReadableStreamBYOBReader',
'WritableStream',
'WritableStreamDefaultWriter',
'WritableStreamDefaultController',
'ByteLengthQueuingStrategy',
'CountQueuingStrategy',
'TransformStream',
'TransformStreamDefaultController',
// Queuing strategy "size" getter
'size'
];
const keepRegex = new RegExp(`^(${keepNames.join('|')})$`);
function build({ target } = {}) {
return [{
input: `src/polyfill.ts`,
output: [
{
file: `dist/polyfill${target === 'es6' ? '' : `.${target}`}.js`,
format: 'iife',
exports: 'none',
banner,
freeze: false
}
],
plugins: plugins({ target })
}, {
input: `src/ponyfill.ts`,
output: [
{
file: `dist/ponyfill${target === 'es6' ? '' : `.${target}`}.js`,
format: 'umd',
exports: 'named',
name: 'WebStreamsPolyfill',
banner,
freeze: false
},
{
file: `dist/ponyfill${target === 'es6' ? '' : `.${target}`}.mjs`,
format: 'es',
banner
}
],
plugins: plugins({ target })
}];
}
function plugins({ target }) {
return [
typescript({
tsconfig: `tsconfig${target === 'es6' ? '' : `-${target}`}.json`,
declaration: false,
declarationMap: false
}),
target === 'es5'
? inject({
include: 'src/**/*.ts',
exclude: 'src/stub/symbol.ts',
modules: {
Symbol: path.resolve(dirname, './src/stub/symbol.ts')
}
})
: undefined,
replace({
include: 'src/**/*.ts',
preventAssignment: true,
values: {
DEBUG: debug
}
}),
!debug
? strip({
include: 'src/**/*.ts',
functions: ['assert']
})
: undefined,
!debug
? terser({
keep_classnames: keepRegex, // needed for WPT
keep_fnames: keepRegex,
mangle: {
toplevel: true
}
})
: undefined
].filter(Boolean);
}
export default [
...build({ target: 'es5' }),
...build({ target: 'es6' })
];