This repository has been archived by the owner on Jun 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
95 lines (81 loc) · 2.52 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// @flow
/* eslint-disable import/no-extraneous-dependencies */
import asyncEach from 'async/each';
import asyncParallel from 'async/parallel';
import { copy as fsExtraCopy, remove as fsExtraRemove } from 'fs-extra';
import klaw from 'klaw';
import _endsWith from 'lodash/fp/endsWith';
import _replace from 'lodash/fp/replace';
import { join as pathJoin } from 'path';
import { rollup as rollupBundle } from 'rollup';
import rollupPluginBabel from 'rollup-plugin-babel';
import rollupPluginFlow from 'rollup-plugin-flow';
import rollupPluginNodeResolve from 'rollup-plugin-node-resolve';
const build = (cb) => {
const srcDir = pathJoin(__dirname, 'src');
const libDir = pathJoin(__dirname, 'lib');
const srcWalk = klaw(srcDir);
const srcPaths = [];
srcWalk.on('data', (file) => {
if (!file.stats.isDirectory()) {
srcPaths.push(file.path);
}
});
srcWalk.on('error', (err) => cb(err));
srcWalk.on('end', () => (
fsExtraRemove(libDir, (removeLibDirErr) => {
if (removeLibDirErr) {
return cb(removeLibDirErr);
}
const bundlePlugins = [
rollupPluginNodeResolve(),
rollupPluginFlow({ pretty: true }),
rollupPluginBabel({
babelrc: false,
externalHelpers: true,
plugins: ['external-helpers', 'transform-object-rest-spread'],
}),
];
return asyncEach(srcPaths, (srcPath, done1) => {
const libPath = _replace(srcDir, libDir, srcPath);
if (!_endsWith('.js', srcPath)) {
return fsExtraCopy(srcPath, libPath)
.catch(err => done1(err))
.then(() => done1());
}
const bundleExternal = (moduleId) => (
moduleId !== srcPath
);
const bundleReadOpts = {
external: bundleExternal,
input: srcPath,
plugins: bundlePlugins,
};
const bundleWriteOpts = {
file: libPath,
format: 'es',
sourcemap: true,
};
return asyncParallel([
done2 => (
fsExtraCopy(srcPath, `${libPath}.flow`)
.catch(err => done2(err))
.then(() => done2())
),
done2 => (
rollupBundle(bundleReadOpts)
.catch(err => done2(err))
.then((bundle) => (
bundle.write(bundleWriteOpts)
.catch(err => done2(err))
.then(() => done2())
))
),
], done1);
}, cb);
})
));
};
build((err) => {
if (err) throw err;
});