Skip to content

Commit

Permalink
fix: remove global types for vue declaration files
Browse files Browse the repository at this point in the history
fix #354
  • Loading branch information
qmhc committed Aug 5, 2024
1 parent d0613f4 commit e873107
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/vue/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
types/
dist/
tsc-dist/
node_modules/
components/*.d.ts
docs/
7 changes: 4 additions & 3 deletions examples/vue/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "vue-example",
"version": "0.0.0",
"auth": "qmhc",
"type": "module",
"private": true,
"scripts": {
"build": "DEBUG=\"vite-plugin-dts:bundle\" vite build"
"build": "DEBUG=\"vite-plugin-dts:bundle\" vite build",
"tsc": "vue-tsc --declaration --emitDeclarationOnly --outDir tsc-dist"
},
"dependencies": {
"vue": "^3.4.32"
Expand All @@ -16,5 +16,6 @@
"tslib": "^2.6.2",
"typescript": "5.5.3",
"vite": "^5.3.4"
}
},
"auth": "qmhc"
}
40 changes: 39 additions & 1 deletion src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { removeEmitGlobalTypes } from 'vue-tsc'

export { createParsedCommandLine }

export const createProgram = proxyCreateProgram(ts, ts.createProgram, (ts, options) => {
const _createProgram = proxyCreateProgram(ts, ts.createProgram, (ts, options) => {
const { configFilePath } = options.options
const vueOptions =
typeof configFilePath === 'string'
Expand All @@ -39,3 +39,41 @@ export const createProgram = proxyCreateProgram(ts, ts.createProgram, (ts, optio
)
return [vueLanguagePlugin]
})

export const createProgram = (options: ts.CreateProgramOptions) => {
const program = _createProgram(options)

const emit = program.emit
program.emit = (
targetSourceFile,
writeFile,
cancellationToken,
emitOnlyDtsFiles,
customTransformers
) => {
if (writeFile) {
return emit(
targetSourceFile,
(fileName, data, writeByteOrderMark, onError, sourceFiles) => {
if (fileName.endsWith('.d.ts')) {
data = removeEmitGlobalTypes(data)
}
return writeFile(fileName, data, writeByteOrderMark, onError, sourceFiles)
},
cancellationToken,
emitOnlyDtsFiles,
customTransformers
)
}

return emit(
targetSourceFile,
writeFile,
cancellationToken,
emitOnlyDtsFiles,
customTransformers
)
}

return program
}
10 changes: 2 additions & 8 deletions src/resolvers/vue.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { removeEmitGlobalTypes } from 'vue-tsc'
import { base64VLQEncode } from '../utils'

import type { Resolver } from '../types'
Expand Down Expand Up @@ -30,19 +31,12 @@ export function VueResolver(): Resolver {
program.emit(
sourceFile,
(path, content) => {
outputs.push({ path, content })
outputs.push({ path, content: removeEmitGlobalTypes(content) })
},
undefined,
true
)

// const outputs = service.getEmitOutput(sourceFile.fileName, true).outputFiles.map(file => {
// return {
// path: file.name,
// content: file.text
// }
// })

if (!program.getCompilerOptions().declarationMap) return outputs

const [beforeScript] = code.split(/\s*<script.*>/)
Expand Down

0 comments on commit e873107

Please sign in to comment.