From 891cf135d2fa9c0cfb702d9c56cb36a3361d91e0 Mon Sep 17 00:00:00 2001 From: Alberto Ricart Date: Mon, 28 Aug 2023 15:28:02 -0500 Subject: [PATCH] experimental bundler --- bin/bundle.ts | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 bin/bundle.ts diff --git a/bin/bundle.ts b/bin/bundle.ts new file mode 100644 index 00000000..ed1955c0 --- /dev/null +++ b/bin/bundle.ts @@ -0,0 +1,58 @@ +/* + * Copyright 2023 The NATS Authors + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { cli } from "https://deno.land/x/cobra@v0.0.9/mod.ts"; +import { bundle } from "https://deno.land/x/emit@0.26.0/mod.ts"; + +const root = cli({ + use: "bundle javascript/typescript", + run: async (_cmd, _args, flags) => { + const src = flags.value("src"); + const out = flags.value("out"); + const type = flags.value("module") ? "module" : "classic"; + try { + const r = URL.canParse(src) + ? await bundle(new URL(src), { type }) + : await bundle(src, { type }); + await Deno.writeTextFile(out, r.code); + console.log(`wrote ${out}`); + return 0; + } catch (err) { + console.log(`failed to bundle: ${err.message}`); + console.log(err.stack); + return 1; + } + }, +}); + +root.addFlag({ + name: "src", + type: "string", + usage: "input module source path", + required: true, +}); +root.addFlag({ + name: "out", + type: "string", + usage: "output bundle path", + required: true, +}); +root.addFlag({ + name: "module", + type: "boolean", + usage: "output esm module (default)", + default: true, +}); + +Deno.exit(await root.execute(Deno.args));