-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate Deno Bundle to ESbuild (#71)
* Improve bundle to fallback to es build
- Loading branch information
1 parent
949bee3
commit a7c7cd6
Showing
10 changed files
with
415 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,8 +31,8 @@ | |
} | ||
}, | ||
"tasks": { | ||
"test": "deno fmt --check && deno lint && deno test --allow-read --allow-net --allow-write --allow-run src", | ||
"coverage": "rm -rf .coverage && deno test --reporter=dot --allow-read --allow-net --allow-write --allow-run --coverage=.coverage src && deno coverage --exclude=fixtures --exclude=test --lcov --output=lcov.info .coverage && deno run --allow-read https://deno.land/x/[email protected]/cli.ts" | ||
"test": "deno fmt --check && deno lint && deno test --allow-read --allow-net --allow-write --allow-run --allow-env src", | ||
"coverage": "rm -rf .coverage && deno test --reporter=dot --allow-read --allow-net --allow-write --allow-run --allow-env --coverage=.coverage src && deno coverage --exclude=fixtures --exclude=test --lcov --output=lcov.info .coverage && deno run --allow-read https://deno.land/x/[email protected]/cli.ts" | ||
}, | ||
"lock": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { BundleError } from "../errors.ts"; | ||
|
||
type DenoBundleOptions = { | ||
/** The path to the file being bundled */ | ||
entrypoint: string; | ||
/** The path where the bundled file should be written. */ | ||
outFile: string; | ||
}; | ||
|
||
export const DenoBundler = { | ||
bundle: async (options: DenoBundleOptions): Promise<void> => { | ||
// call out to deno to handle bundling | ||
const command = new Deno.Command(Deno.execPath(), { | ||
args: [ | ||
"bundle", | ||
"--quiet", | ||
options.entrypoint, | ||
options.outFile, | ||
], | ||
}); | ||
|
||
const { code, stderr } = await command.output(); | ||
if (code !== 0) { | ||
throw new BundleError({ | ||
cause: new TextDecoder().decode(stderr), | ||
}); | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { assertRejects, assertSpyCall, stub } from "../dev_deps.ts"; | ||
import { BundleError } from "../errors.ts"; | ||
import { DenoBundler } from "./deno_bundler.ts"; | ||
|
||
Deno.test("Deno Bundler tests", async (t) => { | ||
await t.step(DenoBundler.bundle.name, async (tt) => { | ||
const expectedEntrypoint = "./function.ts"; | ||
const expectedOutFile = "./dist/bundle.ts"; | ||
|
||
await tt.step( | ||
"should invoke 'deno bundle' successfully", | ||
async () => { | ||
const commandResp = { | ||
output: () => Promise.resolve({ code: 0 }), | ||
} as Deno.Command; | ||
|
||
// Stub out call to `Deno.Command` and fake return a success | ||
const commandStub = stub( | ||
Deno, | ||
"Command", | ||
() => commandResp, | ||
); | ||
|
||
try { | ||
await DenoBundler.bundle( | ||
{ entrypoint: expectedEntrypoint, outFile: expectedOutFile }, | ||
); | ||
assertSpyCall(commandStub, 0, { | ||
args: [ | ||
Deno.execPath(), | ||
{ | ||
args: [ | ||
"bundle", | ||
"--quiet", | ||
expectedEntrypoint, | ||
expectedOutFile, | ||
], | ||
}, | ||
], | ||
}); | ||
} finally { | ||
commandStub.restore(); | ||
} | ||
}, | ||
); | ||
|
||
await tt.step( | ||
"should throw an exception if the 'deno bundle' command fails", | ||
async () => { | ||
const commandResp = { | ||
output: () => | ||
Promise.resolve({ | ||
code: 1, | ||
stderr: new TextEncoder().encode( | ||
"error: unrecognized subcommand 'bundle'", | ||
), | ||
}), | ||
} as Deno.Command; | ||
|
||
// Stub out call to `Deno.Command` and fake return a success | ||
const commandStub = stub( | ||
Deno, | ||
"Command", | ||
() => commandResp, | ||
); | ||
|
||
try { | ||
await assertRejects( | ||
() => | ||
DenoBundler.bundle( | ||
{ | ||
entrypoint: expectedEntrypoint, | ||
outFile: expectedOutFile, | ||
}, | ||
), | ||
BundleError, | ||
"Error bundling function file", | ||
); | ||
assertSpyCall(commandStub, 0, { | ||
args: [ | ||
Deno.execPath(), | ||
{ | ||
args: [ | ||
"bundle", | ||
"--quiet", | ||
expectedEntrypoint, | ||
expectedOutFile, | ||
], | ||
}, | ||
], | ||
}); | ||
} finally { | ||
commandStub.restore(); | ||
} | ||
}, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { denoPlugins, esbuild } from "../deps.ts"; | ||
|
||
type EsbuildBundleOptions = { | ||
/** The path to the file being bundled */ | ||
entrypoint: string; | ||
/** The path to the deno.json / deno.jsonc config file. */ | ||
configPath: string; | ||
/** specify the working directory to use for the build */ | ||
absWorkingDir: string; | ||
}; | ||
|
||
export const EsbuildBundler = { | ||
bundle: async (options: EsbuildBundleOptions): Promise<Uint8Array> => { | ||
try { | ||
// esbuild configuration options https://esbuild.github.io/api/#overview | ||
const result = await esbuild.build({ | ||
entryPoints: [options.entrypoint], | ||
platform: "neutral", | ||
target: "deno1", // TODO: the versions should come from the user defined input | ||
format: "esm", // esm format stands for "ECMAScript module" | ||
bundle: true, // inline any imported dependencies into the file itself | ||
absWorkingDir: options.absWorkingDir, | ||
write: false, // Favor returning the contents | ||
outdir: "out", // Nothing is being written to file here | ||
plugins: [ | ||
...denoPlugins({ configPath: options.configPath }), | ||
], | ||
}); | ||
return result.outputFiles[0].contents; | ||
} finally { | ||
esbuild.stop(); | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import { assertExists } from "../dev_deps.ts"; | ||
import { EsbuildBundler } from "./esbuild_bundler.ts"; | ||
|
||
Deno.test("Esbuild Bundler tests", async (t) => { | ||
await t.step(EsbuildBundler.bundle.name, async (tt) => { | ||
await tt.step( | ||
"should invoke 'esbuild.build' successfully", | ||
async () => { | ||
const bundle = await EsbuildBundler.bundle( | ||
{ | ||
entrypoint: "src/tests/fixtures/functions/test_function_file.ts", | ||
configPath: `${Deno.cwd()}/deno.jsonc`, | ||
absWorkingDir: Deno.cwd(), | ||
}, | ||
); | ||
assertExists(bundle); | ||
assertEquals(bundle.length, 195); | ||
}, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { EsbuildBundler } from "./esbuild_bundler.ts"; | ||
export { DenoBundler } from "./deno_bundler.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ export type { JSONValue } from "https://deno.land/[email protected]/encoding/jsonc.ts" | |
export { deepMerge } from "https://deno.land/[email protected]/collections/deep_merge.ts"; | ||
export { getProtocolInterface } from "https://deno.land/x/[email protected]/mod.ts"; | ||
export type { Protocol } from "https://deno.land/x/[email protected]/types.ts"; | ||
export * as esbuild from "https://deno.land/x/[email protected]/mod.js"; | ||
export { denoPlugins } from "https://deno.land/x/[email protected]/mod.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class BundleError extends Error { | ||
constructor(options?: ErrorOptions) { | ||
super("Error bundling function file", options); | ||
} | ||
} |
Oops, something went wrong.