-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
rollup.config.js
135 lines (127 loc) · 3.52 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
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
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import cleanup from 'rollup-plugin-cleanup';
import dts from 'rollup-plugin-dts';
import typescript from '@rollup/plugin-typescript';
import { terser } from '@chiogen/rollup-plugin-terser';
import replace from '@rollup/plugin-replace';
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}/`));
export default function Config(options) {
const buildFormat = (format) => {
return !options.watch || watchOnly.includes(format);
};
const commonOutput = {
sourcemap: true,
banner,
globals: {
'chart.js': 'Chart',
'chart.js/helpers': 'Chart.helpers',
},
};
const base = {
input: './src/index.ts',
external: (v) => isDependency(v) || isPeerDependency(v),
plugins: [
typescript(),
resolve({
mainFields: ['module', 'main'],
extensions: ['.mjs', '.cjs', '.js', '.jsx', '.json', '.node'],
modulesOnly: true,
}),
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),
},
}),
cleanup({
comments: ['some', 'ts', 'ts3s'],
extensions: ['ts', 'tsx', 'js', 'jsx'],
include: './src/**/*',
}),
],
};
return [
buildFormat('esm') && {
...base,
output: {
...commonOutput,
file: pkg.module,
format: 'esm',
},
},
buildFormat('cjs') && {
...base,
output: {
...commonOutput,
file: pkg.require,
format: 'cjs',
},
external: (v) => (isDependency(v) || isPeerDependency(v)) && ['d3-'].every((di) => !v.includes(di)),
},
(buildFormat('umd') || buildFormat('umd-min')) && {
...base,
input: './src/index.umd.ts',
output: [
buildFormat('umd') && {
...commonOutput,
file: pkg.umd,
format: 'umd',
name: pkg.global,
},
buildFormat('umd-min') && {
...commonOutput,
file: pkg.unpkg,
format: 'umd',
name: pkg.global,
plugins: [terser()],
},
].filter(Boolean),
external: (v) => isPeerDependency(v),
},
buildFormat('types') && {
...base,
output: {
...commonOutput,
file: pkg.types,
format: 'es',
},
plugins: [
dts({
respectExternal: true,
compilerOptions: {
skipLibCheck: true,
skipDefaultLibCheck: true,
},
}),
],
},
].filter(Boolean);
}