Skip to content

Commit

Permalink
fix: normalize paths of resolver transform returns
Browse files Browse the repository at this point in the history
  • Loading branch information
qmhc committed Jul 8, 2023
1 parent 956aec4 commit 3b5a945
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -98,7 +98,7 @@ export interface Resolver {
/**
* Transform source to declaration files
*
* Note that the path of the return files should base on `root`, not `outDir`
* Note that the path of the returns should base on `outDir`, or relative path to `root`
*/
transform: (payload: {
id: string,
Expand Down
2 changes: 1 addition & 1 deletion README.zh-CN.md
Expand Up @@ -98,7 +98,7 @@ export interface Resolver {
/**
* 将源文件转换为类型文件
*
* 注意,返回的文件的路径应该基于 `root` 而不是 `outDir`
* 注意,返回的文件的路径应该基于 `outDir`,或者相对于 `root`
*/
transform: (payload: {
id: string,
Expand Down
2 changes: 1 addition & 1 deletion examples/ts/tsconfig.json
Expand Up @@ -3,7 +3,7 @@
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"moduleResolution": "Bundler",
"importHelpers": true,
"outDir": "dist",
"strict": true,
Expand Down
2 changes: 1 addition & 1 deletion examples/ts/vite.config.ts
Expand Up @@ -26,7 +26,7 @@ export default defineConfig({
exclude: ['src/ignore'],
// aliasesExclude: [/^@components/],
staticImport: true,
rollupTypes: true,
// rollupTypes: true,
insertTypesEntry: true
})
]
Expand Down
15 changes: 12 additions & 3 deletions src/plugin.ts
Expand Up @@ -344,14 +344,20 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {
})

for (const { path, content } of result) {
outputFiles.set(ensureAbsolute(path, publicRoot), content)
outputFiles.set(
resolve(publicRoot, relative(outDir, ensureAbsolute(path, outDir))),
content
)
}
} else {
const sourceFile = program.getSourceFile(id)

if (sourceFile) {
for (const outputFile of service.getEmitOutput(sourceFile.fileName, true).outputFiles) {
outputFiles.set(resolve(publicRoot, relative(outDir, outputFile.name)), outputFile.text)
outputFiles.set(
resolve(publicRoot, relative(outDir, ensureAbsolute(outputFile.name, outDir))),
outputFile.text
)
}
}
}
Expand Down Expand Up @@ -429,7 +435,10 @@ export function dtsPlugin(options: PluginOptions = {}): import('vite').Plugin {

if (rootFiles.has(sourceFile.fileName)) {
for (const outputFile of service.getEmitOutput(sourceFile.fileName, true).outputFiles) {
outputFiles.set(resolve(publicRoot, relative(outDir, outputFile.name)), outputFile.text)
outputFiles.set(
resolve(publicRoot, relative(outDir, ensureAbsolute(outputFile.name, outDir))),
outputFile.text
)
}

rootFiles.delete(sourceFile.fileName)
Expand Down
6 changes: 4 additions & 2 deletions src/resolvers/json.ts
@@ -1,3 +1,5 @@
import { relative } from 'node:path'

import type { Resolver } from '../types'

const jsonRE = /\.json$/
Expand All @@ -8,14 +10,14 @@ export function JsonResolver(): Resolver {
supports(id) {
return jsonRE.test(id)
},
transform({ id, program }) {
transform({ id, root, program }) {
const sourceFile = program.getSourceFile(id)

if (!sourceFile) return []

return [
{
path: `${id}.d.ts`,
path: relative(root, `${id}.d.ts`),
content: `declare const _default: ${sourceFile.text};\n\nexport default _default;\n`
}
]
Expand Down
6 changes: 4 additions & 2 deletions src/resolvers/svelte.ts
@@ -1,3 +1,5 @@
import { relative } from 'node:path'

import type { Resolver } from '../types'

const svelteRE = /\.svelte$/
Expand All @@ -8,10 +10,10 @@ export function SvelteResolver(): Resolver {
supports(id) {
return svelteRE.test(id)
},
transform({ id }) {
transform({ id, root }) {
return [
{
path: `${id}.d.ts`,
path: relative(root, `${id}.d.ts`),
content: "export { SvelteComponentTyped as default } from 'svelte';\n"
}
]
Expand Down
8 changes: 2 additions & 6 deletions src/resolvers/vue.ts
@@ -1,7 +1,3 @@
import { relative } from 'node:path'

import { resolve } from '../utils'

import type { Resolver } from '../types'

const vueRE = /\.vue$/
Expand All @@ -12,7 +8,7 @@ export function VueResolver(): Resolver {
supports(id) {
return vueRE.test(id)
},
transform({ id, root, outDir, program, service }) {
transform({ id, program, service }) {
const sourceFile =
program.getSourceFile(id) ||
program.getSourceFile(id + '.ts') ||
Expand All @@ -24,7 +20,7 @@ export function VueResolver(): Resolver {

return service.getEmitOutput(sourceFile.fileName, true).outputFiles.map(file => {
return {
path: resolve(root, relative(outDir, file.name)),
path: file.name,
content: file.text
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Expand Up @@ -17,7 +17,7 @@ export interface Resolver {
/**
* Transform source to declaration files
*
* Note that the path of the return files should base on `root`, not `outDir`
* Note that the path of the returns should base on `outDir`, or relative path to `root`
*/
transform: (payload: {
id: string,
Expand Down

0 comments on commit 3b5a945

Please sign in to comment.