-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.config.js
200 lines (167 loc) · 5.37 KB
/
build.config.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// @ts-check
import { minify } from 'html-minifier-terser';
import {
mkdir,
readdir,
readFile,
rm,
stat,
symlink,
writeFile,
} from 'node:fs/promises';
import { join } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { ok, system } from './scripts/clikit.js';
import ed from './scripts/ed.js';
const srcDir = process.cwd();
const buildDir = join(srcDir, 'build');
/**
* @param {string} uri
* @param {(contents: string) => Promise<void>} fn
*/
async function withLocalAsset(uri, fn) {
for (const dir of [srcDir, buildDir]) {
const base = pathToFileURL(dir + '/');
const u = new URL(uri, base);
if (u.protocol !== 'file:') {
continue;
}
const path = fileURLToPath(u);
if (!path.startsWith(dir)) {
continue;
}
let contents = '';
try {
contents = await readFile(path, { encoding: 'utf-8' });
} catch (err) {
continue;
}
return fn(contents);
}
}
/** @type {import('./scripts/build.js').Manifest} */
const manifest = [
// start out with an empty build directory; just nuke any prior
{
async do() {
if (await ok(stat('build'))) {
for (const ent of await readdir('build')) {
if (!ent.startsWith('.')) {
const file = join('build', ent);
console.log(`rm -rf ${file}`);
await rm(file, { recursive: true, force: true })
}
}
} else {
console.log('mkdir build');
await mkdir('build');
}
},
},
// TODO run rollup via api instead
{
cmd: ['rollup',
'--plugin', '@rollup/plugin-node-resolve',
'--plugin', '@rollup/plugin-commonjs',
'--format', 'esm',
'--sourcemap',
'-i', 'index.js',
'-o', 'build/index.bundle.js',
],
},
{
cmd: ['rollup',
'--plugin', './scripts/rollup-alt.js',
'--plugin', '@rollup/plugin-node-resolve',
'--plugin', '@rollup/plugin-commonjs',
'--plugin', 'rollup-plugin-terser',
'--format', 'esm',
'--sourcemap',
'-i', 'index.js',
'-o', 'build/index.bundle.min.js',
],
},
'CHANGELOG.md',
{
async do() {
// TODO would be nice to use a proper parser for html manipulation some
// day, but I'm having too much fun with ed() to take on a
// dependency just yet...
const [
template,
version,
] = await Promise.all([
readFile('index.html', { encoding: 'utf-8' }),
system('git', 'describe', '--always').then(s => s.trimEnd())
])
// interpolate
let content = template
.replace(/\{DEV\}/g, version);
content = await ed(content, async ({ y, g, x, lines }) => {
// delete importmap
await y(/<script.*type="importmap"/i, /<\/script>/i, async (i, j) => {
while (j < lines.length && !lines[j + 1].trim()) j++;
lines.splice(i, 1 + j - i);
});
// NOTE: this won't properly match a multi-line <link ...> tag
await g(/<link.*? rel="stylesheet".*?>/i, x(/ href=("[^"]+")/i,
([_, hrefAttr], i) => withLocalAsset(unquote(hrefAttr), async contents => {
lines.splice(i, 1,
'<style>',
...contents.split(/\n/),
'</style>',
);
console.log('inlined', unquote(hrefAttr));
})));
});
// NOTE: this won't properly match a multi-line <script ...> tag
/** @param {string} html */
const inlineScripts = html => ed(html, ({ y, x, lines }) =>
y(/<script/i, /<\/script>/i,
x(/<script([^<>]*?( src=("[^"]+"))[^<>]*?)\s*>/i,
([_, attrs, srcMatch, srcAttr], _i, start, end) => withLocalAsset(unquote(srcAttr), async contents => {
lines.splice(start, 1 + end - start,
`<script ${attrs.replace(srcMatch, '').trim()}>`,
...contents.split(/\n/),
'</script>',
);
console.log('inlined', unquote(srcAttr));
}))));
/** @param {string} script */
const withEntry = script => content.replace('./index.js', script);
await Promise.all([
async () => {
const content = await inlineScripts(withEntry('index.bundle.js'));
await writeFile('build/index.dev.html', content, { encoding: 'utf-8' });
console.log('built index.dev.html from index.html + index.bundle.js ');
},
async () => {
const content = await minify(
await inlineScripts(withEntry('index.bundle.min.js')), {
collapseWhitespace: true,
collapseInlineTagWhitespace: true,
collapseBooleanAttributes: true,
minifyCSS: true,
removeComments: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
sortAttributes: true,
sortClassName: true,
});
await writeFile('build/index.min.html', content, { encoding: 'utf-8' });
console.log('built index.min.html from html-minifier-terser( index.html + index.bundle.min.js )');
},
async () => {
await symlink('index.min.html', 'build/index.html');
console.log('symlinked index.html => index.min.html');
},
].map(fn => fn()));
},
},
];
export default manifest;
/** @param {string} s */
function unquote(s) {
// TODO simpler
return JSON.parse(s)
}