This repository has been archived by the owner on Sep 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rollup.js
123 lines (102 loc) · 3.05 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const argv = require('argv');
const ejs = require("ejs");
const fs = require("fs");
const less = require('less');
const rollup = require('rollup');
const resolve = require('rollup-plugin-node-resolve');
const uglify = require('rollup-plugin-uglify');
const minify = require('uglify-es').minify;
// Preamble
const json = JSON.parse(fs.readFileSync("package.json", "utf8"));
const preamble = `// ${json.homepage} Version ${json.version}. Copyright ${(new Date).getFullYear()} ${json.author}.`;
// Commandline option
argv.option({
name: 'debug',
type : 'boolean',
description :'debug build',
example: "'script --debug=true'"
});
const args = argv.run();
const isDebugBuild = args.options.debug;
const distDir = 'dist';
const buildDir = isDebugBuild ? './docs/_build': './docs/app';
// Bundle setting
const appBundles = [
{name: 'dashboardApp', ejs: 'dashboard'},
{name: 'datagridApp', ejs: 'datagrid'},
{name: 'networkApp', ejs: 'network'},
{name: 'profileApp', ejs: 'profile'},
{name: 'tileApp', ejs: 'tile'}
];
const debugBundles = [
{name: 'testAPI', ejs: 'testAPI'},
{name: 'main', source: 'main.js'}
];
const deployBundles = [
{name: 'datagrid', module: 'kw-datagrid', dist: true},
{name: 'network', module: 'kw-network', dist: true},
{name: 'tile', module: 'kw-tile', dist: true}
];
const bundles = appBundles.concat(isDebugBuild ? debugBundles : deployBundles);
// External JS libraries
const external = {
d3: 'd3',
jLouvain: 'jLouvain',
pako: 'pako',
vega: 'vega',
lodash: '_'
};
// JS build
const jsBundled = bundles.map(bundle => {
const plugins = [resolve({jsnext: true})];
if (!isDebugBuild) {
plugins.push(uglify({output: {beautify: false, preamble: preamble}}, minify));
}
const module = bundle.hasOwnProperty('module') ? bundle.module : bundle.name;
return rollup.rollup({
input: bundle.hasOwnProperty('source') ? bundle.source : `src/${bundle.name}.js`,
plugins: plugins,
external: Object.keys(external)
}).then(b => {
b.write({
file: `${bundle.dist ? distDir : buildDir}/${module}.js`,
format: 'umd',
sourcemap: true,
name: module,
banner: preamble,
intro: `const isDebugBuild = ${isDebugBuild}; const kwVersion = '${json.version}';`,
globals: external
});
});
});
// EJS build
const htmlRendered = bundles
.filter(bundle => bundle.hasOwnProperty('ejs'))
.map(bundle => {
return new Promise((resolve, reject) => {
ejs.renderFile(
`./raw/${bundle.ejs}.ejs`, {}, {},
(err, str) => {
if (err) {
console.error(err);
reject();
}
fs.writeFile(`${buildDir}/${bundle.ejs}.html`, str, () => {
resolve();
});
}
);
});
});
// LESS build
const cssRendered = new Promise(resolve => {
fs.readFile('raw/default.less', 'utf8', (err, input) => {
less.render(input).then(output => {
fs.writeFile(`${buildDir}/default.css`, output.css, () => {
resolve();
});
});
});
});
// Run
Promise.all(jsBundled.concat(htmlRendered, cssRendered));