From 1ebd6659b71a2e6d72f1df6e52a890f012b5f6e0 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Mon, 29 Dec 2025 22:57:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=20bun.config.ts=20?= =?UTF-8?q?=E5=92=8C=20index.ts=EF=BC=8C=E6=B7=BB=E5=8A=A0=E7=9B=B8?= =?UTF-8?q?=E5=AF=B9=E8=B7=AF=E5=BE=84=E8=A7=A3=E6=9E=90=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=B9=B6=E6=96=B0=E5=A2=9E=E6=B5=8B=E8=AF=95=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bun.config.ts | 2 +- package.json | 7 ++----- pnpm-lock.yaml | 48 +++--------------------------------------------- src/index.ts | 42 ++++++++++++++++++++++++++++++++++-------- test/index.ts | 3 +++ 5 files changed, 43 insertions(+), 59 deletions(-) create mode 100644 test/index.ts diff --git a/bun.config.ts b/bun.config.ts index 0b33a26..4c3de9f 100644 --- a/bun.config.ts +++ b/bun.config.ts @@ -1,3 +1,3 @@ import { buildWithBun } from './src/index.ts' -await buildWithBun({ meta: import.meta, dts: true }) \ No newline at end of file +await buildWithBun({ dts: true }) \ No newline at end of file diff --git a/package.json b/package.json index ad74dc2..6657447 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kevisual/code-builder", - "version": "0.0.2", + "version": "0.0.3", "description": "", "main": "src/index.ts", "bin": { @@ -23,10 +23,7 @@ "author": "abearxiong ", "license": "MIT", "type": "module", - "dependencies": { - "@kevisual/use-config": "^1.0.21", - "nanoid": "^5.1.6" - }, + "dependencies": {}, "devDependencies": { "@kevisual/types": "^0.0.10", "commander": "^14.0.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8f53cd5..e414b13 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7,16 +7,6 @@ settings: importers: .: - dependencies: - '@kevisual/use-config': - specifier: ^1.0.21 - version: 1.0.21(dotenv@17.2.3) - commander: - specifier: ^14.0.2 - version: 14.0.2 - nanoid: - specifier: ^5.1.6 - version: 5.1.6 devDependencies: '@kevisual/types': specifier: ^0.0.10 @@ -27,20 +17,15 @@ importers: '@types/node': specifier: ^25.0.3 version: 25.0.3 + commander: + specifier: ^14.0.2 + version: 14.0.2 packages: - '@kevisual/load@0.0.6': - resolution: {integrity: sha512-+3YTFehRcZ1haGel5DKYMUwmi5i6f2psyaPZlfkKU/cOXgkpwoG9/BEqPCnPjicKqqnksEpixVRkyHJ+5bjLVA==} - '@kevisual/types@0.0.10': resolution: {integrity: sha512-Q73uzzjk9UidumnmCvOpgzqDDvQxsblz22bIFuoiioUFJWwaparx8bpd8ArRyFojicYL1YJoFDzDZ9j9NN8grA==} - '@kevisual/use-config@1.0.21': - resolution: {integrity: sha512-czgy4+tBDBJI6QTnKh2PCwswET6ZpZ4ZqBE/SPkkOivEtlrcPzLs5elwMLZ3goD1XMD4VB3yjumb5WuW/8H8MA==} - peerDependencies: - dotenv: ^17 - '@types/bun@1.3.5': resolution: {integrity: sha512-RnygCqNrd3srIPEWBd5LFeUYG7plCoH2Yw9WaZGyNmdTEei+gWaHqydbaIRkIkcbXwhBT94q78QljxN0Sk838w==} @@ -54,34 +39,13 @@ packages: resolution: {integrity: sha512-TywoWNNRbhoD0BXs1P3ZEScW8W5iKrnbithIl0YH+uCmBd0QpPOA8yc82DS3BIE5Ma6FnBVUsJ7wVUDz4dvOWQ==} engines: {node: '>=20'} - dotenv@17.2.3: - resolution: {integrity: sha512-JVUnt+DUIzu87TABbhPmNfVdBDt18BLOWjMUFJMSi/Qqg7NTYtabbvSNJGOJ7afbRuv9D/lngizHtP7QyLQ+9w==} - engines: {node: '>=12'} - - eventemitter3@5.0.1: - resolution: {integrity: sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==} - - nanoid@5.1.6: - resolution: {integrity: sha512-c7+7RQ+dMB5dPwwCp4ee1/iV/q2P6aK1mTZcfr1BTuVlyW9hJYiMPybJCcnBlQtuSmTIWNeazm/zqNoZSSElBg==} - engines: {node: ^18 || >=20} - hasBin: true - undici-types@7.16.0: resolution: {integrity: sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw==} snapshots: - '@kevisual/load@0.0.6': - dependencies: - eventemitter3: 5.0.1 - '@kevisual/types@0.0.10': {} - '@kevisual/use-config@1.0.21(dotenv@17.2.3)': - dependencies: - '@kevisual/load': 0.0.6 - dotenv: 17.2.3 - '@types/bun@1.3.5': dependencies: bun-types: 1.3.5 @@ -96,10 +60,4 @@ snapshots: commander@14.0.2: {} - dotenv@17.2.3: {} - - eventemitter3@5.0.1: {} - - nanoid@5.1.6: {} - undici-types@7.16.0: {} diff --git a/src/index.ts b/src/index.ts index f37dea5..729efe0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,33 @@ -import { resolvePath } from '@kevisual/use-config'; import { execSync } from 'node:child_process'; import { BuildConfig } from 'bun'; +import path from 'node:path'; type EntryOptions = { + /** 入口文件路径 */ entry?: string; + /** 输出目录 */ outDir?: string; + /** 输出文件命名 */ naming?: string; + /** 外部依赖 */ external?: string[]; + /** 构建目标 */ target?: 'node' | 'browser'; - dts?: boolean, - meta?: { url: string }; + /** 是否生成类型声明文件 */ + dts?: boolean; + /** 当前工作目录 */ + cwd?: string; + /** + * 相对路径基准,比如bun.config.ts所在路径 + */ + meta?: ImportMeta; }; + +const resolvePath = (cwd: string, relativePath: string) => { + if (path.isAbsolute(relativePath)) { + return relativePath; + } + return path.resolve(cwd, relativePath); +} export const gererateBunConfig = (options: EntryOptions = {}): { config: BuildConfig, dtsCommand?: string } => { const { entry = 'src/index.ts', @@ -18,10 +36,16 @@ export const gererateBunConfig = (options: EntryOptions = {}): { config: BuildCo external = [], target = 'node', dts = false, - meta + meta, + cwd = process.cwd(), } = options; - const entrypoint = resolvePath(entry, { meta }); - const outputDir = resolvePath(outDir, { meta }); + let entrypoint = resolvePath(cwd, entry); + let outputDir = resolvePath(cwd, outDir); + if (meta && meta.url) { + const metaDir = path.dirname(new URL(meta.url).pathname); + entrypoint = resolvePath(metaDir, entry); + outputDir = resolvePath(metaDir, outDir); + } return { config: { target: target, @@ -39,12 +63,11 @@ export const gererateBunConfig = (options: EntryOptions = {}): { config: BuildCo export const buildWithBun = async (options: EntryOptions = {}) => { const { config, dtsCommand } = gererateBunConfig(options); - const meta = options.meta; await Bun.build(config); if (dtsCommand) { execSync(dtsCommand, { stdio: 'inherit', - cwd: resolvePath('./', { meta }) + cwd: options.cwd || process.cwd(), }); } } @@ -65,8 +88,11 @@ export const parse = async () => { .option('-n, --naming ', '输出文件命名', 'app') .option('-t, --target ', '构建目标 (node 或 browser)', 'node') .option('--dts', '生成 TypeScript 声明文件', false).action(async (options) => { + const cwd = process.cwd(); + await buildWithBun({ entry: options.entry, + cwd, outDir: options.outDir, naming: options.naming, target: options.target, diff --git a/test/index.ts b/test/index.ts new file mode 100644 index 0000000..b05242c --- /dev/null +++ b/test/index.ts @@ -0,0 +1,3 @@ +export const test = () => { + return 'test'; +}; \ No newline at end of file