-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.js
More file actions
67 lines (54 loc) · 1.46 KB
/
Copy pathbuild.js
File metadata and controls
67 lines (54 loc) · 1.46 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
import { readdir, stat, readFile, writeFile, mkdir, copyFile } from 'fs/promises'
import path from 'path'
import esbuild from 'esbuild'
import { minify as minifyHTML } from 'html-minifier-terser'
const SRC = 'src'
const DIST = 'dist'
const imageExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.webp', '.svg', '.ico']
async function walk(dir) {
const entries = await readdir(dir)
for (const entry of entries) {
const fullPath = path.join(dir, entry)
const info = await stat(fullPath)
if (info.isDirectory()) {
await walk(fullPath)
} else {
await processFile(fullPath)
}
}
}
async function processFile(file) {
const out = path.join(DIST, file.replace(SRC, ''))
await mkdir(path.dirname(out), { recursive: true })
const ext = path.extname(file).toLowerCase()
if (imageExtensions.includes(ext)) {
await copyFile(file, out)
return
}
if (ext === '.js') {
await esbuild.build({
entryPoints: [file],
outfile: out,
minify: true,
bundle: false,
platform: 'browser'
})
return
}
const content = await readFile(file, 'utf8')
if (ext === '.css') {
await writeFile(out, content.replace(/\s+/g, ' '))
return
}
if (ext === '.html') {
const minified = await minifyHTML(content, {
collapseWhitespace: true,
removeComments: true
})
await writeFile(out, minified)
return
}
await writeFile(out, content)
}
await walk(SRC)
console.warn("Compilé !")