-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
rollup.config.mjs
67 lines (60 loc) · 1.68 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
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'
// scan files to build
const files = (await globby('./src/*.ts', {
ignore: ['**/*.spec.ts', 'examples'],
})).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', '.js'),
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.replace('/dist', ''),
require: file.cjs.replace('/dist', ''),
types: file.types.replace('/dist', ''),
}
return acc
}, {})
// write updated package.json
await fs.writeJSON('./package.json', pkg, { spaces: 2 })
export default async () => {
console.log(files.map(f => f.path))
return files.map(file => ({
input: file.path,
output: [
{
format: 'esm',
file: file.esm,
sourcemap: true,
},
{
format: 'cjs',
file: file.cjs,
sourcemap: true,
},
],
plugins: [
typescript({ sourceMap: true }),
terser(),
bundleSize(),
copy({
targets: [
{
src: ['LICENSE'],
dest: 'dist',
},
],
}),
],
}))
}