Skip to content

Commit

Permalink
use new esbuild config and add eslint config/tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Cynosphere committed Jan 7, 2025
1 parent 66a28d2 commit bbfc539
Show file tree
Hide file tree
Showing 5 changed files with 2,345 additions and 225 deletions.
121 changes: 17 additions & 104 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -1,113 +1,26 @@
/* eslint-disable no-console */
import * as esbuild from "esbuild";
import copyStaticFiles from "esbuild-copy-static-files";
import fs from "fs";
import fs from "node:fs";
import path from "node:path";
import { watchExt, buildExt } from "@moonlight-mod/esbuild-config";

const prod = process.env.NODE_ENV === "production";
const watch = process.argv.includes("--watch");
const clean = process.argv.includes("--clean");

function makeConfig(ext, name) {
const entryPoints = [];
const fileExts = ["js", "jsx", "ts", "tsx"];
for (const fileExt of fileExts) {
const path = `./src/${ext}/${name}.${fileExt}`;
if (fs.existsSync(path)) entryPoints.push(path);
}

const wpModulesDir = `./src/${ext}/webpackModules`;
if (fs.existsSync(wpModulesDir) && name === "index") {
const wpModules = fs.readdirSync(wpModulesDir);
for (const wpModule of wpModules) {
entryPoints.push(`./src/${ext}/webpackModules/${wpModule}`);
}
}

if (entryPoints.length === 0) return null;

const wpImportPlugin = {
name: "webpackImports",
setup(build) {
build.onResolve({ filter: /^@moonlight-mod\/wp\// }, (args) => {
const wpModule = args.path.replace(/^@moonlight-mod\/wp\//, "");
return {
path: wpModule,
external: true
};
});
}
};

const timeFormatter = new Intl.DateTimeFormat(undefined, {
hour: "numeric",
minute: "numeric",
second: "numeric",
hour12: false
});
const buildLogPlugin = {
name: "buildLog",
setup(build) {
build.onEnd(() => {
console.log(`[${timeFormatter.format(new Date())}] [${ext}/${name}] build finished`);
});
}
};

const styleInput = `./src/${ext}/style.css`;
const styleOutput = `./dist/${ext}/style.css`;

return {
entryPoints,
outdir: `./dist/${ext}`,

format: "iife",
globalName: "module.exports",
platform: "node",

treeShaking: true,
bundle: true,
minify: prod,
sourcemap: "inline",

external: ["electron", "fs", "path", "module", "events", "original-fs"],

plugins: [
copyStaticFiles({
src: `./src/${ext}/manifest.json`,
dest: `./dist/${ext}/manifest.json`
}),
...(fs.existsSync(styleInput)
? [
copyStaticFiles({
src: styleInput,
dest: styleOutput
})
]
: []),
wpImportPlugin,
buildLogPlugin
]
};
}

const exts = fs.readdirSync("./src");

const config = exts
.map((x) => [makeConfig(x, "index"), makeConfig(x, "node"), makeConfig(x, "host")])
.flat()
.filter((c) => c !== null);

if (clean) {
fs.rmSync("./dist", { recursive: true, force: true });
} else if (watch) {
await Promise.all(
config.map(async (c) => {
const ctx = await esbuild.context(c);
await ctx.watch();
})
);
} else {
for (const c of config) {
await esbuild.build(c);
const exts = fs.readdirSync("./src");

for (const ext of exts) {
const cfg = {
src: path.resolve(path.join("src", ext)),
dst: path.resolve(path.join("dist", ext)),
ext
};

if (watch) {
await watchExt(cfg);
} else {
await buildExt(cfg);
}
}
}
20 changes: 20 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import config from "@moonlight-mod/eslint-config";

export default [
...config,
{
rules: {
"no-restricted-imports": [
"error",
{
patterns: [
{
group: ["src/*"],
message: "Use relative import instead"
}
]
}
]
}
}
];
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
},
"devDependencies": {
"@electron/asar": "^3.2.8",
"@moonlight-mod/eslint-config": "github:moonlight-mod/eslint-config",
"@moonlight-mod/esbuild-config": "github:moonlight-mod/esbuild-config",
"esbuild": "^0.19.3",
"esbuild-copy-static-files": "^0.1.0"
"esbuild-copy-static-files": "^0.1.0",
"eslint": "^9.12.0",
"typescript": "^5.3.2"
},
"dependencies": {
"@moonlight-mod/types": "^1.3.0"
Expand Down
Loading

0 comments on commit bbfc539

Please sign in to comment.