-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathesbuild.common.js
129 lines (116 loc) · 3.81 KB
/
esbuild.common.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
import { join } from 'path'
import { readdir, stat, mkdir, rm } from 'fs/promises'
import { writeFile } from 'fs/promises'
import { relative } from 'path'
const generateShadersJson = async (shaderFiles) => {
const shaders = shaderFiles.sort().map(file => {
const relativePath = relative('shaders', file)
return {
name: relativePath.replace(/\\/g, '/').replace('.frag', ''),
fileUrl: `shaders/${relativePath}`,
visualizerUrl: `/?shader=${relativePath.replace(/\\/g, '/').replace('.frag', '')}`
}
})
await writeFile(
join('dist', 'shaders.json'),
JSON.stringify(shaders, null, 2)
)
}
export async function ensureDistDirectory() {
try{
await rm('dist', {recursive: true})
} catch(e){}
await mkdir('dist', { recursive: true })
}
export async function findFiles(dir, extensions = ['.js', '.css', '.html']) {
let fileList = []
const files = await readdir(dir, { withFileTypes: true })
await Promise.all(
files.map(async (file) => {
const filePath = join(dir, file.name)
const fileStat = await stat(filePath)
if (fileStat.isDirectory()) {
const subDirFiles = await findFiles(filePath, extensions)
fileList = fileList.concat(subDirFiles)
} else if (fileStat.isFile() && extensions.some((ext) => file.name.endsWith(ext))) {
fileList.push(filePath)
}
}),
)
return fileList
}
export function createBuildOptions(isDev = false) {
const sharedOptions = {
format: 'esm',
minify: !isDev,
sourcemap: true,
define: {
CACHE_NAME: '"2025-02-23:03:50"',
'process.env.NODE_ENV': isDev ? '"development"' : '"production"',
'process.env.LIVE_RELOAD': isDev ? 'true' : 'false',
},
loader: {
'.ttf': 'copy',
'.woff': 'file',
'.woff2': 'file',
'.html': 'copy',
'.png': 'copy',
'.svg': 'file',
'.frag': 'copy',
'.ico': 'copy',
'.jpeg': 'copy',
'.jpg': 'copy',
'.png': 'copy',
},
}
return async function getConfigs() {
const baseDir = './src'
const shaderDir = './shaders'
const imgDir = './images'
const jsFiles = await findFiles(baseDir, ['.js'])
const otherFiles = await findFiles(baseDir, ['.css', '.html', '.ttf', '.png', '.svg'])
const shaderFiles = await findFiles(shaderDir, ['.frag'])
const imgFiles = await findFiles(imgDir, ['.png', '.jpg', '.jpeg'])
await generateShadersJson(shaderFiles)
const bundleEntrypoints = [
'index.js',
'analyze.js',
'edit.js',
'list.js',
'service-worker.js',
...jsFiles,
]
const copyEntrypoints = [
'analyze.css',
'analyze.html',
'edit.css',
'edit.html',
'index.css',
'index.html',
'list.html',
'BarGraph.css',
'favicon.ico',
...otherFiles,
...shaderFiles,
...imgFiles,
]
return {
copyOptions: {
...sharedOptions,
entryPoints: copyEntrypoints,
outdir: join(process.cwd(), 'dist'),
outbase: '.',
bundle: false,
format: undefined,
},
bundleOptions: {
...sharedOptions,
entryPoints: [...bundleEntrypoints, ...shaderFiles],
outdir: join(process.cwd(), 'dist'),
outbase: '.',
bundle: true,
treeShaking: true,
}
}
}
}