-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.ts
75 lines (68 loc) · 2.15 KB
/
rollup.config.ts
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
import _eslint from '@rollup/plugin-eslint';
import _typescript from '@rollup/plugin-typescript';
import { OutputOptions, RollupOptions } from 'rollup';
import cleanup from 'rollup-plugin-cleanup';
import dts from 'rollup-plugin-dts';
import esbuild, { Options as EsbuildOptions } from 'rollup-plugin-esbuild';
import outputSize from 'rollup-plugin-output-size';
import pkg from './package.json' with { type: 'json' };
// NOTE: remove once import errors are fixed for their respective packages
const eslint = _eslint as unknown as typeof _eslint.default;
const typescript = _typescript as unknown as typeof _typescript.default;
// skip sourcemap and umd unless production
const PROD = process.env.NODE_ENV !== 'development';
const WATCH = process.env.ROLLUP_WATCH === 'true';
const name = pkg.name.slice(pkg.name.lastIndexOf('/') + 1);
const input = 'src/index.ts';
const inputUmd = 'src/index.umd.ts';
function build(options: EsbuildOptions = {}) {
return esbuild({ target: 'esnext', ...options });
}
function clean() {
return cleanup({
comments: ['some', 'sources', /__PURE__/],
extensions: ['js', 'ts']
});
}
function umd(options: Partial<OutputOptions>): OutputOptions {
return {
name,
format: 'umd',
exports: 'default',
sourcemap: PROD,
...options
};
}
function defineConfig(options: (false | RollupOptions)[]) {
return options.filter((options): options is RollupOptions => !!options);
}
export default defineConfig([
{
input,
output: [
{ file: pkg.main, format: 'cjs', exports: 'named', sourcemap: PROD },
{ file: pkg.module, format: 'esm', exports: 'named', sourcemap: PROD }
],
plugins: [build(), clean(), outputSize()]
},
PROD && {
input: inputUmd,
output: umd({ file: pkg.unpkg.replace(/\.min\.js$/, '.js') }),
plugins: [build(), clean(), outputSize()]
},
PROD && {
input: inputUmd,
output: umd({ file: pkg.unpkg }),
plugins: [build({ minify: true }), clean(), outputSize()]
},
{
input,
output: { file: pkg.types, format: 'esm' },
plugins: [dts(), outputSize()]
},
WATCH && {
input,
watch: { skipWrite: true },
plugins: [eslint(), typescript()]
}
]);