|
3 | 3 | import { getDynamicLibs } from "@yarnpkg/cli";
|
4 | 4 | import { Command, Option } from "clipanion";
|
5 | 5 | import * as fs from "node:fs";
|
| 6 | +import { getRootEnginesField } from "../rootWorkspace.js"; |
6 | 7 |
|
7 | 8 | /**
|
8 | 9 | * @typedef {import("esbuild").BuildOptions} BuildOptions
|
@@ -50,16 +51,35 @@ export class BundleCommand extends Command {
|
50 | 51 | });
|
51 | 52 |
|
52 | 53 | async execute() {
|
53 |
| - await bundle({ |
| 54 | + const { name, outfile } = await bundle({ |
54 | 55 | minify: this.minify,
|
55 | 56 | platform: this.platform,
|
56 | 57 | sourceMap: this.sourceMap,
|
57 | 58 | });
|
| 59 | + |
| 60 | + // report success with file size of the output file |
| 61 | + if (!process.stdin.isTTY && fs.existsSync(outfile)) { |
| 62 | + const sizeKb = Math.round(fs.statSync(outfile).size / 1024); |
| 63 | + this.context.stdout.write(`Success: ${name} bundled: ${sizeKb}kb\n`); |
| 64 | + } |
58 | 65 | }
|
59 | 66 | }
|
60 | 67 |
|
61 | 68 | const defaultTarget = "es2021";
|
62 |
| -const defaultNodeTarget = "node16.17"; |
| 69 | + |
| 70 | +/** |
| 71 | + * @param {Manifest} manifest |
| 72 | + * @returns {string} |
| 73 | + */ |
| 74 | +function getNodeTarget(manifest) { |
| 75 | + const enginesNode = manifest.engines?.node ?? getRootEnginesField().node; |
| 76 | + const match = enginesNode?.match(/(\d+)\.(\d+)/); |
| 77 | + if (!match) { |
| 78 | + throw new Error("Could not get minimum Node version"); |
| 79 | + } |
| 80 | + |
| 81 | + return `node${match[1]}.${match[2]}`; |
| 82 | +} |
63 | 83 |
|
64 | 84 | /**
|
65 | 85 | * @param {Manifest} manifest
|
@@ -173,38 +193,26 @@ function platformOptions(platform, manifest) {
|
173 | 193 | return preset(manifest);
|
174 | 194 | }
|
175 | 195 |
|
176 |
| -/** |
177 |
| - * @param {Manifest} manifest |
178 |
| - * @returns {string} |
179 |
| - */ |
180 |
| -function getNodeTarget(manifest) { |
181 |
| - const enginesNode = manifest.engines?.node; |
182 |
| - const match = enginesNode?.match(/(\d+)\.(\d+)/); |
183 |
| - return match ? `node${match[1]}.${match[2]}` : defaultNodeTarget; |
184 |
| -} |
185 |
| - |
186 | 196 | /**
|
187 | 197 | * @param {Record<string, unknown> | undefined} options
|
| 198 | + * @returns {Promise<{ name: string; outfile: string; }>} |
188 | 199 | */
|
189 | 200 | export async function bundle(options) {
|
190 | 201 | const { minify, platform, sourceMap } = options || {};
|
191 | 202 |
|
192 | 203 | const manifestFile = fs.readFileSync("package.json", { encoding: "utf-8" });
|
193 | 204 | const manifest = JSON.parse(manifestFile);
|
| 205 | + const { name, main: outfile } = manifest; |
194 | 206 |
|
195 | 207 | const esbuild = await import("esbuild");
|
196 | 208 | await esbuild.build({
|
197 | 209 | ...platformOptions(platform, manifest),
|
198 | 210 | bundle: true,
|
199 |
| - outfile: manifest.main, |
| 211 | + outfile, |
200 | 212 | entryPoints: ["src/index.ts"],
|
201 | 213 | minify: Boolean(minify),
|
202 | 214 | sourcemap: Boolean(sourceMap),
|
203 | 215 | });
|
204 | 216 |
|
205 |
| - // report success with file size of the output file |
206 |
| - if (fs.existsSync(manifest.main)) { |
207 |
| - const sizeKb = Math.round(fs.statSync(manifest.main).size / 1024); |
208 |
| - console.log(`Success: ${manifest.name} bundled: ${sizeKb}kb`); |
209 |
| - } |
| 217 | + return { name, outfile }; |
210 | 218 | }
|
0 commit comments