diff --git a/README.md b/README.md index 91fe626..08371f5 100644 --- a/README.md +++ b/README.md @@ -286,6 +286,15 @@ export interface PluginOptions { */ copyDtsFiles?: boolean, + /** + * Whether to emit declaration files only + * + * When `true`, all the original outputs of vite (rollup) will be force removed + * + * @default false + */ + declarationOnly?: boolean, + /** * Logging level for this plugin * diff --git a/README.zh-CN.md b/README.zh-CN.md index 914569a..c5b595b 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -286,6 +286,15 @@ export interface PluginOptions { */ copyDtsFiles?: boolean, + /** + * 是否只生成类型文件 + * + * 当为 `true` 时会强制删除所有 Vite(Rollup)的原始产物 + * + * @default false + */ + declarationOnly?: boolean, + /** * 指定插件的输出等级 * diff --git a/examples/ts/vite.config.ts b/examples/ts/vite.config.ts index 2d31809..0121229 100644 --- a/examples/ts/vite.config.ts +++ b/examples/ts/vite.config.ts @@ -27,7 +27,8 @@ export default defineConfig({ // aliasesExclude: [/^@components/], staticImport: true, // insertTypesEntry: true, - rollupTypes: true + rollupTypes: true, + declarationOnly: true }) ] }) diff --git a/src/plugin.ts b/src/plugin.ts index 4905319..5b223b1 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -76,6 +76,7 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { pathsToAliases = true, aliasesExclude = [], copyDtsFiles = false, + declarationOnly = false, strictOutput = true, afterDiagnostic = noop, beforeWriteFile = noop, @@ -691,6 +692,13 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin { logger.info( green(`${logPrefix} Declaration files built in ${timeRecord + Date.now() - startTime}ms.\n`) ) + }, + generateBundle(_, bundle) { + if (declarationOnly) { + for (const id of Object.keys(bundle)) { + delete bundle[id] + } + } } } } diff --git a/src/types.ts b/src/types.ts index 4789302..6c72570 100644 --- a/src/types.ts +++ b/src/types.ts @@ -192,6 +192,15 @@ export interface PluginOptions { */ copyDtsFiles?: boolean, + /** + * Whether to emit declaration files only + * + * When `true`, all the original outputs of vite (rollup) will be force removed + * + * @default false + */ + declarationOnly?: boolean, + /** * Logging level for this plugin *