This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.js
77 lines (71 loc) · 2.25 KB
/
build.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
70
71
72
73
74
75
76
77
'use strict';
const fs = require('fs');
const del = require('del');
const rollup = require('rollup');
const babel = require('rollup-plugin-babel');
const uglify = require('rollup-plugin-uglify');
const nodeResolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const pkg = require('./package.json');
const Visualizer = require('rollup-plugin-visualizer');
const bundles = [
{
format: 'es', ext: '.js'
},
{
format: 'umd', ext: '.js'
},
{
format: 'umd', ext: '.min.js', minify: true
}
];
let promise = Promise.resolve();
promise = promise.then(() => del(['dist/*']));
for (const config of bundles) {
const plugins = [
nodeResolve(),
commonjs(),
babel({
presets: [
'es2015-rollup',
'react',
'stage-0'
],
plugins: [
['transform-runtime', {polyfill: false, regenerator: true}],
'external-helpers'
],
babelrc: false,
exclude: 'node_modules/**',
runtimeHelpers: true
}),
Visualizer({
filename: './build_stats.html'
})
];
if (config.minify) {
plugins.push(uglify());
}
promise = promise.then(() => rollup.rollup({
input: 'src/index.js',
external: Object.keys(pkg.peerDependencies).concat(Object.keys(pkg.devDependencies)),
plugins: plugins
})).then(bundle => bundle.write({
file: `dist/index.${config.format}${config.ext}`,
format: config.format,
sourcemap: !config.minify,
globals: {
react: 'React'
},
name: pkg.name
}));
}
promise.then(() => {
fs.writeFileSync('dist/package.json', JSON.stringify(pkg, null, ' '), 'utf-8');
fs.writeFileSync('dist/README.md', fs.readFileSync('README.md', 'utf-8'), 'utf-8');
fs.writeFileSync('dist/LICENSE', fs.readFileSync('LICENSE', 'utf-8'), 'utf-8');
fs.mkdirSync('dist/docs');
fs.writeFileSync('dist/docs/api.md', fs.readFileSync('docs/api.md', 'utf-8'), 'utf-8');
fs.writeFileSync('dist/CHANGELOG.md', fs.readFileSync('CHANGELOG.md', 'utf-8'), 'utf-8');
});
promise.catch(err => console.error(err.stack));