Skip to content

Commit

Permalink
feat: support emit declaration files only (#253)
Browse files Browse the repository at this point in the history
* feat: support only output dts

* chore: update api docs

---------

Co-authored-by: qmhc <40221744+qmhc@users.noreply.github.com>
  • Loading branch information
biggerstar and qmhc committed Aug 4, 2023
1 parent 3767fdf commit a23d063
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -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
*
Expand Down
9 changes: 9 additions & 0 deletions README.zh-CN.md
Expand Up @@ -286,6 +286,15 @@ export interface PluginOptions {
*/
copyDtsFiles?: boolean,

/**
* 是否只生成类型文件
*
* 当为 `true` 时会强制删除所有 Vite(Rollup)的原始产物
*
* @default false
*/
declarationOnly?: boolean,

/**
* 指定插件的输出等级
*
Expand Down
3 changes: 2 additions & 1 deletion examples/ts/vite.config.ts
Expand Up @@ -27,7 +27,8 @@ export default defineConfig({
// aliasesExclude: [/^@components/],
staticImport: true,
// insertTypesEntry: true,
rollupTypes: true
rollupTypes: true,
declarationOnly: true
})
]
})
Expand Down
8 changes: 8 additions & 0 deletions src/plugin.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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]
}
}
}
}
}
9 changes: 9 additions & 0 deletions src/types.ts
Expand Up @@ -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
*
Expand Down

0 comments on commit a23d063

Please sign in to comment.