-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollup.config.js
94 lines (88 loc) · 3.43 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
/**
* Copyright 2024 Ceeblue B.V.
* This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
* See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
*/
// For an extensive guide to getting started with the rollup.js JavaScript bundler, visit:
// https://blog.openreplay.com/the-ultimate-guide-to-getting-started-with-the-rollup-js-javascript-bundler
import replace from '@rollup/plugin-replace';
import eslint from '@rollup/plugin-eslint';
import typescript from '@rollup/plugin-typescript';
import terser from '@rollup/plugin-terser';
import { dts } from 'rollup-plugin-dts';
const input = 'index.ts';
const output = 'dist/web-utils';
export default args => {
let target;
let format = args.format;
// Determine the target and format based on provided arguments
if (format) {
if (format.toLowerCase().startsWith('es')) {
target = format;
format = 'es';
} else {
target = 'es5';
}
} else {
format = 'es';
target = 'es6';
}
const downlevelIteration = !(Number(target.substring(2)) > 5);
// Determine the package version by using the 'version' environment variable (for CI/CD processes) or fallback to the version specified in the 'package.json' file.
let version = process.env.version ?? process.env.npm_package_version;
// Validate the version format
if (typeof version === 'string') {
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
const versionRegex =
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
if (!versionRegex.test(version)) {
throw new Error(
'The provided version string does not comply with the Semantic Versioning (SemVer) format required. Please refer to https://semver.org/ for more details on the SemVer specification.'
);
}
console.info('Building version: ' + version);
} else {
throw new Error('Version is undefined or not a string.');
}
return [
{
// Transpile and bundle the code
input,
output: {
name: process.env.npm_package_name,
format, // iife, es, cjs, umd, amd, system
compact: true,
sourcemap: true,
file: output + '.js'
},
plugins: [
replace({
__lib__version__: "'" + version + "'",
preventAssignment: true
}),
eslint(),
typescript({ target, downlevelIteration })
]
},
{
// Minify the bundled code
input: output + '.js',
output: {
compact: true,
sourcemap: true,
file: output + '.min.js'
},
plugins: [terser()],
context: 'window' // Useful for ES5 builds, ensures 'this' refers to 'window' in a browser context
},
{
// Generate type definitions
input,
output: {
compact: true,
file: output + '.d.ts'
},
plugins: [dts()]
}
];
};