Skip to content

Commit

Permalink
use emit to avoid separate node dependency (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
wydengyre authored Nov 29, 2023
1 parent 3a8ccc4 commit e34f54d
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 332 deletions.
316 changes: 173 additions & 143 deletions deno.lock

Large diffs are not rendered by default.

42 changes: 12 additions & 30 deletions deno/build.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as path from "std/path/mod.ts";
import { configPath, configVal } from "./build-config.ts";
import * as esbuild from "esbuild";
import { denoPlugins } from "esbuild_plugin_deno_loader";

const mainPath = configPath("main");
const workerPath = configPath("worker");
const tesseractWasmPath = configPath("tesseractWasm");

const IMPORT_MAP_PATH_REL = "../import_map.json";
const importMapPath = import.meta.resolve(IMPORT_MAP_PATH_REL);

const bundleDistDir = configPath("bundleDistDir");
const mainBundlePath = path.join(bundleDistDir, configVal("mainBundle"));
export const workerBundlePath = path.join(
Expand All @@ -32,29 +36,20 @@ async function main() {

// TODO: consider a timeout using an abort signal
async function denoBundle(inPath: string, outPath: string) {
const cmd = new Deno.Command("deno", {
args: ["bundle", inPath],
cwd: path.fromFileUrl(import.meta.resolve("../")),
});
const { success, code, stdout, stderr } = await cmd.output();
if (!success) {
const err = new TextDecoder().decode(stderr);
throw `bundling file ${inPath} to ${outPath} failed with code ${code}: ${err}`;
}

const outText = new TextDecoder().decode(stdout);
const cleanedImportMeta = cleanImportMeta(outText);

const buildOptions: esbuild.TransformOptions = {
const buildOptions: esbuild.BuildOptions = {
bundle: true,
entryPoints: [inPath],
format: "esm",
minify: true,
outfile: outPath,
plugins: denoPlugins({ importMapURL: importMapPath }),
treeShaking: true,
};
const build = await esbuild.transform(cleanedImportMeta, buildOptions);
const build = await esbuild.build(buildOptions);
esbuild.stop();
if (build.warnings.length > 0) {
throw `Warnings from esbuild: ${build.warnings}`;
}

await Deno.writeTextFile(outPath, build.code);
}

// TODO: consider a timeout using an abort signal
Expand All @@ -76,19 +71,6 @@ async function zip(fromPath: string, inPath: string, outPath: string) {
}
}

// The Deno bundler unnecessarily adds an importMeta.url property that leaks details about the build path
// Sadly, we still need importMeta.url because emscripten uses import.meta.url to construct a URL
// that ultimately doesn't get used, so we need to swap in a dummy value.
function cleanImportMeta(bundledCode: string): string {
const importMetaUrlRegex = /^(const importMeta = {\n\s*url:)(.+)(,)$/m;
const found = importMetaUrlRegex.test(bundledCode);
if (!found) {
throw "Failed to find importMeta.url";
}

return bundledCode.replace(importMetaUrlRegex, '$1"file://"$3');
}

if (import.meta.main) {
await main();
}
1 change: 0 additions & 1 deletion import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"esbuild_plugin_deno_loader": "https://deno.land/x/[email protected]/mod.ts",
"iso6393": "https://esm.sh/[email protected]",
"std/": "https://deno.land/[email protected]/",
"node/": "https://deno.land/[email protected]/node/",
"zod": "https://deno.land/x/[email protected]/mod.ts"
}
}
3 changes: 2 additions & 1 deletion lib/bitmap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (C) 2023 Wyden and Gyre, LLC
// hacked together from https://github.com/wydengyre/bitmap
import { Buffer } from "node/buffer.ts";

import { Buffer } from "node:buffer";

class BitmapEncoder {
buffer: Buffer;
Expand Down
Loading

0 comments on commit e34f54d

Please sign in to comment.