domutil/scripts/build.ts
2023-08-23 19:21:22 +02:00

68 lines
1.8 KiB
TypeScript

import { build, stop } from "https://deno.land/x/esbuild@v0.19.2/mod.js";
import ts from "npm:typescript";
// from https://github.com/Microsoft/TypeScript/issues/6387#issuecomment-169739615
function error(diagnostics: ts.Diagnostic[]): void {
diagnostics.forEach((diagnostic) => {
let message = "Error";
if (diagnostic.file) {
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(
diagnostic.start || 0
);
message += ` ${diagnostic.file.fileName} (${line + 1},${character + 1})`;
}
message +=
": " + ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
console.log(message);
});
}
async function readConfigFile(configFileName: string) {
const configFileText = await Deno.readTextFile(configFileName);
const result = ts.parseConfigFileTextToJson(configFileName, configFileText);
if (result.error) {
error([result.error]);
Deno.exit(1);
}
const config = ts.parseJsonConfigFileContent(
result.config,
ts.sys,
await Deno.realPath(".")
);
if (config.errors.length > 0) {
error(config.errors);
Deno.exit(1);
}
return config;
}
async function buildDTS() {
const config = await readConfigFile("./tsconfig.json");
const emitted = ts
.createProgram(config.fileNames, {
...config.options,
emitDeclarationOnly: true,
})
.emit();
if (emitted.diagnostics.length > 0) {
error(emitted.diagnostics.slice());
Deno.exit(1);
}
}
async function buildESM() {
await build({
bundle: true,
minify: true,
keepNames: true,
target: "es2017", // Required because OBS ships with a really old version of Chrome
entryPoints: ["./domutil.ts"],
outfile: "./dist/domutil.js",
format: "esm",
sourcemap: true,
});
await stop();
}
await Promise.all([buildDTS(), buildESM()]);