2023-05-23 18:53:53 +00:00
|
|
|
import { denoPlugins, esbuild, path, YAML } from "./deps.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
import { bundleAssets } from "./asset_bundle/builder.ts";
|
2022-10-13 13:16:18 +00:00
|
|
|
import { Manifest } from "./types.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
import { version } from "../version.ts";
|
|
|
|
|
2023-10-03 13:27:56 +00:00
|
|
|
// const workerRuntimeUrl = new URL("./worker_runtime.ts", import.meta.url);
|
|
|
|
const workerRuntimeUrl =
|
|
|
|
`https://deno.land/x/silverbullet@${version}/plugos/worker_runtime.ts`;
|
2022-10-10 12:50:21 +00:00
|
|
|
|
|
|
|
export type CompileOptions = {
|
|
|
|
debug?: boolean;
|
2023-05-23 18:53:53 +00:00
|
|
|
runtimeUrl?: string;
|
|
|
|
importMap?: string;
|
2022-10-13 13:16:18 +00:00
|
|
|
// Reload plug import cache
|
|
|
|
reload?: boolean;
|
|
|
|
// Print info on bundle size
|
|
|
|
info?: boolean;
|
2022-10-10 12:50:21 +00:00
|
|
|
};
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
export async function compileManifest(
|
|
|
|
manifestPath: string,
|
|
|
|
destPath: string,
|
|
|
|
options: CompileOptions = {},
|
|
|
|
): Promise<string> {
|
|
|
|
const rootPath = path.dirname(manifestPath);
|
|
|
|
const manifest = YAML.parse(
|
|
|
|
await Deno.readTextFile(manifestPath),
|
|
|
|
) as Manifest<any>;
|
|
|
|
|
|
|
|
if (!manifest.name) {
|
|
|
|
throw new Error(`Missing 'name' in ${manifestPath}`);
|
2022-10-13 13:16:18 +00:00
|
|
|
}
|
2023-05-23 18:53:53 +00:00
|
|
|
|
|
|
|
// Assets
|
|
|
|
const assetsBundle = await bundleAssets(
|
|
|
|
path.resolve(rootPath),
|
|
|
|
manifest.assets as string[] || [],
|
|
|
|
);
|
|
|
|
manifest.assets = assetsBundle.toJSON();
|
|
|
|
|
|
|
|
const jsFile = `
|
|
|
|
import { setupMessageListener } from "${
|
|
|
|
options.runtimeUrl || workerRuntimeUrl
|
|
|
|
}";
|
|
|
|
|
|
|
|
// Imports
|
|
|
|
${
|
|
|
|
Object.entries(manifest.functions).map(([funcName, def]) => {
|
|
|
|
if (!def.path) {
|
|
|
|
return "";
|
2022-10-13 13:16:18 +00:00
|
|
|
}
|
2023-05-23 18:53:53 +00:00
|
|
|
let [filePath, jsFunctionName] = def.path.split(":");
|
|
|
|
// Resolve path
|
|
|
|
filePath = path.join(rootPath, filePath);
|
2022-10-13 13:16:18 +00:00
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
return `import {${jsFunctionName} as ${funcName}} from "file://${
|
2023-01-01 19:28:25 +00:00
|
|
|
// Replacaing \ with / for Windows
|
2023-01-22 17:53:14 +00:00
|
|
|
path.resolve(filePath).replaceAll(
|
|
|
|
"\\",
|
|
|
|
"\\\\",
|
2023-05-23 18:53:53 +00:00
|
|
|
)}";\n`;
|
|
|
|
}).join("")
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
// Function mapping
|
|
|
|
export const functionMapping = {
|
|
|
|
${
|
|
|
|
Object.entries(manifest.functions).map(([funcName, def]) => {
|
|
|
|
if (!def.path) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return ` ${funcName}: ${funcName},\n`;
|
|
|
|
}).join("")
|
|
|
|
}
|
|
|
|
};
|
2022-10-10 12:50:21 +00:00
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
const manifest = ${JSON.stringify(manifest, null, 2)};
|
|
|
|
|
|
|
|
setupMessageListener(functionMapping, manifest);
|
|
|
|
`;
|
|
|
|
|
|
|
|
// console.log("Code:", jsFile);
|
|
|
|
|
|
|
|
const inFile = await Deno.makeTempFile({ suffix: ".js" });
|
|
|
|
const outFile = `${destPath}/${manifest.name}.plug.js`;
|
|
|
|
await Deno.writeTextFile(inFile, jsFile);
|
|
|
|
|
|
|
|
const result = await esbuild.build({
|
|
|
|
entryPoints: [path.basename(inFile)],
|
|
|
|
bundle: true,
|
|
|
|
format: "iife",
|
|
|
|
globalName: "mod",
|
|
|
|
platform: "browser",
|
|
|
|
sourcemap: options.debug ? "linked" : false,
|
|
|
|
minify: !options.debug,
|
|
|
|
outfile: outFile,
|
|
|
|
metafile: options.info,
|
|
|
|
treeShaking: true,
|
|
|
|
plugins: [
|
|
|
|
{
|
|
|
|
name: "json",
|
|
|
|
setup: (build) =>
|
|
|
|
build.onLoad({ filter: /\.json$/ }, () => ({ loader: "json" })),
|
|
|
|
},
|
|
|
|
...denoPlugins({
|
|
|
|
// TODO do this differently
|
|
|
|
importMapURL: options.importMap ||
|
|
|
|
new URL("../import_map.json", import.meta.url).toString(),
|
|
|
|
loader: "native",
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
absWorkingDir: path.resolve(path.dirname(inFile)),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (options.info) {
|
|
|
|
const text = await esbuild.analyzeMetafile(result.metafile!);
|
|
|
|
console.log("Bundle info for", manifestPath, text);
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
let jsCode = await Deno.readTextFile(outFile);
|
|
|
|
jsCode = patchDenoLibJS(jsCode);
|
|
|
|
await Deno.writeTextFile(outFile, jsCode);
|
|
|
|
console.log(`Plug ${manifest.name} written to ${outFile}.`);
|
|
|
|
return outFile;
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
export async function compileManifests(
|
|
|
|
manifestFiles: string[],
|
|
|
|
dist: string,
|
|
|
|
watch: boolean,
|
2022-10-10 12:50:21 +00:00
|
|
|
options: CompileOptions = {},
|
2023-05-23 18:53:53 +00:00
|
|
|
) {
|
|
|
|
let building = false;
|
|
|
|
dist = path.resolve(dist);
|
|
|
|
async function buildAll() {
|
|
|
|
if (building) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log("Building", manifestFiles);
|
|
|
|
building = true;
|
|
|
|
Deno.mkdirSync(dist, { recursive: true });
|
|
|
|
const startTime = Date.now();
|
|
|
|
// Build all plugs in parallel
|
|
|
|
await Promise.all(manifestFiles.map(async (plugManifestPath) => {
|
|
|
|
const manifestPath = plugManifestPath as string;
|
|
|
|
try {
|
|
|
|
await compileManifest(
|
|
|
|
manifestPath,
|
|
|
|
dist,
|
|
|
|
options,
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Error building ${manifestPath}:`, e);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
console.log(`Done building plugs in ${Date.now() - startTime}ms`);
|
|
|
|
building = false;
|
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
await buildAll();
|
|
|
|
|
|
|
|
if (watch) {
|
|
|
|
console.log("Watching for changes...");
|
|
|
|
const watcher = Deno.watchFs(manifestFiles.map((p) => path.dirname(p)));
|
|
|
|
for await (const event of watcher) {
|
|
|
|
if (event.paths.length > 0) {
|
2023-08-15 05:55:36 +00:00
|
|
|
if (
|
|
|
|
event.paths[0].endsWith(".json") || event.paths[0].startsWith(dist)
|
|
|
|
) {
|
2023-05-23 18:53:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log("Change detected, rebuilding...");
|
2023-10-03 12:16:33 +00:00
|
|
|
await buildAll();
|
2023-05-23 18:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
export function patchDenoLibJS(code: string): string {
|
|
|
|
// The Deno std lib has one occurence of a regex that Webkit JS doesn't (yet parse), we'll strip it because it's likely never invoked anyway, YOLO
|
|
|
|
return code.replaceAll("/(?<=\\n)/", "/()/");
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|