Skip to content

Commit

Permalink
build: extract bundle scripts into js modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmlnc committed Jan 3, 2025
1 parent 4083f47 commit 3d2fdba
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
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',
});

0 comments on commit 3d2fdba

Please sign in to comment.