From a23d06317925c0ccd9a77227590ce18cec44195a Mon Sep 17 00:00:00 2001 From: hedada <58533654+biggerstar@users.noreply.github.com> Date: Fri, 4 Aug 2023 18:54:39 +0800 Subject: [PATCH] feat: support emit declaration files only (#253) * feat: support only output dts * chore: update api docs --------- Co-authored-by: qmhc <40221744+qmhc@users.noreply.github.com> --- README.md | 9 +++++++++ README.zh-CN.md | 9 +++++++++ examples/ts/vite.config.ts | 3 ++- src/plugin.ts | 8 ++++++++ src/types.ts | 9 +++++++++ 5 files changed, 37 insertions(+), 1 deletion(-) 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 *