Skip to content

Commit

Permalink
feat: support entryRoot option
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
qmhc committed Mar 22, 2022
1 parent 64ba2ef commit c939e35
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
13 changes: 10 additions & 3 deletions src/plugin.ts
Expand Up @@ -20,7 +20,8 @@ import {
mergeObjects,
ensureAbsolute,
ensureArray,
runParallel
runParallel,
queryPublicPath
} from './utils'

import type { Plugin, Alias, Logger } from 'vite'
Expand All @@ -36,6 +37,7 @@ export interface PluginOptions {
exclude?: string | string[],
root?: string,
outputDir?: string,
entryRoot?: string,
compilerOptions?: ts.CompilerOptions | null,
tsConfigFilePath?: string,
cleanVueFileName?: boolean,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 => {
Expand All @@ -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') {
Expand Down Expand Up @@ -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, '')
Expand Down
49 changes: 48 additions & 1 deletion src/utils.ts
@@ -1,4 +1,4 @@
import { resolve, isAbsolute } from 'path'
import { resolve, isAbsolute, dirname } from 'path'

export function isNativeObj<T extends Record<string, any> = Record<string, any>>(
value: T
Expand Down Expand Up @@ -99,3 +99,50 @@ export async function runParallel<T>(

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)
}

0 comments on commit c939e35

Please sign in to comment.