Skip to content

Commit

Permalink
feat: auto switch to original program if no Vue
Browse files Browse the repository at this point in the history
fix #363
  • Loading branch information
qmhc committed Aug 28, 2024
1 parent 068e711 commit a04a191
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 56 deletions.
125 changes: 70 additions & 55 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,88 @@ import {

import { proxyCreateProgram } from '@volar/typescript'
import ts from 'typescript'

import { getPackageInfoSync, resolveModule } from 'local-pkg'
import { removeEmitGlobalTypes } from 'vue-tsc'

export { createParsedCommandLine }

const _createProgram = proxyCreateProgram(ts, ts.createProgram, (ts, options) => {
const { configFilePath } = options.options
const vueOptions =
typeof configFilePath === 'string'
? createParsedCommandLine(ts, ts.sys, configFilePath.replace(/\\/g, '/')).vueOptions
: resolveVueCompilerOptions({})
let hasVue = false

try {
hasVue = !!(
getPackageInfoSync('vue') ??
getPackageInfoSync('svelte', { paths: [resolveModule('svelte') || process.cwd()] })
)
} catch (e) {}

if (options.host) {
const writeFile = options.host.writeFile.bind(options.host)
options.host.writeFile = (fileName, contents, ...args) => {
return writeFile(fileName, removeEmitGlobalTypes(contents), ...args)
// If there has no Vue dependency, we think it's a normal TypeScript project.
// So we use the original createProgram of TypeScript.
const _createProgram = !hasVue
? ts.createProgram
: proxyCreateProgram(ts, ts.createProgram, (ts, options) => {
const { configFilePath } = options.options
const vueOptions =
typeof configFilePath === 'string'
? createParsedCommandLine(ts, ts.sys, configFilePath.replace(/\\/g, '/')).vueOptions
: resolveVueCompilerOptions({})

if (options.host) {
const writeFile = options.host.writeFile.bind(options.host)
options.host.writeFile = (fileName, contents, ...args) => {
return writeFile(fileName, removeEmitGlobalTypes(contents), ...args)
}
}
}

const vueLanguagePlugin = createVueLanguagePlugin2<string>(
ts,
id => id,
createRootFileChecker(
undefined,
() => options.rootNames.map(rootName => rootName.replace(/\\/g, '/')),
options.host?.useCaseSensitiveFileNames?.() ?? false
),
options.options,
vueOptions
)
return [vueLanguagePlugin]
})
const vueLanguagePlugin = createVueLanguagePlugin2<string>(
ts,
id => id,
createRootFileChecker(
undefined,
() => options.rootNames.map(rootName => rootName.replace(/\\/g, '/')),
options.host?.useCaseSensitiveFileNames?.() ?? false
),
options.options,
vueOptions
)
return [vueLanguagePlugin]
})

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

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

return program
}
return program
}
2 changes: 1 addition & 1 deletion src/resolvers/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function querySvelteVersion() {
getPackageInfoSync('svelte')?.version ??
getPackageInfoSync('svelte', { paths: [resolveModule('svelte') || process.cwd()] })?.version
lowerVersion = version ? compare(version, '4.0.0', '<') : false
} catch {
} catch (e) {
lowerVersion = false
}
}
Expand Down

0 comments on commit a04a191

Please sign in to comment.