-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.mjs
71 lines (63 loc) · 1.77 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
import terser from '@rollup/plugin-terser';
import typescript from '@rollup/plugin-typescript';
import { globby } from 'globby';
import bundleSize from 'rollup-plugin-bundle-size';
import copy from 'rollup-plugin-copy';
import fs from 'fs-extra';
import nodeResolve from '@rollup/plugin-node-resolve';
// scan files to build
const files = (await globby('./src/*.ts', {
ignore: ['**/*.spec.ts', 'example', 'util'],
})).map(path => ({
path,
shortPath: path.replace(/(\/src)|(\.ts)/g, '').replace('./index', '.'),
esm: path.replace('/src/', '/dist/').replace('.ts', '.mjs'),
cjs: path.replace('/src/', '/dist/').replace('.ts', '.cjs'),
types: path.replace('/src/', '/dist/').replace('.ts', '.d.ts'),
})).sort((a, b) => a.shortPath.toLowerCase() < b.shortPath.toLowerCase() ? -1 : 1);
// read original package.json
const pkg = await fs.readJSON('./package.json');
// create updated exports list from build files
pkg.exports = files.reduce((acc, file) => {
acc[file.shortPath] = {
import: file.esm,
require: file.cjs,
types: file.types,
};
return acc;
}, {});
// write updated package.json
await fs.writeJSON('./package.json', pkg, { spaces: 2, EOL: '\r\n' });
// noinspection JSUnusedGlobalSymbols
export default async () => {
console.log(files.map(f => f.path));
return files.map(file => ({
input: file.path,
output: [
{
format: 'esm',
file: file.esm,
sourcemap: false,
},
{
format: 'cjs',
file: file.cjs,
sourcemap: false,
},
],
plugins: [
nodeResolve(),
typescript({ sourceMap: true }),
terser(),
bundleSize(),
copy({
targets: [
{
src: ['LICENSE'],
dest: 'dist',
},
],
}),
],
}));
}