feat: 更新 bun.config.ts 和 index.ts,添加相对路径解析功能并新增测试文件
This commit is contained in:
@@ -1,3 +1,3 @@
|
|||||||
import { buildWithBun } from './src/index.ts'
|
import { buildWithBun } from './src/index.ts'
|
||||||
|
|
||||||
await buildWithBun({ meta: import.meta, dts: true })
|
await buildWithBun({ dts: true })
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@kevisual/code-builder",
|
"name": "@kevisual/code-builder",
|
||||||
"version": "0.0.2",
|
"version": "0.0.3",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "src/index.ts",
|
"main": "src/index.ts",
|
||||||
"bin": {
|
"bin": {
|
||||||
@@ -23,10 +23,7 @@
|
|||||||
"author": "abearxiong <xiongxiao@xiongxiao.me>",
|
"author": "abearxiong <xiongxiao@xiongxiao.me>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {},
|
||||||
"@kevisual/use-config": "^1.0.21",
|
|
||||||
"nanoid": "^5.1.6"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@kevisual/types": "^0.0.10",
|
"@kevisual/types": "^0.0.10",
|
||||||
"commander": "^14.0.2",
|
"commander": "^14.0.2",
|
||||||
|
|||||||
48
pnpm-lock.yaml
generated
48
pnpm-lock.yaml
generated
@@ -7,16 +7,6 @@ settings:
|
|||||||
importers:
|
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:
|
devDependencies:
|
||||||
'@kevisual/types':
|
'@kevisual/types':
|
||||||
specifier: ^0.0.10
|
specifier: ^0.0.10
|
||||||
@@ -27,20 +17,15 @@ importers:
|
|||||||
'@types/node':
|
'@types/node':
|
||||||
specifier: ^25.0.3
|
specifier: ^25.0.3
|
||||||
version: 25.0.3
|
version: 25.0.3
|
||||||
|
commander:
|
||||||
|
specifier: ^14.0.2
|
||||||
|
version: 14.0.2
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
|
|
||||||
'@kevisual/load@0.0.6':
|
|
||||||
resolution: {integrity: sha512-+3YTFehRcZ1haGel5DKYMUwmi5i6f2psyaPZlfkKU/cOXgkpwoG9/BEqPCnPjicKqqnksEpixVRkyHJ+5bjLVA==}
|
|
||||||
|
|
||||||
'@kevisual/types@0.0.10':
|
'@kevisual/types@0.0.10':
|
||||||
resolution: {integrity: sha512-Q73uzzjk9UidumnmCvOpgzqDDvQxsblz22bIFuoiioUFJWwaparx8bpd8ArRyFojicYL1YJoFDzDZ9j9NN8grA==}
|
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':
|
'@types/bun@1.3.5':
|
||||||
resolution: {integrity: sha512-RnygCqNrd3srIPEWBd5LFeUYG7plCoH2Yw9WaZGyNmdTEei+gWaHqydbaIRkIkcbXwhBT94q78QljxN0Sk838w==}
|
resolution: {integrity: sha512-RnygCqNrd3srIPEWBd5LFeUYG7plCoH2Yw9WaZGyNmdTEei+gWaHqydbaIRkIkcbXwhBT94q78QljxN0Sk838w==}
|
||||||
|
|
||||||
@@ -54,34 +39,13 @@ packages:
|
|||||||
resolution: {integrity: sha512-TywoWNNRbhoD0BXs1P3ZEScW8W5iKrnbithIl0YH+uCmBd0QpPOA8yc82DS3BIE5Ma6FnBVUsJ7wVUDz4dvOWQ==}
|
resolution: {integrity: sha512-TywoWNNRbhoD0BXs1P3ZEScW8W5iKrnbithIl0YH+uCmBd0QpPOA8yc82DS3BIE5Ma6FnBVUsJ7wVUDz4dvOWQ==}
|
||||||
engines: {node: '>=20'}
|
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:
|
undici-types@7.16.0:
|
||||||
resolution: {integrity: sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw==}
|
resolution: {integrity: sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw==}
|
||||||
|
|
||||||
snapshots:
|
snapshots:
|
||||||
|
|
||||||
'@kevisual/load@0.0.6':
|
|
||||||
dependencies:
|
|
||||||
eventemitter3: 5.0.1
|
|
||||||
|
|
||||||
'@kevisual/types@0.0.10': {}
|
'@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':
|
'@types/bun@1.3.5':
|
||||||
dependencies:
|
dependencies:
|
||||||
bun-types: 1.3.5
|
bun-types: 1.3.5
|
||||||
@@ -96,10 +60,4 @@ snapshots:
|
|||||||
|
|
||||||
commander@14.0.2: {}
|
commander@14.0.2: {}
|
||||||
|
|
||||||
dotenv@17.2.3: {}
|
|
||||||
|
|
||||||
eventemitter3@5.0.1: {}
|
|
||||||
|
|
||||||
nanoid@5.1.6: {}
|
|
||||||
|
|
||||||
undici-types@7.16.0: {}
|
undici-types@7.16.0: {}
|
||||||
|
|||||||
42
src/index.ts
42
src/index.ts
@@ -1,15 +1,33 @@
|
|||||||
import { resolvePath } from '@kevisual/use-config';
|
|
||||||
import { execSync } from 'node:child_process';
|
import { execSync } from 'node:child_process';
|
||||||
import { BuildConfig } from 'bun';
|
import { BuildConfig } from 'bun';
|
||||||
|
import path from 'node:path';
|
||||||
type EntryOptions = {
|
type EntryOptions = {
|
||||||
|
/** 入口文件路径 */
|
||||||
entry?: string;
|
entry?: string;
|
||||||
|
/** 输出目录 */
|
||||||
outDir?: string;
|
outDir?: string;
|
||||||
|
/** 输出文件命名 */
|
||||||
naming?: string;
|
naming?: string;
|
||||||
|
/** 外部依赖 */
|
||||||
external?: string[];
|
external?: string[];
|
||||||
|
/** 构建目标 */
|
||||||
target?: 'node' | 'browser';
|
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 } => {
|
export const gererateBunConfig = (options: EntryOptions = {}): { config: BuildConfig, dtsCommand?: string } => {
|
||||||
const {
|
const {
|
||||||
entry = 'src/index.ts',
|
entry = 'src/index.ts',
|
||||||
@@ -18,10 +36,16 @@ export const gererateBunConfig = (options: EntryOptions = {}): { config: BuildCo
|
|||||||
external = [],
|
external = [],
|
||||||
target = 'node',
|
target = 'node',
|
||||||
dts = false,
|
dts = false,
|
||||||
meta
|
meta,
|
||||||
|
cwd = process.cwd(),
|
||||||
} = options;
|
} = options;
|
||||||
const entrypoint = resolvePath(entry, { meta });
|
let entrypoint = resolvePath(cwd, entry);
|
||||||
const outputDir = resolvePath(outDir, { meta });
|
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 {
|
return {
|
||||||
config: {
|
config: {
|
||||||
target: target,
|
target: target,
|
||||||
@@ -39,12 +63,11 @@ export const gererateBunConfig = (options: EntryOptions = {}): { config: BuildCo
|
|||||||
|
|
||||||
export const buildWithBun = async (options: EntryOptions = {}) => {
|
export const buildWithBun = async (options: EntryOptions = {}) => {
|
||||||
const { config, dtsCommand } = gererateBunConfig(options);
|
const { config, dtsCommand } = gererateBunConfig(options);
|
||||||
const meta = options.meta;
|
|
||||||
await Bun.build(config);
|
await Bun.build(config);
|
||||||
if (dtsCommand) {
|
if (dtsCommand) {
|
||||||
execSync(dtsCommand, {
|
execSync(dtsCommand, {
|
||||||
stdio: 'inherit',
|
stdio: 'inherit',
|
||||||
cwd: resolvePath('./', { meta })
|
cwd: options.cwd || process.cwd(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,8 +88,11 @@ export const parse = async () => {
|
|||||||
.option('-n, --naming <name>', '输出文件命名', 'app')
|
.option('-n, --naming <name>', '输出文件命名', 'app')
|
||||||
.option('-t, --target <target>', '构建目标 (node 或 browser)', 'node')
|
.option('-t, --target <target>', '构建目标 (node 或 browser)', 'node')
|
||||||
.option('--dts', '生成 TypeScript 声明文件', false).action(async (options) => {
|
.option('--dts', '生成 TypeScript 声明文件', false).action(async (options) => {
|
||||||
|
const cwd = process.cwd();
|
||||||
|
|
||||||
await buildWithBun({
|
await buildWithBun({
|
||||||
entry: options.entry,
|
entry: options.entry,
|
||||||
|
cwd,
|
||||||
outDir: options.outDir,
|
outDir: options.outDir,
|
||||||
naming: options.naming,
|
naming: options.naming,
|
||||||
target: options.target,
|
target: options.target,
|
||||||
|
|||||||
3
test/index.ts
Normal file
3
test/index.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export const test = () => {
|
||||||
|
return 'test';
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user