Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
Replace Deno.emit() with a call to deno bundle via Deno.run() (#20)
Browse files Browse the repository at this point in the history
* Replace Deno.emit() with a call to deno bundle via Deno.run()

* add a try/catch around the Deno.run() call to account for any unexpected errors

* attempt to use deno executable that is running the process

* use options.log

* adding comment about defaulting of deno executable
  • Loading branch information
selfcontained authored May 19, 2022
1 parent 4de3c01 commit f1852d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 39 deletions.
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true,
"deno.importMap": "import_map.json",
"deno.suggest.imports.hosts": {
"https://deno.land": false
},
Expand Down
62 changes: 24 additions & 38 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,52 +73,38 @@ const createFunctionFile = async (
fnId: string,
fnFilePath: string,
) => {
// Bundle File
let isImportMapPresent = false;
const importMapPath = `${options.workingDirectory}/import_map.json`;
const fnFileRelative = path.join("functions", `${fnId}.js`);
const fnBundledPath = path.join(options.outputDirectory, fnFileRelative);

// We'll default to just using whatever Deno executable is on the path
// Ideally we should be able to rely on Deno.execPath() so we make sure to bundle with the same version of Deno
// that called this script. This is perhaps a bit overly cautious, so we can look to remove the defaulting here in the future.
let denoExecutablePath = "deno";
try {
const { isFile } = await Deno.stat(importMapPath);
isImportMapPresent = isFile;
} catch (_e) {
isImportMapPresent = false;
denoExecutablePath = Deno.execPath();
} catch (e) {
options.log("Error calling Deno.execPath()", e);
}

let result;
try {
result = await Deno.emit(fnFilePath, {
bundle: "module",
check: false,
importMapPath: isImportMapPresent ? importMapPath : undefined,
// call out to deno to handle bundling
const p = Deno.run({
cmd: [
denoExecutablePath,
"bundle",
fnFilePath,
fnBundledPath,
],
});
} catch (e) {
console.log(
"This is likely due to the newest versions of Deno no longer supporting Deno.emit(). Please downgrade your version of Deno to 1.21.3",
);
throw new Error(e);
}

// Write FN File and sourcemap file
const fnFileRelative = path.join("functions", `${fnId}.js`);
const fnBundledPath = path.join(options.outputDirectory, fnFileRelative);
const fnSourcemapPath = path.join(
options.outputDirectory,
"functions",
`${fnId}.js.map`,
);
const status = await p.status();
if (status.code !== 0 || !status.success) {
throw new Error(`Error bundling function file: ${fnId}`);
}

options.log(`wrote function file: ${fnFileRelative}`);
try {
await Deno.writeTextFile(
fnBundledPath,
result.files["deno:///bundle.js"],
);
await Deno.writeTextFile(
fnSourcemapPath,
result.files["deno:///bundle.js.map"],
);
options.log(`wrote function file: ${fnFileRelative}`);
} catch (e) {
options.log(e);
throw new Error(`Error writing bundled function file: ${fnId}`, e);
options.log(`Error bundling function file: ${fnId}`);
throw e;
}
};

0 comments on commit f1852d2

Please sign in to comment.