Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle dts files into single file #470

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -35,6 +33,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",
Expand All @@ -49,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"
Expand All @@ -68,9 +69,9 @@
"_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: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: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",
"bundle": "npm run _build:compile && npm run _bundle:build && npm run _bundle:test",
Expand Down
30 changes: 30 additions & 0 deletions scripts/bundle-dts.mjs
Original file line number Diff line number Diff line change
@@ -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'(?<filepath>[^']+)'/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');
13 changes: 13 additions & 0 deletions scripts/bundle-js.mjs
Original file line number Diff line number Diff line change
@@ -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',
});
Loading