-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
253 lines (247 loc) · 8.51 KB
/
rollup.config.mjs
File metadata and controls
253 lines (247 loc) · 8.51 KB
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
import { defineConfig } from 'rollup';
import nodeResolve from '@rollup/plugin-node-resolve';
import { minify, swc } from 'rollup-plugin-swc3';
import { minifyHTML } from './scripts/minify-html.mjs';
import minifyHtmlLiterals from 'rollup-plugin-minify-html-literals-v3';
import postcss from 'rollup-plugin-postcss';
import postcssPresetEnv from 'postcss-preset-env';
import postcssMixins from 'postcss-mixins';
import postcssLit from 'rollup-plugin-postcss-lit';
import * as path from 'node:path';
import { readFile } from 'node:fs/promises';
import { string } from 'rollup-plugin-string';
import replace from '@rollup/plugin-replace';
import dts from 'rollup-plugin-dts';
import virtual from '@rollup/plugin-virtual';
import json from '@rollup/plugin-json';
const fileName = 'THEOplayerUI';
const umdName = 'THEOplayerUI';
const { browserslist: browserslistModern, version, license } = JSON.parse(await readFile('./package.json', { encoding: 'utf8' }));
const browserslistLegacy = ['last 2 versions', 'ie >= 11'];
const production = process.env.BUILD === 'production';
const banner = `/*!
* THEOplayer Open Video UI for Web (v${version})
* License: ${license}
*/`;
const theoplayerModule = 'theoplayer/chromeless';
const globals = {
[theoplayerModule]: 'THEOplayer'
};
const theoplayerExternals = [theoplayerModule];
const litExternals = [/^lit/, /^@lit/];
/**
* @param {{configOutputDir?: string}} cliArgs
* @return {import("rollup").RollupOptions[]}
*/
export default (cliArgs) => {
const outputDir = cliArgs.configOutputDir || './dist';
return defineConfig([
...jsConfig(outputDir, { es5: false, node: true, production, sourcemap: true }),
...jsConfig(outputDir, { es5: true, production, sourcemap: false }),
{
input: './src/polyfills.ts',
output: {
file: path.join(outputDir, `${fileName}.polyfills.js`),
format: 'iife',
sourcemap: false,
indent: false,
banner
},
context: 'self',
plugins: jsPlugins({ es5: true, module: false, production: true, sourcemap: false })
},
{
input: './src/index.ts',
output: {
file: path.join(outputDir, `${fileName}.d.ts`),
format: 'es',
indent: false,
banner,
footer: `export as namespace ${umdName};`
},
external: [...theoplayerExternals, ...litExternals],
plugins: [dts()]
}
]);
};
/**
* @return {import("rollup").RollupOptions[]}
*/
function jsConfig(outputDir, { es5 = false, node = false, production = false, sourcemap = false }) {
return defineConfig([
{
input: './src/index.ts',
output: {
file: path.join(outputDir, `${fileName}${es5 ? '.es5' : ''}.js`),
format: 'umd',
name: umdName,
sourcemap,
indent: false,
banner,
globals
},
context: 'self',
external: theoplayerExternals,
plugins: jsPlugins({ es5, module: false, production, sourcemap })
},
{
input: './src/index.ts',
output: {
file: path.join(outputDir, `${fileName}${es5 ? '.es5' : ''}.mjs`),
format: 'es',
sourcemap,
indent: false,
banner
},
context: 'self',
external: es5 ? theoplayerExternals : [...theoplayerExternals, ...litExternals],
plugins: jsPlugins({ es5, module: true, production, sourcemap })
},
node && {
input: './src/index.ts',
output: {
file: path.join(outputDir, `${fileName}.node${es5 ? '.es5' : ''}.mjs`),
format: 'es',
sourcemap,
indent: false,
banner
},
context: 'globalThis',
external: litExternals,
plugins: jsPlugins({ es5, node, module: true, production, sourcemap })
}
]).filter(Boolean);
}
/**
* @return {import("rollup").Plugin[]}
*/
function jsPlugins({ es5 = false, node = false, module = false, production = false, sourcemap = false }) {
const browserslist = es5 ? browserslistLegacy : browserslistModern;
const ecmaVersion = es5 ? 5 : 2017;
const minifyHtmlOptions = {
removeComments: production,
removeRedundantAttributes: true,
sortClassName: true,
collapseWhitespace: production
};
return [
nodeResolve({
exportConditions: [
// Use Lit's Node entry point for SSR
node ? 'node' : 'browser',
production ? false : 'development'
].filter(Boolean)
}),
// For Node, stub out THEOplayer.
node &&
virtual({
include: './src/**',
// Remove THEOplayer altogether.
[theoplayerModule]: `export const ChromelessPlayer = undefined;`
}),
json(),
// Run PostCSS on .css files.
postcss({
include: './src/**/*.css',
inject: false,
plugins: [
postcssPresetEnv({
browsers: browserslist,
autoprefixer: { grid: 'no-autoplace' },
enableClientSidePolyfills: false
}),
postcssMixins()
],
minimize: production
}),
postcssLit({
include: './src/**/*.css'
}),
// Minify HTML and CSS.
minifyHTML({
include: ['./src/**/*.html'],
minifyOptions: minifyHtmlOptions
}),
minifyHtmlLiterals({
include: ['./src/**/*.ts'],
options: {
minifyOptions: minifyHtmlOptions
}
}),
// Import HTML and SVG as strings.
string({
include: ['./src/**/*.html', './src/**/*.svg']
}),
es5 &&
replace({
include: ['./node_modules/lit/**', './node_modules/lit-html/**', './node_modules/@lit/**'],
preventAssignment: true,
delimiters: ['', ''],
values: {
// Replace `globalThis` in lit-html.
globalThis: 'self',
// HACK: Fix lit-html for IE11.
// d.createTreeWalker(d, 129 /* NodeFilter.SHOW_{ELEMENT|COMMENT} */)
// ^ This needs additional arguments in IE11.
[`129 /* NodeFilter.SHOW_{ELEMENT|COMMENT} */);`]: `129 /* NodeFilter.SHOW_{ELEMENT|COMMENT} */, null, false);`,
[`129);`]: `129,null,false);`
}
}),
// Transpile TypeScript.
swc({
include: './src/**',
extensions: ['.ts', '.js', '.html', '.css'],
sourceMaps: sourcemap,
tsconfig: false,
env: {
loose: true,
targets: browserslist
},
jsc: {
loose: true,
externalHelpers: true,
parser: {
syntax: 'typescript',
decorators: true
},
transform: {
decoratorVersion: '2022-03'
}
}
}),
// Transpile dependencies for older browsers.
swc({
include: './node_modules/**',
exclude: ['./src/**', './node_modules/@swc/helpers/**'],
sourceMaps: sourcemap,
tsconfig: false,
env: {
loose: false,
targets: browserslist
},
jsc: {
loose: false,
externalHelpers: true,
assumptions: {
constantSuper: true,
noDocumentAll: true,
ignoreFunctionName: true,
ignoreFunctionLength: true,
ignoreToPrimitiveHint: true,
iterableIsArray: false // must NEVER be true!
}
}
}),
// Minify production builds.
production &&
minify({
sourceMap: sourcemap,
mangle: {
toplevel: true
},
toplevel: true,
module,
ecma: ecmaVersion
})
].filter(Boolean);
}