forked from JosuaKrause/bubblesets-js
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rollup.config.mjs
123 lines (115 loc) · 3.41 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 commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import dts from 'rollup-plugin-dts';
import typescript from '@rollup/plugin-typescript';
import { terser } from 'rollup-plugin-terser';
import replace from '@rollup/plugin-replace';
import babel from '@rollup/plugin-babel';
import fs from 'fs';
const pkg = JSON.parse(fs.readFileSync('./package.json'));
function resolveYear() {
// Extract copyrights from the LICENSE.
const license = fs.readFileSync('./LICENSE', 'utf-8').toString();
const matches = Array.from(license.matchAll(/\(c\) (\d+-\d+)/gm));
if (!matches || matches.length === 0) {
return 2021;
}
return matches[matches.length - 1][1];
}
const year = resolveYear();
const banner = `/**
* ${pkg.name}
* ${pkg.homepage}
*
* Copyright (c) ${year} ${pkg.author.name} <${pkg.author.email}>
*/
`;
/**
* defines which formats (umd, esm, cjs, types) should be built when watching
*/
const watchOnly = ['umd'];
const isDependency = (v) => Object.keys(pkg.dependencies || {}).some((e) => e === v || v.startsWith(e + '/'));
const isPeerDependency = (v) => Object.keys(pkg.peerDependencies || {}).some((e) => e === v || v.startsWith(e + '/'));
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export default function Config(options) {
const buildFormat = (format) => !options.watch || watchOnly.includes(format);
const base = {
input: './src/index.ts',
output: {
sourcemap: true,
banner,
exports: 'named',
globals: {},
},
external: (v) => isDependency(v) || isPeerDependency(v),
plugins: [
typescript(),
resolve(),
commonjs(),
replace({
preventAssignment: true,
values: {
// eslint-disable-next-line no-undef
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) || 'production',
__VERSION__: JSON.stringify(pkg.version),
},
}),
],
};
return [
(buildFormat('esm') || buildFormat('cjs')) && {
...base,
output: [
buildFormat('esm') && {
...base.output,
file: pkg.module,
format: 'esm',
},
buildFormat('cjs') && {
...base.output,
file: pkg.require,
format: 'cjs',
},
].filter(Boolean),
},
((buildFormat('umd') && pkg.umd) || (buildFormat('umd-min') && pkg.unpkg)) && {
...base,
input: fs.existsSync(base.input.replace('.ts', '.umd.ts')) ? base.input.replace('.ts', '.umd.ts') : base.input,
output: [
buildFormat('umd') &&
pkg.umd && {
...base.output,
file: pkg.umd,
format: 'umd',
name: pkg.global,
},
buildFormat('umd-min') &&
pkg.unpkg && {
...base.output,
file: pkg.unpkg,
format: 'umd',
name: pkg.global,
plugins: [terser()],
},
].filter(Boolean),
external: (v) => isPeerDependency(v),
plugins: [...base.plugins, babel({ presets: ['@babel/env'], babelHelpers: 'bundled' })],
},
buildFormat('types') && {
...base,
output: {
...base.output,
file: pkg.types,
format: 'es',
},
plugins: [
dts({
compilerOptions: {
removeComments: false,
},
respectExternal: true,
}),
],
},
].filter(Boolean);
}