Skip to content

Commit e50a7b8

Browse files
committed
feat(build): integrate esbuild for improved build performance and add CSS processing
Signed-off-by: 7HR4IZ3 <90985774+7HR4IZ3@users.noreply.github.com>
1 parent 1207f64 commit e50a7b8

9 files changed

Lines changed: 696 additions & 22 deletions

File tree

package-lock.json

Lines changed: 515 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@
8080
"cordova-plugin-system": "file:src/plugins/system",
8181
"cordova-plugin-websocket": "file:src/plugins/websocket",
8282
"css-loader": "^7.1.2",
83+
"esbuild": "^0.25.10",
8384
"mini-css-extract-plugin": "^2.9.3",
8485
"path-browserify": "^1.0.1",
86+
"postcss": "^8.4.38",
8587
"postcss-loader": "^8.1.1",
8688
"prettier": "^3.6.2",
8789
"prettier-plugin-java": "^2.7.4",

src/components/quickTools/footer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import settings from "lib/settings";
6-
import items, { ref } from "./items";
6+
import items from "./items";
77

88
/**
99
* Create a row with common buttons

src/pages/sponsors/sponsors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ function SponsorCard({ name, image, website, tier, tagline }) {
207207
function handleLinkClick(e) {
208208
const target = e.target.closest(".sponsor-card");
209209
if (!target) return;
210-
const { website } = target.dataset;
210+
let { website } = target.dataset;
211211
if (!website) return;
212212
if (!website.startsWith("http")) {
213213
website = "http://" + website;

src/sidebarApps/searchInFiles/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ function terminateWorker(initializeNewWorkers = true) {
529529
* @returns {Worker} A new Worker object that runs the code in 'searchInFilesWorker.build.js'.
530530
*/
531531
function getWorker() {
532-
return new Worker(new URL("./worker.js", import.meta.url));
532+
return new Worker("./build/searchInFilesWorker.js");
533533
}
534534

535535
/**

src/utils/keyboardEvent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const keys = {
3737
46: "Delete",
3838
};
3939

40-
const initKeyboardEventType = (function (event) {
40+
let initKeyboardEventType = (function (event) {
4141
try {
4242
event.initKeyboardEvent(
4343
"keyup", // in DOMString typeArg

utils/esbuild.js

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#!/usr/bin/env node
2+
const path = require("node:path");
3+
const fs = require("node:fs/promises");
4+
const babel = require("@babel/core");
5+
const esbuild = require("esbuild");
6+
const sass = require("sass");
7+
const postcss = require("postcss");
8+
const tagLoader = require("html-tag-js/jsx/tag-loader");
9+
10+
const postcssConfig = require("../postcss.config.js");
11+
12+
const args = process.argv.slice(2);
13+
const modeArgIndex = args.indexOf("--mode");
14+
const mode =
15+
modeArgIndex > -1 && args[modeArgIndex + 1]
16+
? args[modeArgIndex + 1]
17+
: "development";
18+
const isProd = mode === "production";
19+
const watch = args.includes("--watch");
20+
21+
const root = path.resolve(__dirname, "..");
22+
const outdir = path.join(root, "www", "build");
23+
const target = ["es5"];
24+
25+
async function ensureCleanOutdir() {
26+
await fs.rm(outdir, { recursive: true, force: true });
27+
await fs.mkdir(outdir, { recursive: true });
28+
}
29+
30+
async function processCssFile(filePath) {
31+
const isSass = /\.(sa|sc)ss$/.test(filePath);
32+
let css;
33+
34+
if (isSass) {
35+
const result = sass.compile(filePath, {
36+
style: isProd ? "compressed" : "expanded",
37+
loadPaths: [
38+
path.dirname(filePath),
39+
path.join(root, "src"),
40+
path.join(root, "node_modules"),
41+
],
42+
});
43+
css = result.css;
44+
} else {
45+
css = await fs.readFile(filePath, "utf8");
46+
}
47+
48+
const postcssPlugins = postcssConfig.plugins || [];
49+
const processed = await postcss(postcssPlugins).process(css, {
50+
from: filePath,
51+
map: false,
52+
});
53+
54+
return processed.css;
55+
}
56+
57+
const babelPlugin = {
58+
name: "babel-transform",
59+
setup(build) {
60+
build.onLoad({ filter: /\.[cm]?js$/ }, async (args) => {
61+
const source = await fs.readFile(args.path, "utf8");
62+
const result = await babel.transformAsync(source, {
63+
filename: args.path,
64+
configFile: path.join(root, ".babelrc"),
65+
babelrc: false,
66+
sourceType: "unambiguous",
67+
caller: {
68+
name: "esbuild",
69+
supportsStaticESM: true,
70+
supportsDynamicImport: true,
71+
},
72+
});
73+
const transformed = result && result.code ? result.code : source;
74+
const contents = tagLoader(transformed);
75+
return { contents, loader: "js" };
76+
});
77+
},
78+
};
79+
80+
const cssPlugin = {
81+
name: "css-modules-as-text",
82+
setup(build) {
83+
build.onLoad({ filter: /\.(sa|sc|c)ss$/ }, async (args) => {
84+
const isModule = /\.m\.(sa|sc|c)ss$/.test(args.path);
85+
const contents = await processCssFile(args.path);
86+
return {
87+
contents,
88+
loader: isModule ? "text" : "css",
89+
};
90+
});
91+
},
92+
};
93+
94+
const nodeFallbackPlugin = {
95+
name: "node-fallbacks",
96+
setup(build) {
97+
const emptyNamespace = "empty-module";
98+
99+
build.onResolve({ filter: /^path$/ }, () => ({
100+
path: require.resolve("path-browserify"),
101+
}));
102+
103+
build.onResolve({ filter: /^crypto$/ }, () => ({
104+
path: "crypto",
105+
namespace: emptyNamespace,
106+
}));
107+
108+
build.onLoad({ filter: /.*/, namespace: emptyNamespace }, () => ({
109+
contents: "export default {};",
110+
loader: "js",
111+
}));
112+
},
113+
};
114+
115+
async function run() {
116+
await ensureCleanOutdir();
117+
118+
const buildOptions = {
119+
absWorkingDir: root,
120+
entryPoints: {
121+
main: "./src/main.js",
122+
console: "./src/lib/console.js",
123+
searchInFilesWorker: "./src/sidebarApps/searchInFiles/worker.js",
124+
},
125+
outdir,
126+
entryNames: "[name]",
127+
chunkNames: "[name].chunk",
128+
assetNames: "[name][ext]",
129+
publicPath: "/build/",
130+
bundle: true,
131+
format: "iife",
132+
platform: "browser",
133+
target,
134+
minify: isProd,
135+
define: {
136+
"process.env.NODE_ENV": JSON.stringify(mode),
137+
},
138+
nodePaths: [path.join(root, "src")],
139+
loader: {
140+
".hbs": "text",
141+
".md": "text",
142+
".png": "file",
143+
".svg": "file",
144+
".jpg": "file",
145+
".jpeg": "file",
146+
".ico": "file",
147+
".ttf": "file",
148+
".woff2": "file",
149+
".webp": "file",
150+
".eot": "file",
151+
".woff": "file",
152+
".webm": "file",
153+
".mp4": "file",
154+
".wav": "file",
155+
},
156+
plugins: [babelPlugin, cssPlugin, nodeFallbackPlugin],
157+
};
158+
159+
if (watch) {
160+
const ctx = await esbuild.context(buildOptions);
161+
await ctx.watch();
162+
console.log("esbuild is watching for changes...");
163+
return;
164+
}
165+
166+
await esbuild.build(buildOptions);
167+
}
168+
169+
run().catch((error) => {
170+
console.error(error);
171+
process.exit(1);
172+
});

utils/scripts/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ RED=''
8080
NC=''
8181

8282
script1="node ./utils/config.js $mode $app"
83-
script2="webpack --progress --mode $webpackmode "
83+
script2="node ./utils/esbuild.js --mode $webpackmode"
8484
# script3="node ./utils/loadStyles.js"
8585

8686
echo "type : $packageType"

utils/scripts/start.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fi
3030
RED=''
3131
NC=''
3232
script1="node ./utils/config.js $mode $app"
33-
script2="webpack --progress --mode $webpackmode "
33+
script2="node ./utils/esbuild.js --mode $webpackmode"
3434
# script3="node ./utils/loadStyles.js"
3535
script4="cordova run $platform $cordovamode"
3636
eval "
@@ -42,4 +42,4 @@ $script2&&
4242
# $script3;
4343
echo \"${RED}$script4${NC}\";
4444
$script4
45-
"
45+
"

0 commit comments

Comments
 (0)