-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesbuild.config.js
89 lines (81 loc) · 2.2 KB
/
esbuild.config.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
(async () => {
const outputDirectory = "out";
let { build } = require("esbuild");
let fs = require("fs");
let { readFile, writeFile } = require("fs").promises;
let { minify } = require("html-minifier-terser");
let dev = process.env.NODE_ENV === "development";
let copyPlugin = {
name: "copy",
setup(build) {
fs.access(outputDirectory, (error) => {
if (error) {
fs.mkdir(outputDirectory, (directoryError) => {
if (directoryError) {
throw new Error(directoryError.message);
} else {
fs.copyFile(
"src/ui.html",
outputDirectory + "/ui.html",
(copyError) => {
if (copyError) {
throw new Error(copyError.message);
}
}
);
}
});
}
});
},
};
let cleanPlugin = {
name: "clean",
setup(build) {
try {
fs.rmdirSync(outputDirectory, { recursive: true });
} catch (error) {
console.error(error);
}
},
};
try {
let result = await build({
logLevel: "debug",
entryPoints: ["src/code.ts", "src/ui.tsx"],
watch: dev,
sourcemap: dev,
minify: !dev,
bundle: true,
write: false,
outdir: outputDirectory,
plugins: [cleanPlugin, copyPlugin],
});
for (const file of result.outputFiles) {
if (/ui[.]js$/.test(file.path)) {
let html = await readFile("src/ui.html", "utf8");
let minifiedHtml = minify(html, {
collapseWhitespace: true,
keepClosingSlash: false,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
minifyCSS: true,
});
await writeFile(
outputDirectory + "/ui.html",
`${minifiedHtml}<script>${file.text.replace(
"</script>",
"</ + script>"
)}</script>`
);
} else {
await writeFile(file.path, file.text);
}
}
} catch (error) {
console.error(error);
}
})();