Skip to content

Commit

Permalink
feat: add cli flag --define
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed May 13, 2020
1 parent 5f019f3 commit 8eab2f7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <format>', 'Bundle format, "cjs", "iife", "umd", "esm"', {
default: 'cjs',
})
Expand All @@ -19,6 +19,7 @@ cli
.option('--bundle', 'Bundle node_modules')
.option('--dts', 'Generate declaration file')
.option('--watch', 'Watch mode')
.option('--define.* <value>', 'Define compile-time constants')
.option('--jsxFactory <jsxFactory>', 'Name of JSX factory function', {
default: 'React.createElement',
})
Expand All @@ -38,6 +39,7 @@ cli
dts: options.dts,
bundle: options.bundle,
outDir: options.outDir,
define: options.define,
})
if (options.watch) {
const watcher = watch(
Expand Down Expand Up @@ -67,12 +69,14 @@ cli
.command('run <file>', 'Bundle and execute a file', {
allowUnknownOptions: true,
})
.action(async (file: string) => {
.option('--define.* <value>', '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',
})
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type Options = {
jsxFragment?: string
outDir: string
format: ModuleFormat
define?: {
[k: string]: string
}
}

export async function createRollupConfigs(files: string[], options: Options) {
Expand All @@ -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),
Expand Down

0 comments on commit 8eab2f7

Please sign in to comment.