-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
68 lines (57 loc) · 1.66 KB
/
build.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
import { build } from 'vite'
import * as glob from 'glob'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { readFile, writeFile } from 'node:fs/promises'
import * as cheerio from 'cheerio'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
async function makeBuild() {
const publicSrcDir = resolve(__dirname, 'src', 'public')
const apps = await glob.glob('**/index.html', {
cwd: publicSrcDir,
})
// regenerate public index.html
const indexHtml = await readFile(resolve(publicSrcDir, 'index.html'), 'utf-8')
const $ = cheerio.load(indexHtml)
$('ul').html(
apps
.filter((a) => a !== 'index.html')
.map((a) => a.replace(/\/index\.html$/, ''))
.sort((a, b) => a.localeCompare(b))
.map((a) => `<li><a href="/${a}/">${a}</a></li>`)
.join('\n'),
)
await writeFile(resolve(publicSrcDir, 'index.html'), $.html())
// build complete repo as one app
if (!process.argv.includes('--skip-main')) {
await build({
root: publicSrcDir,
plugins: [],
build: {
assetsDir: '__assets',
rollupOptions: {
input: apps.reduce((entries, entry, i) => {
entries[i] = resolve(publicSrcDir, entry)
return entries
}, {}),
},
outDir: resolve(__dirname, 'public'),
},
})
}
// build each sketch individually
if (process.argv.includes('-a') || process.argv.includes('--all')) {
for (const app of apps) {
const appDir = dirname(app)
await build({
root: resolve(publicSrcDir, appDir),
plugins: [],
build: { outDir: resolve(__dirname, 'dist', appDir) },
})
}
}
}
makeBuild()
.then(() => console.log('done'))
.catch(console.error)