-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpugconfig.js
98 lines (72 loc) · 2.71 KB
/
pugconfig.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
/**
* This custom script is used to build/copy website sources for distribution:
* - copy specific assets such as style sheets or scripts (either 3rd party or custom ones)
* - compile specific pug templates and render to dist path
*/
const watch = process.argv.indexOf('--watch') > 1;
const fs = require('fs');
const path = require('path');
const pug = require('pug');
const copy = require('./copy.js');
const websiteDir = './website';
const buildDir = './build';
const demosDir = 'demos';
const demos = require(`./${demosDir}/demos.json`);
const examplesDir = 'examples';
const examples = require(`./${examplesDir}/examples.json`);
const assets = [
[websiteDir, buildDir, ['css/*.css', 'js/*.js', 'img/*.{svg,png,jpg}', 'fonts/*', '*.{svg,png,ico,xml,json}'], [], false],
// ['./source', buildDir, ['data/{colorbrewer,smithwalt}.json'], [], false],
['./node_modules/rxjs/bundles/', `${buildDir}/js`, ['rxjs.umd.min.js', 'rxjs.umd.min.js.map'], [], false],
// ['./source/data/', `${buildDir}/data`, ['*'], [], false],
[`./${demosDir}/data/`, `${buildDir}/${demosDir}/data`, ['*'], [], false],
[`./${examplesDir}/data/`, `${buildDir}/${examplesDir}/data`, ['**'], [], false],
['./dist/', `${buildDir}/js`, ['webgl-operate.js*'], [], false],
];
function render(template, target, object, outputDir = '') {
const src = path.join(websiteDir, template + '.pug');
const dst = path.join(buildDir, outputDir, target + '.html');
if (!fs.existsSync(src)) {
console.log('skipped:', target);
return;
}
const html = pug.renderFile(src, object);
fs.writeFileSync(dst, html);
console.log('emitted:', dst);
}
var build_pending = false;
function build() {
assets.forEach((asset) => copy(asset[0], asset[1], asset[2], asset[3], asset[4]));
render('index', 'index', { 'demos': demos });
render('about', 'about');
demos.forEach((demo) => {
if (demo.disabled) {
return;
}
render(`./${demosDir}/${demo.target}`, demo.target, { 'demo': demo }, demosDir);
});
render('examples', 'index', { 'examples': examples }, examplesDir);
examples.forEach((example) => {
if (example.disabled) {
return;
}
render('example', example.target, { 'example': example }, examplesDir);
});
build_pending = false;
}
build(); // trigger initial build
function watchdir(paths) {
const rebuild = function () {
if (build_pending) {
return;
}
build_pending = true;
setTimeout(build, 100);
}
for (const path of paths) {
fs.watch(path, { recursive: true }, rebuild);
}
}
if (watch) {
watchdir([websiteDir, `./${demosDir}`, `./${examplesDir}`]);
}