forked from snovakovic/js-flock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
86 lines (65 loc) · 2.02 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
/* eslint-disable import/no-extraneous-dependencies */
const Path = require('path');
const babel = require('rollup-plugin-babel');
const commonjs = require('rollup-plugin-commonjs');
const eslint = require('rollup-plugin-eslint');
const Fs = require('node-fs-extra');
const resolve = require('rollup-plugin-node-resolve');
const Rollup = require('rollup');
const uglify = require('rollup-plugin-uglify');
const options = {
src: Path.resolve(__dirname, 'src/'),
dist: Path.resolve(__dirname, 'dist/'),
mandatoryFiles: ['README.md', 'publish.js', '.npmignore', 'package.json']
};
const plugins = [
eslint({}),
resolve({
jsnext: true,
main: true,
browser: true
}),
commonjs({
include: 'src/**'
}),
babel({
exclude: 'node_modules/**',
externalHelpers: true
})
];
// Clean dist folder
Fs.removeSync(options.dist);
// Copy all unmodified source files to dist
Fs.copy(options.src, options.dist);
// Copy all other required files to dist
options.mandatoryFiles.forEach((fileName) => Fs.copySync(fileName, `${options.dist}/${fileName}`));
// Transpile all source code with rollup
const modules = Fs.readdirSync(options.src).filter((file) => file.includes('.js'));
(function bundler(idx) {
if (idx >= modules.length) return;
const fileName = modules[idx];
const moduleName = fileName === 'index.js' ? 'js-flock' : fileName;
const input = `${options.src}/${fileName}`;
const format = 'umd';
const build = Rollup.rollup({ input, plugins })
.then((bundle) =>
bundle.write({
moduleName,
format,
exports: 'default',
dest: `${options.dist}/es5/${fileName}`
}));
const minifiedBuild =
Rollup.rollup({
input,
plugins: [...plugins, uglify({})]
}).then((bundle) => {
bundle.write({
moduleName,
format,
dest: `${options.dist}/es5/${fileName.replace('.js', '.min.js')}`
});
});
// Rollup fails if we don't build one at the time;
Promise.all([build, minifiedBuild]).then(() => bundler(idx + 1));
}(0));