Page MenuHomeSealhub

builder.ts
No OneTemporary

builder.ts

import { build } from "esbuild";
import glob from "tiny-glob";
import { promises as fs } from "fs";
import { embeddable_file_extensions } from "./embeddable-file-extensions.js";
import { load_assets_plugin } from "./esbuild-plugins/load-assets.js";
import { rewrite_asset_imports_plugin } from "./esbuild-plugins/rewrite-asset-imports.js";
import { generateCollections } from "./generate-collections.js";
import { generateComponents } from "./generate-components.js";
import { generateRoutes } from "./generate-routes.js";
import { generateStimulusControllers } from "./generate-stimulus.js";
import { FONTS_CONFIG_PATH, getFonts } from "./get-fonts.js";
import { relative, resolve } from "node:path";
import {
prepareSingleFileReplaceTscAliasPaths,
SingleFileReplacer,
} from "tsc-alias";
export abstract class Builder {
abstract ownsFile(file_path: string): boolean;
abstract getName(): string;
abstract _build(): Promise<void>;
abstract dispose(): Promise<void>;
constructor(public project_dir: string, public style_dirs: string[]) {}
public ongoing_build: Promise<void> | null = null;
public build(notifier?: (message: string) => void) {
if (!this.ongoing_build) {
const build = this._build()
.catch((err) => {
console.error(err);
})
.then(() => {
notifier?.(this.getName());
this.ongoing_build = null;
});
this.ongoing_build = build;
}
return this.ongoing_build;
}
}
export class FontsBuilder extends Builder {
getName(): string {
return "fonts";
}
ownsFile(file_path: string) {
return file_path == FONTS_CONFIG_PATH;
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async dispose(): Promise<void> {}
async _build() {
return getFonts();
}
}
export class BackendTSBuilder extends Builder {
getName(): string {
return "backend-ts";
}
ownsFile(file_path: string) {
return (
(file_path.endsWith(".ts") || file_path.endsWith(".tsx")) &&
!file_path.endsWith("src/back/collections/collections.ts") &&
!file_path.endsWith("src/back/routes/routes.ts") &&
!file_path.endsWith("src/back/routes/urls.ts") &&
!file_path.endsWith("stimulus.ts") &&
!file_path.startsWith("src/front")
);
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async dispose(): Promise<void> {}
async _build(): Promise<void> {
await Promise.all([
generateCollections(),
generateRoutes(),
generateComponents(),
]);
const entryPoints = (
await glob(
`./src/back/**/*.{ts,tsx,${embeddable_file_extensions.join(
","
)}}`
)
).filter((path) => !path.includes(".#"));
const { metafile } = await build({
entryPoints,
sourcemap: true,
bundle: false,
jsxFactory: "TempstreamJSX.createElement",
jsxFragment: "TempstreamJSX.Fragment",
outdir: "./dist/back",
logLevel: "info",
platform: "node",
target: "es2022",
format: "esm",
loader: Object.fromEntries(
embeddable_file_extensions.map((ext) => ["." + ext, "js"])
),
plugins: [
rewrite_asset_imports_plugin(this.project_dir),
load_assets_plugin,
],
metafile: true,
});
await fs.writeFile(
relative(this.project_dir, "dist") + "/" + "back" + ".meta.json",
JSON.stringify(metafile)
);
}
}
export class FrontendTSBuilder extends Builder {
constructor(
public project_dir: string,
public style_dirs: string[],
public controller_dirs: string[]
) {
super(project_dir, style_dirs);
}
getName(): string {
return "frontend-ts";
}
ownsFile(file_path: string) {
return (
file_path.startsWith("src/front") ||
file_path.endsWith(".stimulus.ts")
);
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async dispose(): Promise<void> {}
async _build(): Promise<void> {
await generateStimulusControllers(this.controller_dirs);
const { metafile } = await build({
entryPoints: ["./src/front/index.ts"],
sourcemap: true,
outfile: "./public/dist/bundle.js",
logLevel: "info",
bundle: true,
minify: true,
metafile: true,
treeShaking: true,
});
await fs.writeFile(
relative(this.project_dir, "dist") + "/" + "front" + ".meta.json",
JSON.stringify(metafile)
);
}
}

File Metadata

Mime Type
text/x-java
Expires
Tue, Dec 24, 14:02 (16 h, 56 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557122
Default Alt Text
builder.ts (4 KB)

Event Timeline