-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.mjs
60 lines (46 loc) · 1.29 KB
/
build.mjs
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
import * as esbuild from 'esbuild';
import * as fs from 'fs';
import { globPlugin } from 'esbuild-plugin-glob';
const srcDir = 'src';
const outDir = 'dist';
fs.rm(outDir, { recursive: true, force: true }, (err) =>
console.log('rm error: ', err)
);
await esbuild.build({
entryPoints: [`${srcDir}/**/*.{js,css}`],
outdir: outDir,
bundle: true,
minify: true,
plugins: [globPlugin()],
});
// Copy html files
function getHtmlFilePaths(rootPath) {
const htmlPathList = [];
function findPath(path) {
try {
const paths = fs.readdirSync(path);
paths.forEach((p) => {
if (p.endsWith('.html')) htmlPathList.push(`${path}/${p}`);
// If the path is for file
if (p.includes('.')) return;
// If the path is for directory
findPath(`${path}/${p}`);
});
} catch (error) {
console.log('Error: ', error);
}
}
findPath(rootPath);
return htmlPathList;
}
const srcPathList = getHtmlFilePaths(srcDir);
srcPathList.forEach((sPath) => {
fs.copyFileSync(sPath, sPath.replace(srcDir, outDir));
});
// Copy manifest
fs.copyFileSync('manifest.json', `${outDir}/manifest.json`);
// Copy assets
fs.mkdirSync(`${outDir}/assets`);
fs.readdirSync('assets').forEach((path) => {
fs.copyFileSync(`assets/${path}`, `${outDir}/assets/${path}`);
});