47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { Plugin } from "..";
|
|
import * as fs from "fs";
|
|
import * as esbuild from "esbuild";
|
|
|
|
export default class TSCompiler extends Plugin {
|
|
name = "ts-compiler";
|
|
rewriteTriggers = ["ts", "tsx", "jsx"];
|
|
renameTo = "js";
|
|
longLasting = false;
|
|
|
|
minify = false;
|
|
constructor() {
|
|
super();
|
|
if(process.argv.includes("--prod")) {
|
|
this.minify = true;
|
|
}
|
|
}
|
|
async rewriteFile(file: string, filePath: string) {
|
|
|
|
const result = await esbuild.build({
|
|
stdin: {
|
|
contents: file,
|
|
resolveDir: filePath.split("/")?.slice(0,-1).join("/"),
|
|
sourcefile: filePath.split("/").at(-1),
|
|
loader: filePath.split("/").at(-1)?.split(".").at(-1) as "ts"|"tsx"|"jsx"
|
|
},
|
|
jsxFragment: "Fragment",
|
|
jsxFactory: "Nano.h",
|
|
jsxImportSource: "nano-jsx",
|
|
jsx: "transform",
|
|
write: false,
|
|
bundle: true,
|
|
outdir: 'out',
|
|
minify: this.minify,
|
|
});
|
|
|
|
if(result.errors.length != 0) {
|
|
console.log("TS compiler errored.")
|
|
result.errors.forEach(element => {
|
|
console.error(element);
|
|
});
|
|
} else {
|
|
const output = result.outputFiles[0].contents;
|
|
return (new TextDecoder()).decode(output);
|
|
}
|
|
}
|
|
}
|