Repository navigation
Expand file tree
/
Copy pathesbuild.config.mjs
More file actions
99 lines (89 loc) 路 3.14 KB
/
Copy pathesbuild.config.mjs
File metadata and controls
99 lines (89 loc) 路 3.14 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
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
import path from "node:path";
import { readFile } from "node:fs/promises";
import { build, transform } from "esbuild";
/**
* @type {import("esbuild").Plugin}
*/
export const minifyCSS = {
name: "minifyCSS",
setup(pluginBuild){
pluginBuild.onLoad({ filter: /\.css$/ }, async(args) => {
const file = await readFile(args.path, "utf8");
const css = await transform(file, { loader: "css", minify: true });
return { loader: "text", contents: css.code };
});
},
};
/**
* @type {import("esbuild").Plugin}
*/
export const textMinifyPlugin = {
name: "textMinifyPlugin",
setup(pluginBuild){
pluginBuild.onLoad({ filter: /\.html$/ }, async(args) => {
let contents = await readFile(args.path, "utf8");
/** @type {string[]} */
const scripts = [];
let index = 0;
// park scripts in placeholders so the whitespace collapse doesn't wreck them
contents = contents.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, (_, scriptContent) => {
scripts.push(scriptContent);
return `___SCRIPT_${index++}___`;
});
for (let i = 0; i < scripts.length; i++){
const transformed = await transform(scripts[i], { loader: "js", minify: true });
scripts[i] = transformed.code;
}
contents = contents.replace(/\s+/g, " ").trim();
for (let i = 0; i < scripts.length; i++){
const minifiedCode = scripts[i];
contents = contents.replace(`___SCRIPT_${i}___`, `<script>${minifiedCode}</script>`);
}
return { loader: "text", contents };
});
},
};
/**
* `import code from "popup-script:./queue.js"` gives the minified source as a string, for code injected elsewhere.
*
* @type {import("esbuild").Plugin}
*/
export const popupScriptPlugin = {
name: "popupScriptPlugin",
setup(pluginBuild){
pluginBuild.onResolve({ filter: /^popup-script:/ }, (args) => ({
path: path.resolve(args.resolveDir, args.path.slice("popup-script:".length)),
namespace: "popup-script",
}));
pluginBuild.onLoad({ filter: /.*/, namespace: "popup-script" }, async(args) => {
const source = await readFile(args.path, "utf8");
const result = await transform(source, { loader: "js", minify: true });
return { loader: "text", contents: result.code, watchFiles: [args.path] };
});
},
};
console.log("Starting esbuild process...");
await build({
entryPoints: ["./src/frontend/main.js"],
bundle: true,
minify: true,
format: "iife",
treeShaking: true,
minifyWhitespace: true,
minifySyntax: true,
ignoreAnnotations: true,
loader: {
".html": "text",
".webp": "dataurl",
".ogg": "base64",
},
outfile: "./target/bundle.js",
plugins: [textMinifyPlugin, minifyCSS, popupScriptPlugin],
})
.then(() => {
console.log("Build completed successfully!");
})
.catch((error) => {
console.error("Build failed:", error);
process.exit(1);
});