Skip to content

Commit 7aa5782

Browse files
committed
refactor(cli): discover package version at runtime
Instead of relying on esbuild to set the package version of the CLI, we now load it from `VERSION.json`. `scipts/version.sh` and thus `npm version` update `VERSION.json`. We could directly load `package.json`, unfortunately ESbuild is not able to tree-shake JSON files. I opened the following issue: evanw/esbuild#4000 We have to require NodeJS 20.10.0 or above for import attributes.
1 parent 763263d commit 7aa5782

File tree

6 files changed

+15
-9
lines changed

6 files changed

+15
-9
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ This project adheres to [Semantic Versioning][semver].
66
The format of this changelog is [a variant][lib9-versionning] of [Keep a Changelog][keep-changelog].
77
New entries must be placed in a section entitled `Unreleased`.
88

9+
## Unreleased
10+
11+
- BREAKING CHANGES: require Node.js 20.10.0 or above
12+
13+
This allows us to use [import attributes](https://nodejs.org/api/esm.html#import-attributes).
14+
915
## 0.16.0 (2024-11-02)
1016

1117
- BREAKING CHANGES: require Node.js 20.0.0 or above

VERSION.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"0.16.0"

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"url": "https://github.com/bare-ts/bare/issues"
2424
},
2525
"engines": {
26-
"node": ">=20.0.0"
26+
"node": ">=20.10.0"
2727
},
2828
"type": "module",
2929
"bin": {

scripts/build.sh

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ set -eu
33

44
# https://node.green/#ES2022
55
# https://kangax.github.io/compat-table/es2016plus
6-
TARGET='node16.9.0'
7-
VERSION=\""$npm_package_version"\"
6+
TARGET='node20.10.0'
87

98
# build .d.ts
109
tsc --build src/
1110

1211
cp -f dist/index.d.ts dist/index.d.cts
1312

1413
# build ESM
15-
esbuild 'src/**/*.ts' --target=$TARGET --define:VERSION=$VERSION --outdir=dist --log-level=warning
14+
esbuild 'src/**/*.ts' --target=$TARGET --outdir=dist --log-level=warning
1615

1716
# build CommonJS (fallback)
1817
esbuild src/index.ts --bundle --target=$TARGET --platform=node > dist/index.cjs

scripts/version.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ DATE="$(date -u +%Y-%m-%d)"
99
# set version and current date
1010
sed -i "s/^## Unreleased$/## $npm_package_version ($DATE)/" CHANGELOG.md
1111

12-
git add CHANGELOG.md
12+
echo "\"$npm_package_version\"" >| VERSION.json
13+
14+
git add CHANGELOG.md VERSION.json

src/bin/cli.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
import * as fs from "node:fs"
77
import * as process from "node:process"
88
import * as util from "node:util"
9+
import packageVersion from "../../VERSION.json" with { type: "json" }
910
import { CompilerError, Config, transform } from "../index.js"
1011

11-
// WARNING: This constant MUST be defined at build time.
12-
declare const VERSION: string
13-
1412
const HELP_TEXT = `
1513
Usage: bare [options] [schema]
1614
@@ -82,7 +80,7 @@ function main(): void {
8280
if (values.help) {
8381
console.info(HELP_TEXT)
8482
} else if (values.version) {
85-
console.info(VERSION)
83+
console.info(packageVersion)
8684
} else {
8785
if (positionals.length > 1 && positionals[0] === "compile") {
8886
positionals.pop()

0 commit comments

Comments
 (0)