-
Notifications
You must be signed in to change notification settings - Fork 8
/
rollup.config.js
69 lines (60 loc) · 1.63 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
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import babel from 'rollup-plugin-babel';
import { terser } from 'rollup-plugin-terser';
import fs from 'fs';
import path from 'path';
const pkg = require(path.resolve(process.cwd(), './package.json'));
const simpleName = pkg.name.replace(/(\W)(\w)(\w*)/gm, '$2$3');
const extensions = ['.ts', '.tsx', '.js', '.jsx'];
const external = [...Object.keys(pkg.peerDependencies || {})];
const getEntryFile = f => {
const isDir = fs.lstatSync(`./src/${f}`).isDirectory();
if (isDir) {
if (fs.existsSync(`./src/${f}/index.ts`)) return `./src/${f}/index.ts`;
if (fs.existsSync(`./src/${f}/index.tsx`)) return `./src/${f}/index.tsx`;
return null;
} return `./src/${f}`;
};
const inputModules = fs.readdirSync('./src').reduce((acc, m) => {
const entryPoint = getEntryFile(m);
if (!entryPoint) return acc;
const fileName = path.parse(m).name;
return {
...acc,
[fileName]: entryPoint
};
}, {});
const plugins = [
commonjs({ include: /node_modules/ }),
// Allows node_modules resolution
resolve({ extensions, mainFields: ['module', 'main'] }),
// Compile TypeScript/JavaScript files
babel({ configFile: '../../babel.config.js', extensions, include: ['src/**/*'] }),
terser()
];
export default [{
input: {
...inputModules
},
external,
plugins,
output: [
{
dir: 'lib',
format: 'esm'
},
{
dir: 'lib/cjs',
format: 'cjs'
}
]
},
{
input: './src/index.ts',
external,
plugins,
output: [
{ file: `./lib/${simpleName}.umd.js`, format: 'umd', name: simpleName }
]
}];