-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
75 lines (68 loc) · 2.06 KB
/
Copy pathbuild.js
File metadata and controls
75 lines (68 loc) · 2.06 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
import { resolve } from 'path';
import { readFileSync, writeFileSync, readdirSync, unlinkSync } from 'fs';
import { createHash } from 'crypto';
import { build } from 'vite';
const root = import.meta.dirname;
const docsJsDir = resolve(root, 'docs/public/js');
const builds = [
// CDN (minified)
{
lib: {
entry: resolve(root, 'src/cdn.js'),
fileName: () => 'alpine.forms.min.js',
formats: ['iife'],
name: 'AlpineForms',
},
outDir: 'dist',
minify: 'oxc',
},
// Docs (minified, will be renamed with hash)
{
lib: {
entry: resolve(root, 'src/cdn.js'),
fileName: () => 'alpine.forms.tmp.js',
formats: ['iife'],
name: 'AlpineForms',
},
outDir: 'docs/public/js',
minify: 'oxc',
},
// ESM + CJS
{
lib: {
entry: resolve(root, 'src/module.js'),
fileName: (format) => `alpine.forms.${format === 'es' ? 'esm' : 'cjs'}.js`,
formats: ['es', 'cjs'],
},
outDir: 'dist',
minify: false,
},
];
for (const b of builds) {
await build({
configFile: false,
build: {
lib: b.lib,
outDir: b.outDir,
emptyOutDir: false,
minify: b.minify,
target: 'es2019',
sourcemap: false,
},
});
}
// Add content hash to docs build and write manifest
const tmpFile = resolve(docsJsDir, 'alpine.forms.tmp.js');
const content = readFileSync(tmpFile);
const hash = createHash('md5').update(content).digest('hex').slice(0, 8);
const hashedName = `alpine.forms.${hash}.min.js`;
// Remove old hashed builds
for (const file of readdirSync(docsJsDir)) {
if (/^alpine\.forms\.[a-f0-9]+\.min\.js$/.test(file)) {
unlinkSync(resolve(docsJsDir, file));
}
}
writeFileSync(resolve(docsJsDir, hashedName), content);
unlinkSync(tmpFile);
writeFileSync(resolve(docsJsDir, 'manifest.json'), JSON.stringify({ 'alpine.forms': hashedName }));
console.log(`\ndocs → ${hashedName}`);