Skip to content

Commit a80324e

Browse files
authored
Merge pull request #82 from ublib/bundle-dts
bundle-dts
2 parents 7add978 + c3ea843 commit a80324e

File tree

4 files changed

+117
-85
lines changed

4 files changed

+117
-85
lines changed
+23-27
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
import fs from "node:fs";
22
import path from "node:path";
3+
import { execSync } from "node:child_process";
4+
35
import * as esbuild from "esbuild";
46
import { rimraf } from "rimraf";
7+
import { bundle } from "dts-bundle";
58

6-
/** @type {(str: string) => string} */
7-
const blue = str => `\x1b[34m${str}\x1b[0m`;
8-
9-
/** @type {(str: string) => string} */
10-
const green = str => `\x1b[32m${str}\x1b[0m`;
11-
12-
/** @type {(dir: string) => void} */
13-
const finishedBuild = dir => console.log(`${green("✔︎")} build: ${blue(dir)}`);
9+
const blue = (str: string) => `\x1b[34m${str}\x1b[0m`;
10+
const green = (str: string) => `\x1b[32m${str}\x1b[0m`;
11+
const finishedBuild = (dir: string) => console.log(`${green("✔︎")} build: ${blue(dir)}`);
1412

1513
const CLI_OUT_DIR = `packages/tools/bin`;
1614

17-
/**
18-
* @type {() => PromiseLike<void>}
19-
*/
2015
export const buildCli = async () => {
21-
/**
22-
* @type {import('esbuild').BuildOptions}
23-
*/
2416
await esbuild.build({
2517
entryPoints: [path.resolve("packages/tools/src/bin")],
2618
bundle: true,
@@ -44,48 +36,52 @@ export const buildCli = async () => {
4436
finishedBuild(`${CLI_OUT_DIR}/jTegaki.zip)`);
4537
};
4638

47-
/**
48-
* @type {() => void}
49-
*/
5039
export const clearCliSync = () => {
5140
rimraf.sync(CLI_OUT_DIR);
5241
};
5342

5443
const PACKAGES = ["tecack", "frontend", "backend", "dataset", "shared"];
5544

56-
/**
57-
* @type {() => PromiseLike<import('esbuild').BuildResult<{ entryPoints: string, outdir: string }>>[]}
58-
*/
5945
export const buildTecack = () =>
6046
PACKAGES.map(pkg => {
61-
/**
62-
* @type {import('esbuild').BuildOptions}
63-
*/
6447
const res = esbuild.build({
6548
entryPoints: [path.resolve(`packages/${pkg}/src/index`)],
6649
bundle: true,
6750
minify: true,
6851
target: "es2018",
6952
outdir: `packages/${pkg}/dist`,
7053
format: "esm",
54+
plugins: [
55+
{
56+
name: "TypeScriptDeclarationsPlugin",
57+
setup(build) {
58+
build.onEnd(() => {
59+
bundle({
60+
name: pkg,
61+
main: `temp/packages/${pkg}/src/index.d.ts`,
62+
out: path.resolve(`packages/${pkg}/dist/index.d.ts`),
63+
});
64+
});
65+
},
66+
},
67+
],
7168
});
7269
res.then(() => finishedBuild(`packages/${pkg}/dist`));
7370
return res;
7471
});
7572

76-
/**
77-
* @type {function(): void}
78-
*/
7973
export const clearTecackSync = () => {
8074
PACKAGES.map(pkg => `packages/${pkg}/dist`).forEach(dir => rimraf.sync(dir));
8175
};
8276

83-
(async function main() {
77+
await (async function main() {
8478
console.log("clear dist...");
8579
await Promise.allSettled([clearCliSync(), clearTecackSync()]);
8680
console.log(`${green("✔︎")} finished clearing dist`);
8781
console.log("building tecack...");
82+
execSync("tsc -p tsconfig.build.json");
8883
const buildingTecack = buildTecack();
8984
const buildingCli = buildCli();
9085
await Promise.all([...buildingTecack, buildingCli]);
86+
execSync("rm -rf temp");
9187
})();

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
"scripts": {
2323
"prepare": "husky install",
2424
"deploy": "nr build && sh scripts/deploy.sh",
25-
"build": "pnpm run bundle && pnpm run build:dts",
26-
"bundle": "node scripts/build.js",
27-
"build:dts": "rm -rf temp && tsc -p tsconfig.build.json && node scripts/dts.js && rm -rf temp",
25+
"build": "tsx build",
2826
"fmt": "prettier",
2927
"lint": "ESLINT_USE_FLAT_CONFIG=true eslint",
3028
"spell-check": "cspell lint \"**\" --no-progress --show-suggestions --show-context --cache",
@@ -45,13 +43,15 @@
4543
},
4644
"devDependencies": {
4745
"cspell": "^7.3.8",
46+
"dts-bundle": "^0.7.3",
4847
"esbuild": "^0.19.4",
4948
"eslint": "^8.51.0",
5049
"eslint-config-prettier": "^9.0.0",
5150
"husky": "^8.0.3",
5251
"lint-staged": "^15.0.1",
5352
"prettier": "^3.0.3",
5453
"rimraf": "^5.0.5",
54+
"tsx": "^3.12.2",
5555
"typescript": "^5.2.2",
5656
"vitepress": "1.0.0-rc.22"
5757
}

pnpm-lock.yaml

+91-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/dts.js

-51
This file was deleted.

0 commit comments

Comments
 (0)