From 0388b6a2a4c9a591148faee608d726c2c6447576 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 6 Jul 2022 12:17:14 +0200 Subject: [PATCH] Compiler robustness --- packages/plugos/compile.ts | 49 ++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/packages/plugos/compile.ts b/packages/plugos/compile.ts index baa5faa..c23375a 100644 --- a/packages/plugos/compile.ts +++ b/packages/plugos/compile.ts @@ -28,32 +28,35 @@ export async function compile( ); } - // TODO: Figure out how to make source maps work correctly with eval() code - let result = await esbuild.build({ - entryPoints: [path.basename(inFile)], - bundle: true, - format: "iife", - globalName: "mod", - platform: "browser", - sourcemap: false, //debug ? "inline" : false, - minify: !debug, - outfile: outFile, - metafile: true, - external: excludeModules, - absWorkingDir: path.resolve(path.dirname(inFile)), - }); + try { + // TODO: Figure out how to make source maps work correctly with eval() code + let result = await esbuild.build({ + entryPoints: [path.basename(inFile)], + bundle: true, + format: "iife", + globalName: "mod", + platform: "browser", + sourcemap: false, //debug ? "inline" : false, + minify: !debug, + outfile: outFile, + metafile: true, + external: excludeModules, + absWorkingDir: path.resolve(path.dirname(inFile)), + }); - if (meta) { - let text = await esbuild.analyzeMetafile(result.metafile); - console.log("Bundle info for", functionName, text); - } + if (meta) { + let text = await esbuild.analyzeMetafile(result.metafile); + console.log("Bundle info for", functionName, text); + } - let jsCode = (await readFile(outFile)).toString(); - await unlink(outFile); - if (inFile !== filePath) { - await unlink(inFile); + let jsCode = (await readFile(outFile)).toString(); + await unlink(outFile); + return `(() => { ${jsCode} return mod;})()`; + } finally { + if (inFile !== filePath) { + await unlink(inFile); + } } - return `(() => { ${jsCode} return mod;})()`; } export async function compileModule(