deno/ext/bundle/bundle.ts
Nathan Whitaker c76c3f7c13
Some checks are pending
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / build libs (push) Blocked by required conditions
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions
feat(bundle): runtime API for deno bundle (#29949)
2025-09-04 09:47:27 -07:00

45 lines
1.2 KiB
TypeScript

// Copyright 2018-2025 the Deno authors. MIT license.
/// <reference path="../../cli/tsc/dts/lib.deno.unstable.d.ts" />
import { op_bundle } from "ext:core/ops";
import { primordials } from "ext:core/mod.js";
import { TextDecoder } from "ext:deno_web/08_text_encoding.js";
const { SafeArrayIterator, Uint8Array, ObjectPrototypeIsPrototypeOf } =
primordials;
const decoder = new TextDecoder();
export async function bundle(
options: Deno.bundle.Options,
): Promise<Deno.bundle.Result> {
const result = {
success: false,
...await op_bundle(
options,
),
};
result.success = result.errors.length === 0;
for (
const f of new SafeArrayIterator(
// deno-lint-ignore no-explicit-any
result.outputFiles as any ?? [],
)
) {
// deno-lint-ignore no-explicit-any
const file = f as any;
if (file.contents?.length === 0) {
delete file.contents;
file.text = () => "";
} else {
if (!ObjectPrototypeIsPrototypeOf(Uint8Array, file.contents)) {
file.contents = new Uint8Array(file.contents);
}
file.text = () => decoder.decode(file.contents ?? "");
}
}
if (result.outputFiles?.length === 0) {
delete result.outputFiles;
}
return result;
}