-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.ts
120 lines (110 loc) · 2.76 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
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
import {
defineConfig,
type Plugin,
type OutputOptions,
type RollupOptions,
ExternalOption
} from 'rollup';
import typescript from '@rollup/plugin-typescript';
import dts from 'rollup-plugin-dts';
import { readPackageJSON } from 'pkg-types';
import { resolve } from 'node:path';
import nodeResolve from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
const TARGET = process.env.TARGET;
const OUT = process.env.OUT;
const dev = process.env.DEV === 'true';
const pkg = await readPackageJSON(resolve(TARGET));
const input = resolve(TARGET, 'src', 'index.ts');
const outDir = resolve(TARGET, OUT);
const outputs: Record<BuildFormat, OutputOptions> = {
cjs: {
file: `${outDir}/index.cjs`,
format: 'commonjs',
exports: 'named',
sourcemap: dev
},
esm: {
file: `${outDir}/index.mjs`,
format: 'esm',
sourcemap: dev
},
iife: {
file: `${outDir}/index.js`,
format: 'iife',
exports: 'named',
name: pkg.buildOptions?.name ?? 'Dstl',
sourcemap: dev
}
};
const createConfig = (
output: BuildFormat | OutputOptions | BuildFormat[] | OutputOptions[],
plugins: Plugin[] = [],
callback?: (output: OutputOptions) => OutputOptions
): RollupOptions => {
if (Array.isArray(output))
output = output.map(o => (typeof o === 'string' ? outputs[o] : o));
else if (typeof output === 'string') output = [outputs[output]];
else output = [output];
let external: ExternalOption = [];
if (output.some(o => o.format === 'iife'))
output.length > 1 &&
plugins.push({
name: 'iife-warn',
buildStart() {
this.warn(
`The current output format is 'life' and the option 'external' will be ignored`
);
}
});
else if (!dev)
external = Object.keys(pkg.dependencies ?? {}).filter(
dep => !pkg.buildOptions?.internal?.includes(dep)
);
output = output.map(o => {
callback && (o = callback(o));
return {
...o,
banner: `/**
* ${pkg.name} v${pkg.version}
* @license MIT
*/`
};
});
return {
input,
external,
output,
plugins: [
nodeResolve(),
typescript({
tsconfig: './tsconfig.app.json'
}),
...plugins
]
};
};
const options = [createConfig('iife'), createConfig(['cjs', 'esm'])];
if (!dev)
options.push(
createConfig(
{
file: `${outDir}/index.d.ts`
},
[
dts({
tsconfig: './tsconfig.app.json',
respectExternal: true
})
]
),
createConfig(['iife'], [terser()], o => ({
...o,
file: o.file?.replace('.js', '.min.js')
})),
createConfig(['cjs', 'esm'], [terser()], o => ({
...o,
file: o.file?.replace(/\.(cjs|mjs)$/, '.min.$1')
}))
);
export default defineConfig(options);