diff --git a/src/cli.ts b/src/cli.ts index 3dd2fbf8..979f2366 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -7,8 +7,8 @@ import { handlError } from './errors' const cli = cac('tsup') cli - .command('<...files>', 'Entry files') - .option('--out-dir', 'Output directory', { default: 'dist' }) + .command('<...files>', 'Bundle files') + .option('-d, --out-dir', 'Output directory', { default: 'dist' }) .option('--format ', 'Bundle format, "cjs", "iife", "umd", "esm"', { default: 'cjs', }) @@ -19,6 +19,7 @@ cli .option('--bundle', 'Bundle node_modules') .option('--dts', 'Generate declaration file') .option('--watch', 'Watch mode') + .option('--define.* ', 'Define compile-time constants') .option('--jsxFactory ', 'Name of JSX factory function', { default: 'React.createElement', }) @@ -38,6 +39,7 @@ cli dts: options.dts, bundle: options.bundle, outDir: options.outDir, + define: options.define, }) if (options.watch) { const watcher = watch( @@ -67,12 +69,14 @@ cli .command('run ', 'Bundle and execute a file', { allowUnknownOptions: true, }) - .action(async (file: string) => { + .option('--define.* ', 'Define compile-time constants') + .action(async (file: string, options) => { const extraArgs = process.argv.slice(process.argv.indexOf(file) + 1) const { rollup } = await import('rollup') const { createRollupConfigs } = await import('./') const { runCode } = await import('./run') const [rollupConfig] = await createRollupConfigs([file], { + define: options.define, outDir: 'dist', format: 'cjs', }) diff --git a/src/index.ts b/src/index.ts index 58e2dba9..c14a5ab0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,9 @@ type Options = { jsxFragment?: string outDir: string format: ModuleFormat + define?: { + [k: string]: string + } } export async function createRollupConfigs(files: string[], options: Options) { @@ -34,6 +37,7 @@ export async function createRollupConfigs(files: string[], options: Options) { minify: options.minify, jsxFactory: options.jsxFactory, jsxFragment: options.jsxFragment, + define: options.define }), dts && dtsPlugin(), ].filter(Boolean),