1
0
silverbullet/plugins/bundle.ts

74 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-02-24 16:24:49 +00:00
import { parse } from "https://deno.land/std@0.121.0/flags/mod.ts";
import * as path from "https://deno.land/std@0.121.0/path/mod.ts";
import { Manifest, FunctionDef } from "../webapp/src/plugins/types.ts";
2022-02-25 10:27:58 +00:00
async function compile(filePath: string, sourceMaps: boolean): Promise<string> {
2022-02-24 16:24:49 +00:00
// @ts-ignore for Deno.emit (unstable API)
let { files, diagnostics } = await Deno.emit(filePath, {
bundle: "classic",
check: true,
compilerOptions: {
lib: ["WebWorker", "ES2020"],
inlineSourceMap: sourceMaps,
sourceMap: false,
},
});
let bundleSource = files["deno:///bundle.js"];
if (diagnostics.length > 0) {
for (let diagnostic of diagnostics) {
if (diagnostic.start) {
console.error(
`In ${diagnostic.fileName}:${diagnostic.start!.line + 1}: ${
diagnostic.messageText
}`
);
} else {
console.error(diagnostic);
}
}
throw new Error("Diagnostics");
}
2022-02-25 10:27:58 +00:00
return bundleSource;
2022-02-24 16:24:49 +00:00
}
async function bundle(
manifestPath: string,
sourceMaps: boolean
): Promise<Manifest> {
const rootPath = path.dirname(manifestPath);
const manifest = JSON.parse(
new TextDecoder().decode(await Deno.readFile(manifestPath))
) as Manifest;
for (let [name, def] of Object.entries(manifest.functions) as Array<
[string, FunctionDef]
>) {
2022-02-25 10:27:58 +00:00
let jsFunctionName = def.functionName,
2022-02-24 16:24:49 +00:00
filePath = path.join(rootPath, def.path);
2022-02-25 10:27:58 +00:00
if (filePath.indexOf(":") !== -1) {
2022-02-24 16:24:49 +00:00
[filePath, jsFunctionName] = filePath.split(":");
2022-02-25 10:27:58 +00:00
} else if (!jsFunctionName) {
2022-02-24 16:24:49 +00:00
jsFunctionName = "default";
}
2022-02-25 10:27:58 +00:00
def.code = await compile(filePath, sourceMaps);
def.path = filePath;
def.functionName = jsFunctionName;
2022-02-24 16:24:49 +00:00
}
return manifest;
}
let commandLineArguments = parse(Deno.args, {
boolean: true,
});
let [manifestPath, outputPath] = commandLineArguments._ as string[];
console.log(`Generating bundle for ${manifestPath} to ${outputPath}`);
let b = await bundle(manifestPath, !!commandLineArguments.debug);
await Deno.writeFile(
outputPath,
new TextEncoder().encode(JSON.stringify(b, null, 2))
);