-
Notifications
You must be signed in to change notification settings - Fork 6
/
rollup.js
63 lines (57 loc) · 1.46 KB
/
rollup.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
const babel = require('rollup-plugin-babel');
const postcss = require('rollup-plugin-postcss');
const resolve = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const fs = require('fs');
const path = require('path');
const rollup = require('rollup');
function copyTsDefinitions() {
for (const file of fs.readdirSync('src')) {
if (file.endsWith('.d.ts')) {
fs.copyFileSync(
path.resolve(`src/${file}`),
path.resolve(`dist/${file}`),
);
}
}
}
function createBundle() {
return rollup.rollup({
input: 'src/index.js',
external: ['react'],
plugins: [
babel({
exclude: 'node_modules/**',
presets: ['react', 'stage-0', 'flow'],
}),
commonjs(),
resolve(),
postcss({
plugins: [],
}),
],
});
}
async function buildForFormat(bundle, moduleName, format) {
await bundle.write({
format,
file: `dist/bundle.${format}.js`,
name: moduleName,
exports: 'named',
sourcemap: true,
globals: {react: 'React'},
});
}
module.exports = async function build(moduleName) {
try {
const bundle = await createBundle();
await buildForFormat(bundle, moduleName, 'umd');
await buildForFormat(bundle, moduleName, 'es');
await buildForFormat(bundle, moduleName, 'cjs');
copyTsDefinitions();
console.log(`finished building ${moduleName}`);
} catch (e) {
console.error(e);
process.exit(1);
}
};