Skip to content

Commit

Permalink
fix: keep output relative to root not to entry dir
Browse files Browse the repository at this point in the history
When depend on some parallel dir, keep output relative to entry dir will generate declaration files in unnexpected places

BREAKING CHANGE: Ouput declaration structure no longer relative to entry dir
  • Loading branch information
qmhc committed Jun 7, 2021
1 parent 417d637 commit e83ec64
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
1 change: 1 addition & 0 deletions .commitlintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'body-leading-blank': [2, 'always'],
'body-max-line-length': [2, 'always', 200],
'footer-leading-blank': [1, 'always'],
'header-max-length': [2, 'always', 108],
'subject-empty': [2, 'never'],
Expand Down
47 changes: 28 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default (options: PluginOptions = {}): Plugin => {
// const outputDir = options.outputDir ? ensureAbsolute(options.outputDir, root) : ''

// let external: ExternalOption | undefined
let entry: string
// let entry: string
let aliases: Alias[]

let project: Project
Expand Down Expand Up @@ -79,23 +79,32 @@ export default (options: PluginOptions = {}): Plugin => {
// external = config?.build?.rollupOptions?.external ?? undefined
const lib = config?.build?.lib

if (lib) {
entry = lib.entry
} else {
const input = config?.build?.rollupOptions?.input

if (typeof input !== 'string') {
console.log(
chalk.yellow(
'\n[vite:dts] You may have multiple entries, it will make difficult to calculate relative paths.\n'
)
if (!lib) {
console.log(
chalk.yellow(
'\n[vite:dts] You building not a library, it may not need to generate declaration files.\n'
)
}

entry = typeof input === 'string' ? input : ''
)
}

entry = ensureAbsolute(entry && dirname(entry), root)
// if (lib) {
// entry = lib.entry
// } else {
// const input = config?.build?.rollupOptions?.input

// if (typeof input !== 'string') {
// console.log(
// chalk.yellow(
// '\n[vite:dts] You may have multiple entries, it will make difficult to calculate relative paths.\n'
// )
// )
// }

// entry = typeof input === 'string' ? input : ''
// }

// entry = ensureAbsolute(entry && dirname(entry), root)

root = ensureAbsolute(options.root ?? '', config.root)

project = new Project({
Expand Down Expand Up @@ -153,13 +162,13 @@ export default (options: PluginOptions = {}): Plugin => {

for (const outputFile of emitOutput.getOutputFiles()) {
let filePath = outputFile.getFilePath() as string
let content = transformAliasImport(outputFile.getText(), aliases, entry)
let content = transformAliasImport(outputFile.getText(), aliases, root)

content = staticImport ? transformDynamicImport(content) : content

filePath = resolve(
declarationDir,
relative(entry, cleanVueFileName ? filePath.replace('.vue.d.ts', '.d.ts') : filePath)
relative(root, cleanVueFileName ? filePath.replace('.vue.d.ts', '.d.ts') : filePath)
)

if (typeof beforeWriteFile === 'function') {
Expand Down Expand Up @@ -233,7 +242,7 @@ function isAliasMatch(alias: Alias, importee: string) {
return importee.indexOf(alias.find) === 0 && importee.substring(alias.find.length)[0] === '/'
}

function transformAliasImport(content: string, aliases: Alias[], entry: string) {
function transformAliasImport(content: string, aliases: Alias[], root: string) {
if (!aliases.length) return content

return content.replace(/(?:import|export)\s?(?:type)?\s?\{.+\}\s?from\s?['"].+['"]/g, str => {
Expand All @@ -248,7 +257,7 @@ function transformAliasImport(content: string, aliases: Alias[], entry: string)
`$1'${matchResult[1].replace(
matchedAlias.find,
isAbsolute(matchedAlias.replacement)
? normalizePath(relative(entry, matchedAlias.replacement))
? normalizePath(relative(root, matchedAlias.replacement))
: normalizePath(matchedAlias.replacement)
)}'`
)
Expand Down

0 comments on commit e83ec64

Please sign in to comment.