feat: refactor build process to use Bun, removing Rollup configuration
- Implemented `gererateBunConfig` and `buildWithBun` functions for building projects with Bun. - Added command-line interface using Commander for building projects. - Removed Rollup-related code and configuration. - Deleted unused config module. - Updated TypeScript configuration to extend from a shared config. - Added .npmrc for authentication tokens. - Created bun.config.ts for Bun build configuration. - Added README documentation for usage instructions.
This commit is contained in:
99
src/index.ts
99
src/index.ts
@@ -1,23 +1,78 @@
|
||||
// rollup.config.js
|
||||
|
||||
import typescript from '@rollup/plugin-typescript';
|
||||
import resolve from '@rollup/plugin-node-resolve';
|
||||
import commonjs from '@rollup/plugin-commonjs';
|
||||
import json from '@rollup/plugin-json';
|
||||
|
||||
import { rollup } from 'rollup';
|
||||
|
||||
export const build = async () => {
|
||||
const bundle = await rollup({
|
||||
input: 'src/index.ts',
|
||||
output: {
|
||||
file: 'dist/app.mjs',
|
||||
format: 'es',
|
||||
},
|
||||
watch: {
|
||||
include: 'src/**',
|
||||
},
|
||||
// @ts-ignore
|
||||
plugins: [typescript(), resolve(), commonjs(), json()],
|
||||
});
|
||||
import { resolvePath } from '@kevisual/use-config';
|
||||
import { execSync } from 'node:child_process';
|
||||
import { BuildConfig } from 'bun';
|
||||
type EntryOptions = {
|
||||
entry?: string;
|
||||
outDir?: string;
|
||||
naming?: string;
|
||||
external?: string[];
|
||||
target?: 'node' | 'browser';
|
||||
dts?: boolean,
|
||||
meta?: { url: string };
|
||||
};
|
||||
export const gererateBunConfig = (options: EntryOptions = {}): { config: BuildConfig, dtsCommand?: string } => {
|
||||
const {
|
||||
entry = 'src/index.ts',
|
||||
outDir = './dist',
|
||||
naming = 'app',
|
||||
external = [],
|
||||
target = 'node',
|
||||
dts = false,
|
||||
meta
|
||||
} = options;
|
||||
const entrypoint = resolvePath(entry, { meta });
|
||||
const outputDir = resolvePath(outDir, { meta });
|
||||
return {
|
||||
config: {
|
||||
target: target,
|
||||
format: 'esm',
|
||||
entrypoints: [entrypoint],
|
||||
outdir: outputDir,
|
||||
naming: {
|
||||
entry: `${naming}.js`,
|
||||
},
|
||||
external,
|
||||
},
|
||||
dtsCommand: dts ? `dts -i ${entry} -o ${naming}.d.ts` : undefined
|
||||
}
|
||||
}
|
||||
|
||||
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 })
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
export const parse = async () => {
|
||||
const { Command } = await import('commander');
|
||||
const program = new Command();
|
||||
program
|
||||
.name('code-builder')
|
||||
.description('使用 Bun 的代码构建工具')
|
||||
.version('0.0.2');
|
||||
const build = new Command('build').description('使用 Bun 构建项目');
|
||||
build
|
||||
.option('-e, --entry <path>', '入口文件', 'src/index.ts')
|
||||
.option('-o, --outDir <path>', '输出目录', './dist')
|
||||
.option('-n, --naming <name>', '输出文件命名', 'app')
|
||||
.option('-t, --target <target>', '构建目标 (node 或 browser)', 'node')
|
||||
.option('--dts', '生成 TypeScript 声明文件', false).action(async (options) => {
|
||||
await buildWithBun({
|
||||
entry: options.entry,
|
||||
outDir: options.outDir,
|
||||
naming: options.naming,
|
||||
target: options.target,
|
||||
dts: options.dts,
|
||||
});
|
||||
});
|
||||
program.addCommand(build);
|
||||
program.parse(process.argv);
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
import { resolve } from 'path';
|
||||
|
||||
export const runPath = () => {
|
||||
return resolve(process.cwd());
|
||||
};
|
||||
Reference in New Issue
Block a user