-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathbuild.js
More file actions
134 lines (102 loc) · 3.02 KB
/
Copy pathbuild.js
File metadata and controls
134 lines (102 loc) · 3.02 KB
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
124
125
126
127
128
129
130
131
132
133
134
import { deleteAsync } from 'del';
import { glob } from 'glob';
import { promises as fs } from 'fs';
import path from 'path';
import postcss from 'postcss';
import atImport from 'postcss-import';
import autoprefixer from 'autoprefixer';
import svg from 'postcss-inline-svg';
import { minify as cssoMinify } from 'csso';
import { minify as terser } from 'terser';
import { minify as htmlMinify } from 'html-minifier-terser';
// Clean
async function clean() {
await deleteAsync('dist/**');
console.log('Cleaned dist/');
}
// Copy assets
async function copy() {
await fs.cp('src/assets', 'dist', { recursive: true });
console.log('Copied assets');
}
// HTML
async function html() {
const files = await glob([
'src/pages/*.html',
'src/pages/**/index.html'
], { ignore: ['src/pages/**/pres/**'] });
for (const file of files) {
const content = await fs.readFile(file, 'utf-8');
const minified = await htmlMinify(content, {
removeComments: true,
collapseWhitespace: true
});
const relativePath = path.relative('src/pages', file);
const parsed = path.parse(relativePath);
let destPath;
if (parsed.name === 'index') {
destPath = path.join('dist', parsed.dir, 'index.html');
} else {
destPath = path.join('dist', parsed.dir, parsed.name, 'index.html');
}
await fs.mkdir(path.dirname(destPath), { recursive: true });
await fs.writeFile(destPath, minified);
}
console.log(`Processed ${files.length} HTML files`);
}
// Presentations (symlinks)
async function pres() {
const srcDirs = await glob('src/pages/**/pres');
for (const srcDir of srcDirs) {
const destDir = srcDir.replace('src/pages', 'dist');
const destParent = path.dirname(destDir);
await fs.mkdir(destParent, { recursive: true });
const srcAbsolute = path.resolve(srcDir);
try {
await fs.symlink(srcAbsolute, destDir);
} catch (err) {
if (err.code !== 'EEXIST') throw err;
}
}
console.log(`Symlinked ${srcDirs.length} presentation folders`);
}
// Styles
async function styles() {
const content = await fs.readFile('src/styles/screen.css', 'utf-8');
const result = await postcss([
atImport,
autoprefixer,
svg
]).process(content, { from: 'src/styles/screen.css' });
const minified = cssoMinify(result.css).css;
await fs.mkdir('dist/styles', { recursive: true });
await fs.writeFile('dist/styles/screen.css', minified);
console.log('Processed styles');
}
// Scripts
async function scripts() {
const content = await fs.readFile('src/scripts/script.js', 'utf-8');
const result = await terser(content);
await fs.mkdir('dist/scripts', { recursive: true });
await fs.writeFile('dist/scripts/script.js', result.code);
console.log('Processed scripts');
}
// Build
async function build() {
console.log('Building...\n');
const start = performance.now();
await clean();
await copy();
await Promise.all([
html(),
pres(),
styles(),
scripts()
]);
const duration = ((performance.now() - start) / 1000).toFixed(2);
console.log(`\nDone in ${duration}s`);
}
build().catch((err) => {
console.error(err);
process.exit(1);
});