File size: 1,959 Bytes
b91e262 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import tsNextPluginFactory from 'next'
import ts from 'typescript'
export { NEXT_TS_ERRORS } from 'next/dist/server/typescript/constant'
export type PluginLanguageService = ts.LanguageService & {
getCapturedLogs: () => string
}
export function getPluginLanguageService(dir: string): PluginLanguageService {
const files = ts.sys.readDirectory(dir)
const compilerOptions = ts.getDefaultCompilerOptions()
const compilerHost = ts.createCompilerHost(compilerOptions)
let logs = ''
const logger = {
info: (...args: any[]) => {
const message = args
.map((arg) => (typeof arg === 'string' ? arg : JSON.stringify(arg)))
.join(' ')
logs += message + '\n'
console.log(...args)
},
}
const languageServiceHost: ts.LanguageServiceHost = {
...compilerHost,
getCompilationSettings: () => compilerOptions,
getScriptFileNames: () => files,
getScriptSnapshot: (fileName) => {
const contents = ts.sys.readFile(fileName)
if (contents && typeof contents === 'string') {
return ts.ScriptSnapshot.fromString(contents)
}
return
},
getScriptVersion: () => '0',
writeFile: ts.sys.writeFile,
}
const languageService = ts.createLanguageService(languageServiceHost)
const pluginCreateInfo: ts.server.PluginCreateInfo = {
project: {
projectService: {
logger,
},
getCurrentDirectory: () => dir,
} as unknown as ts.server.Project,
languageService,
languageServiceHost,
serverHost: null,
config: {},
}
const plugin: ts.server.PluginModule = (
tsNextPluginFactory as unknown as ts.server.PluginModuleFactory
)({ typescript: ts })
const service = plugin.create(pluginCreateInfo) as PluginLanguageService
// Add a custom method to get captured logs
service.getCapturedLogs = () => logs
return service
}
export function getTsFiles(dir: string): string[] {
return ts.sys.readDirectory(dir)
}
|