From c939e358dfecacaf2dfd2b0d42118c16462c260a Mon Sep 17 00:00:00 2001 From: rk_zhang <40221744+qmhc@users.noreply.github.com> Date: Mon, 21 Mar 2022 17:08:25 +0800 Subject: [PATCH] feat: support entryRoot option fix #59 BREAKING CHANGE: The calculating output paths funtion of root option is assigned to entryRoot option, and currently will auto calculating the smallest public path as the default value for entryRoot option. --- package.json | 2 +- src/plugin.ts | 13 ++++++++++--- src/utils.ts | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index d7a6da1..59320b3 100644 --- a/package.json +++ b/package.json @@ -75,7 +75,7 @@ "url": "git+https://github.com/qmhc/vite-plugin-dts.git" }, "scripts": { - "build": "tsup src/index.ts --dts --format cjs,esm --external @vue/compiler-sfc", + "build": "tsup-node src/index.ts --dts --format cjs,esm", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s --commit-path .", "dev": "pnpm run build -- --watch", "lint": "pnpm run lint:src && pnpm run lint:example", diff --git a/src/plugin.ts b/src/plugin.ts index b5e05cf..065a9cb 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -20,7 +20,8 @@ import { mergeObjects, ensureAbsolute, ensureArray, - runParallel + runParallel, + queryPublicPath } from './utils' import type { Plugin, Alias, Logger } from 'vite' @@ -36,6 +37,7 @@ export interface PluginOptions { exclude?: string | string[], root?: string, outputDir?: string, + entryRoot?: string, compilerOptions?: ts.CompilerOptions | null, tsConfigFilePath?: string, cleanVueFileName?: boolean, @@ -83,6 +85,7 @@ export function dtsPlugin(options: PluginOptions = {}): Plugin { const compilerOptions = options.compilerOptions ?? {} let root: string + let entryRoot: string let aliases: Alias[] let entries: string[] let logger: Logger @@ -314,6 +317,10 @@ export function dtsPlugin(options: PluginOptions = {}): Plugin { })) ) + if (!entryRoot) { + entryRoot = queryPublicPath(outputFiles.map(file => file.path)) + } + bundleDebug('emit') await runParallel(os.cpus().length, outputFiles, async outputFile => { @@ -337,7 +344,7 @@ export function dtsPlugin(options: PluginOptions = {}): Plugin { filePath = resolve( outputDir, - relative(root, cleanVueFileName ? filePath.replace('.vue.d.ts', '.d.ts') : filePath) + relative(entryRoot, cleanVueFileName ? filePath.replace('.vue.d.ts', '.d.ts') : filePath) ) if (typeof beforeWriteFile === 'function') { @@ -369,7 +376,7 @@ export function dtsPlugin(options: PluginOptions = {}): Plugin { entries .map(entry => { let filePath = normalizePath( - relative(dirname(typesPath), resolve(outputDir, relative(root, entry))) + relative(dirname(typesPath), resolve(outputDir, relative(entryRoot, entry))) ) filePath = filePath.replace(tsRE, '') diff --git a/src/utils.ts b/src/utils.ts index 3205532..05ff210 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,4 @@ -import { resolve, isAbsolute } from 'path' +import { resolve, isAbsolute, dirname } from 'path' export function isNativeObj = Record>( value: T @@ -99,3 +99,50 @@ export async function runParallel( return Promise.all(ret) } + +export function queryPublicPath(paths: string[]) { + if (paths.length === 0) { + return '' + } else if (paths.length === 1) { + return dirname(paths[0]) + } + + let publicPath = dirname(paths[0]) + '\\' + let publicUnits = publicPath.split(/[\\/]/) + let index = publicUnits.length - 1 + + for (const path of paths.slice(1)) { + if (!index) { + return publicPath + } + + const dirPath = dirname(path) + '\\' + + if (dirPath.startsWith(publicPath)) { + continue + } + + const units = dirPath.split(/[\\/]/) + + if (units.length < index) { + publicPath = dirPath + publicUnits = units + continue + } + + for (let i = 0; i <= index; ++i) { + if (publicUnits[i] !== units[i]) { + if (!i) { + return '' + } + + index = i - 1 + publicUnits = publicUnits.slice(0, index + 1) + publicPath = publicUnits.join('\\') + '\\' + break + } + } + } + + return publicPath.slice(0, -1) +}