|
| 1 | +#!/usr/bin/env node |
| 2 | +import module from 'node:module'; |
| 3 | + |
| 4 | +import { cac } from 'cac'; |
| 5 | +import { build, type UserConfig, globalLogger } from 'tsdown'; |
| 6 | + |
| 7 | +import { resolveConfig } from './index.js'; |
| 8 | + |
| 9 | +const cli = cac('vite lib'); |
| 10 | +cli.help(); |
| 11 | + |
| 12 | +const DEFAULT_ENV_PREFIXES = ['VITE_LIB_', 'TSDOWN_']; |
| 13 | + |
| 14 | +cli |
| 15 | + .command('[...files]', 'Bundle files', { |
| 16 | + ignoreOptionDefaultValue: true, |
| 17 | + allowUnknownOptions: true, |
| 18 | + }) |
| 19 | + // Only support config file in vite.config.ts |
| 20 | + // .option('-c, --config <filename>', 'Use a custom config file') |
| 21 | + .option('--config-loader <loader>', 'Config loader to use: auto, native, unrun', { |
| 22 | + default: 'auto', |
| 23 | + }) |
| 24 | + .option('--no-config', 'Disable config file') |
| 25 | + .option('-f, --format <format>', 'Bundle format: esm, cjs, iife, umd', { |
| 26 | + default: 'esm', |
| 27 | + }) |
| 28 | + .option('--clean', 'Clean output directory, --no-clean to disable') |
| 29 | + .option('--external <module>', 'Mark dependencies as external') |
| 30 | + .option('--minify', 'Minify output') |
| 31 | + .option('--devtools', 'Enable devtools integration') |
| 32 | + .option('--debug [feat]', 'Show debug logs') |
| 33 | + .option('--target <target>', 'Bundle target, e.g "es2015", "esnext"') |
| 34 | + .option('-l, --logLevel <level>', 'Set log level: info, warn, error, silent') |
| 35 | + .option('--fail-on-warn', 'Fail on warnings', { default: true }) |
| 36 | + .option('--no-write', 'Disable writing files to disk, incompatible with watch mode') |
| 37 | + .option('-d, --out-dir <dir>', 'Output directory', { default: 'dist' }) |
| 38 | + .option('--treeshake', 'Tree-shake bundle', { default: true }) |
| 39 | + .option('--sourcemap', 'Generate source map', { default: false }) |
| 40 | + .option('--shims', 'Enable cjs and esm shims ', { default: false }) |
| 41 | + .option('--platform <platform>', 'Target platform', { |
| 42 | + default: 'node', |
| 43 | + }) |
| 44 | + .option('--dts', 'Generate dts files') |
| 45 | + .option('--publint', 'Enable publint', { default: false }) |
| 46 | + .option('--attw', 'Enable Are the types wrong integration', { |
| 47 | + default: false, |
| 48 | + }) |
| 49 | + .option('--unused', 'Enable unused dependencies check', { default: false }) |
| 50 | + .option('-w, --watch [path]', 'Watch mode') |
| 51 | + .option('--ignore-watch <path>', 'Ignore custom paths in watch mode') |
| 52 | + .option('--from-vite [vitest]', 'Reuse config from Vite or Vitest') |
| 53 | + .option('--report', 'Size report', { default: true }) |
| 54 | + .option('--env.* <value>', 'Define compile-time env variables') |
| 55 | + .option( |
| 56 | + '--env-file <file>', |
| 57 | + 'Load environment variables from a file, when used together with --env, variables in --env take precedence', |
| 58 | + ) |
| 59 | + .option('--env-prefix <prefix>', 'Prefix for env variables to inject into the bundle', { |
| 60 | + // support `TSDOWN_` for migration compatibility |
| 61 | + default: DEFAULT_ENV_PREFIXES, |
| 62 | + }) |
| 63 | + .option('--on-success <command>', 'Command to run on success') |
| 64 | + .option('--copy <dir>', 'Copy files to output dir') |
| 65 | + .option('--public-dir <dir>', 'Alias for --copy, deprecated') |
| 66 | + .option('--tsconfig <tsconfig>', 'Set tsconfig path') |
| 67 | + .option('--unbundle', 'Unbundle mode') |
| 68 | + .option('-W, --workspace [dir]', 'Enable workspace mode') |
| 69 | + .option('-F, --filter <pattern>', 'Filter configs (cwd or name), e.g. /pkg-name$/ or pkg-name') |
| 70 | + .option('--exports', 'Generate export-related metadata for package.json (experimental)') |
| 71 | + .action(async (input: string[], flags: UserConfig) => { |
| 72 | + const viteConfig = await resolveConfig({ root: process.cwd() }, 'build'); |
| 73 | + if (input.length > 0) flags.entry = input; |
| 74 | + // TODO: set default envPrefix after tsdown upgrade |
| 75 | + await build({ |
| 76 | + ...(viteConfig.lib as UserConfig), |
| 77 | + ...flags, |
| 78 | + config: false, |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | +export async function runCLI(): Promise<void> { |
| 83 | + cli.parse(process.argv, { run: false }); |
| 84 | + |
| 85 | + // TODO: enable debug after tsdown upgrade |
| 86 | + // enableDebug(cli.options) |
| 87 | + |
| 88 | + try { |
| 89 | + await cli.runMatchedCommand(); |
| 90 | + } catch (error: any) { |
| 91 | + globalLogger.error(String(error.stack || error.message)); |
| 92 | + process.exit(1); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +if (module.enableCompileCache) { |
| 97 | + module.enableCompileCache(); |
| 98 | +} |
| 99 | +runCLI(); |
0 commit comments