From aac00d5bc8a9b7ca7a1f55ccc011caaa8a4b44a1 Mon Sep 17 00:00:00 2001 From: mrmlnc Date: Sat, 14 Dec 2024 01:18:49 +0300 Subject: [PATCH 1/4] build: bundle dts files --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index dc0a2f84..940c9f4d 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "@types/picomatch": "^3.0.1", "@types/sinon": "^17.0.3", "bencho": "^0.1.1", + "dts-bundle-generator": "^9.5.1", "esbuild": "^0.24.0", "eslint": "9.14.0", "eslint-config-mrmlnc": "^5.0.0", @@ -68,7 +69,7 @@ "_build:compile": "npm run clean && npm run compile", "build": "npm run _build:compile && npm run lint && npm test", "watch": "npm run _build:compile -- -- --sourceMap --watch", - "_bundle:dts": "tsc --emitDeclarationOnly --outDir ./build", + "_bundle:dts": "dts-bundle-generator ./src/index.ts --out-file=./build/index.d.ts --export-referenced-types=false --external-inlines=@nodelib/fs.walk @nodelib/fs.scandir @nodelib/fs.stat", "_bundle:ts": "esbuild --bundle ./src/index.ts --outfile=./build/index.js --platform=node --target=node18.18 --format=cjs", "_bundle:build": "npm run _bundle:dts && npm run _bundle:ts", "_bundle:test:replace": "cp ./build/index.js ./out", From cb5aa7d99dd0dc400c7d2b9616647f6e41865462 Mon Sep 17 00:00:00 2001 From: mrmlnc Date: Sat, 14 Dec 2024 01:19:40 +0300 Subject: [PATCH 2/4] build: explicitly include build files into package --- package.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 940c9f4d..e0b38610 100644 --- a/package.json +++ b/package.json @@ -14,10 +14,8 @@ "main": "build/index.js", "typings": "build/index.d.ts", "files": [ - "build", - "!build/{benchmark,tests}", - "!build/**/*.map", - "!build/**/*.spec.*" + "build/index.js", + "build/index.d.ts" ], "keywords": [ "glob", From 4083f47856757ae2fefb198b78ed3803bbf4c4b0 Mon Sep 17 00:00:00 2001 From: mrmlnc Date: Fri, 3 Jan 2025 17:00:20 +0300 Subject: [PATCH 3/4] build: bundle JS files instead of transpile & bundle --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index e0b38610..68875369 100644 --- a/package.json +++ b/package.json @@ -68,8 +68,8 @@ "build": "npm run _build:compile && npm run lint && npm test", "watch": "npm run _build:compile -- -- --sourceMap --watch", "_bundle:dts": "dts-bundle-generator ./src/index.ts --out-file=./build/index.d.ts --export-referenced-types=false --external-inlines=@nodelib/fs.walk @nodelib/fs.scandir @nodelib/fs.stat", - "_bundle:ts": "esbuild --bundle ./src/index.ts --outfile=./build/index.js --platform=node --target=node18.18 --format=cjs", - "_bundle:build": "npm run _bundle:dts && npm run _bundle:ts", + "_bundle:js": "esbuild --bundle ./out/index.js --outfile=./build/index.js --platform=node --target=node18.18 --format=cjs", + "_bundle:build": "npm run _bundle:dts && npm run _bundle:js", "_bundle:test:replace": "cp ./build/index.js ./out", "_bundle:test": "npm run _bundle:test:replace", "bundle": "npm run _build:compile && npm run _bundle:build && npm run _bundle:test", From 3d2fdba4fde5669e2d99408e346599d669e2e681 Mon Sep 17 00:00:00 2001 From: mrmlnc Date: Fri, 3 Jan 2025 22:32:48 +0300 Subject: [PATCH 4/4] build: extract bundle scripts into js modules --- package.json | 10 ++++++---- scripts/bundle-dts.mjs | 30 ++++++++++++++++++++++++++++++ scripts/bundle-js.mjs | 13 +++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 scripts/bundle-dts.mjs create mode 100644 scripts/bundle-js.mjs diff --git a/package.json b/package.json index 68875369..154bd8ee 100644 --- a/package.json +++ b/package.json @@ -48,9 +48,11 @@ "tinyglobby": "^0.2.10", "typescript": "^5.7.2" }, - "dependencies": { + "optionalDependencies": { "@nodelib/fs.stat": "^4.0.0", - "@nodelib/fs.walk": "^3.0.0", + "@nodelib/fs.walk": "^3.0.1" + }, + "dependencies": { "glob-parent": "^6.0.2", "merge2": "^1.4.1", "micromatch": "^4.0.8" @@ -67,8 +69,8 @@ "_build:compile": "npm run clean && npm run compile", "build": "npm run _build:compile && npm run lint && npm test", "watch": "npm run _build:compile -- -- --sourceMap --watch", - "_bundle:dts": "dts-bundle-generator ./src/index.ts --out-file=./build/index.d.ts --export-referenced-types=false --external-inlines=@nodelib/fs.walk @nodelib/fs.scandir @nodelib/fs.stat", - "_bundle:js": "esbuild --bundle ./out/index.js --outfile=./build/index.js --platform=node --target=node18.18 --format=cjs", + "_bundle:dts": "node ./scripts/bundle-dts.mjs", + "_bundle:js": "node ./scripts/bundle-js.mjs", "_bundle:build": "npm run _bundle:dts && npm run _bundle:js", "_bundle:test:replace": "cp ./build/index.js ./out", "_bundle:test": "npm run _bundle:test:replace", diff --git a/scripts/bundle-dts.mjs b/scripts/bundle-dts.mjs new file mode 100644 index 00000000..2a32e7d2 --- /dev/null +++ b/scripts/bundle-dts.mjs @@ -0,0 +1,30 @@ +import * as fs from 'node:fs'; + +import dbg from 'dts-bundle-generator'; + +const [declarations] = dbg.generateDtsBundle([ + { + filePath: './src/index.ts', + libraries: { + inlinedLibraries: [ + '@nodelib/fs.stat', + '@nodelib/fs.scandir', + '@nodelib/fs.walk', + ], + }, + output: { + exportReferencedTypes: false, + }, + }, +]); + +const imports = declarations.match(/from\s'(?[^']+)'/g); + +const hasOnlyNodeDependencies = imports.every((it) => it.startsWith("from 'node:")); + +if (!hasOnlyNodeDependencies) { + throw new Error('Not only node: dependencies are found in the dts bundle.'); +} + +fs.mkdirSync('./build', { recursive: true }); +fs.writeFileSync('./build/index.d.ts', declarations, 'utf8'); diff --git a/scripts/bundle-js.mjs b/scripts/bundle-js.mjs new file mode 100644 index 00000000..8ae30e5e --- /dev/null +++ b/scripts/bundle-js.mjs @@ -0,0 +1,13 @@ +import esbuild from 'esbuild'; + +import package_ from '../package.json' with { type: 'json' }; + +await esbuild.build({ + bundle: true, + platform: 'node', + target: 'node18.18', + format: 'cjs', + external: Object.keys(package_.dependencies), + entryPoints: ['./out/index.js'], + outfile: './build/index.js', +});