Skip to content

Commit e20f774

Browse files
authored
refactor: read min Node version from root workspace (#3618)
1 parent 4e75884 commit e20f774

File tree

4 files changed

+45
-27
lines changed

4 files changed

+45
-27
lines changed

.changeset/twenty-apricots-turn.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

scripts/new-package.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as fs from "node:fs";
55
import * as path from "node:path";
66
import { URL, fileURLToPath } from "node:url";
77
import { parseArgs } from "node:util";
8+
import { getRootEnginesField } from "./src/rootWorkspace.js";
89

910
type Options = {
1011
experimental?: boolean;
@@ -16,15 +17,6 @@ const USAGE_TOKEN_START = "<!-- usage start -->";
1617
const USAGE_TOKEN_END = "<!-- usage end -->";
1718
const WARNING_BANNER_TOKEN = "<!-- experimental-warning -->";
1819

19-
function getRootEnginesField(): Record<string, string> {
20-
const root = fs.readFileSync(new URL("../package.json", import.meta.url));
21-
const manifest = JSON.parse(root.toString());
22-
if (typeof manifest.engines !== "object") {
23-
throw new Error("'engines' field is incorrectly configured");
24-
}
25-
return manifest.engines;
26-
}
27-
2820
function writeTextFile(destination: string, data: unknown): void {
2921
const fd = fs.openSync(destination, "w");
3022
if (typeof data === "string") {

scripts/src/commands/bundle.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { getDynamicLibs } from "@yarnpkg/cli";
44
import { Command, Option } from "clipanion";
55
import * as fs from "node:fs";
6+
import { getRootEnginesField } from "../rootWorkspace.js";
67

78
/**
89
* @typedef {import("esbuild").BuildOptions} BuildOptions
@@ -50,16 +51,35 @@ export class BundleCommand extends Command {
5051
});
5152

5253
async execute() {
53-
await bundle({
54+
const { name, outfile } = await bundle({
5455
minify: this.minify,
5556
platform: this.platform,
5657
sourceMap: this.sourceMap,
5758
});
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+
}
5865
}
5966
}
6067

6168
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+
}
6383

6484
/**
6585
* @param {Manifest} manifest
@@ -173,38 +193,26 @@ function platformOptions(platform, manifest) {
173193
return preset(manifest);
174194
}
175195

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-
186196
/**
187197
* @param {Record<string, unknown> | undefined} options
198+
* @returns {Promise<{ name: string; outfile: string; }>}
188199
*/
189200
export async function bundle(options) {
190201
const { minify, platform, sourceMap } = options || {};
191202

192203
const manifestFile = fs.readFileSync("package.json", { encoding: "utf-8" });
193204
const manifest = JSON.parse(manifestFile);
205+
const { name, main: outfile } = manifest;
194206

195207
const esbuild = await import("esbuild");
196208
await esbuild.build({
197209
...platformOptions(platform, manifest),
198210
bundle: true,
199-
outfile: manifest.main,
211+
outfile,
200212
entryPoints: ["src/index.ts"],
201213
minify: Boolean(minify),
202214
sourcemap: Boolean(sourceMap),
203215
});
204216

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 };
210218
}

scripts/src/rootWorkspace.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @ts-check
2+
import * as fs from "node:fs";
3+
import { URL } from "node:url";
4+
5+
/**
6+
* @returns {Record<string, string>}
7+
*/
8+
export function getRootEnginesField() {
9+
const root = fs.readFileSync(new URL("../../package.json", import.meta.url));
10+
const manifest = JSON.parse(root.toString());
11+
if (typeof manifest.engines !== "object") {
12+
throw new Error("'engines' field is incorrectly configured");
13+
}
14+
15+
return manifest.engines;
16+
}

0 commit comments

Comments
 (0)