Skip to content

Commit

Permalink
fix: support vue-tsc works with ts5
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Apr 29, 2023
1 parent 20b7399 commit ebf8996
Show file tree
Hide file tree
Showing 46 changed files with 973 additions and 782 deletions.
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"dependencies": {
"@vueuse/core": "^8.9.4",
"body-scroll-lock": "4.0.0-beta.0",
"vitepress": "^1.0.0-alpha.36",
"vitepress": "^1.0.0-alpha.73",
"vue": "^3.2.37"
}
}
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
]
},
"devDependencies": {
"@tsconfig/node16-strictest-esm": "^1.0.3",
"@tsconfig/esm": "^1.0.2",
"@tsconfig/node18": "^1.0.1",
"@tsconfig/strictest": "^2.0.0",
"@types/babel__code-frame": "^7.0.2",
"@types/debug": "^4.1.5",
"@types/fs-extra": "^11.0.1",
Expand All @@ -50,8 +52,8 @@
"@types/rimraf": "^3.0.0",
"@types/semver": "^7.3.6",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.16.0",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"chalk": "^4.1.1",
"conventional-changelog-cli": "^2.1.1",
"cross-env": "^7.0.3",
Expand All @@ -73,14 +75,14 @@
"prompts": "^2.4.1",
"publint": "^0.1.9",
"rimraf": "^3.0.2",
"semver": "^7.3.5",
"semver": "^7.5.0",
"simple-git-hooks": "^2.8.0",
"sort-deep-object-arrays": "^1.1.2",
"strip-ansi": "^7.0.0",
"svelte": "^3.46.4",
"tiny-invariant": "^1.1.0",
"typescript": "~4.5.5",
"vite": "^4.0.4",
"typescript": "^5.0.4",
"vite": "^4.3.0",
"vite-plugin-checker": "workspace:*",
"vitest": "^0.28.5",
"ws": "^8.5.0",
Expand Down
14 changes: 8 additions & 6 deletions packages/vite-plugin-checker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"lodash.debounce": "^4.0.8",
"lodash.pick": "^4.4.0",
"npm-run-path": "^4.0.1",
"semver": "^7.5.0",
"strip-ansi": "^6.0.0",
"tiny-invariant": "^1.1.0",
"vscode-languageclient": "^7.0.0",
Expand All @@ -61,13 +62,13 @@
"peerDependencies": {
"eslint": ">=7",
"meow": "^9.0.0",
"optionator": "^0.9.1",
"stylelint": ">=13",
"typescript": "*",
"vite": ">=2.0.0",
"vls": "*",
"vti": "*",
"vue-tsc": "*",
"optionator": "^0.9.1"
"vue-tsc": "*"
},
"peerDependenciesMeta": {
"eslint": {
Expand Down Expand Up @@ -96,20 +97,21 @@
}
},
"devDependencies": {
"@types/fs-extra": "^11.0.1",
"@types/eslint": "^7.2.14",
"@types/fs-extra": "^11.0.1",
"@types/lodash.debounce": "^4.0.6",
"@types/lodash.pick": "^4.4.6",
"@types/semver": "^7.3.13",
"@volar/vue-typescript": "^0.33.0",
"esbuild": "^0.14.27",
"meow": "^9.0.0",
"npm-run-all": "^4.1.5",
"optionator": "^0.9.1",
"stylelint": "^14.0.0",
"tsup": "^6.2.2",
"typescript": "~4.5.5",
"tsup": "^6.7.0",
"typescript": "^5.0.4",
"vls": "^0.7.6",
"vti": "^0.1.7",
"vue-tsc": "^1.1.3"
"vue-tsc": "^1.4.3"
}
}
2 changes: 1 addition & 1 deletion packages/vite-plugin-checker/src/Checker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import invariant from 'tiny-invariant'
import { isInVitestEntryThread, isMainThread } from './utils.js'

import { createScript, Script } from './worker.js'
import { createScript, type Script } from './worker.js'

import type {
CreateDiagnostic,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
toClientPayload,
wrapCheckerSummary,
} from '../../logger.js'
import { ACTION_TYPES, CreateDiagnostic, DiagnosticToRuntime } from '../../types.js'
import { ACTION_TYPES, type CreateDiagnostic, type DiagnosticToRuntime } from '../../types.js'

const __filename = fileURLToPath(import.meta.url)
let createServeAndBuild
Expand Down
12 changes: 6 additions & 6 deletions packages/vite-plugin-checker/src/checkers/vls/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import {
DidChangeTextDocumentNotification,
DidChangeWatchedFilesNotification,
DidOpenTextDocumentNotification,
InitializeParams,
type InitializeParams,
InitializeRequest,
InitializeResult,
Logger,
ServerCapabilities,
type InitializeResult,
type Logger,
type ServerCapabilities,
StreamMessageReader,
StreamMessageWriter,
} from 'vscode-languageserver/node.js'
Expand All @@ -31,10 +31,10 @@ import {
diagnosticToTerminalLog,
normalizeLspDiagnostic,
normalizePublishDiagnosticParams,
NormalizedDiagnostic,
type NormalizedDiagnostic,
} from '../../logger.js'
import type { DeepPartial } from '../../types.js'
import { getInitParams, VlsOptions } from './initParams.js'
import { getInitParams, type VlsOptions } from './initParams.js'

import { FileDiagnosticManager } from '../../FileDiagnosticManager.js'

Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-checker/src/checkers/vls/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
toClientPayload,
} from '../../logger.js'
import { ACTION_TYPES } from '../../types.js'
import { DiagnosticOptions, diagnostics } from './diagnostics.js'
import { type DiagnosticOptions, diagnostics } from './diagnostics.js'

import type { ConfigEnv } from 'vite'
import type { CreateDiagnostic } from '../../types.js'
Expand Down
4 changes: 2 additions & 2 deletions packages/vite-plugin-checker/src/checkers/vueTsc/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
toClientPayload,
wrapCheckerSummary,
} from '../../logger.js'
import { ACTION_TYPES, CreateDiagnostic, DiagnosticToRuntime } from '../../types.js'
import { ACTION_TYPES, type CreateDiagnostic, type DiagnosticToRuntime } from '../../types.js'
import { prepareVueTsc } from './prepareVueTsc.js'

const _require = createRequire(import.meta.url)
Expand All @@ -39,7 +39,7 @@ const createDiagnostic: CreateDiagnostic<'vueTsc'> = (pluginConfig) => {

const { targetTsDir } = await prepareVueTsc()

const vueTs = _require(path.resolve(targetTsDir, 'lib/tsc.js'))
const vueTs = _require(path.resolve(targetTsDir, 'lib/typescript.js'))

const finalConfig =
pluginConfig.vueTsc === true
Expand Down
57 changes: 35 additions & 22 deletions packages/vite-plugin-checker/src/checkers/vueTsc/prepareVueTsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fsExtra from 'fs-extra'
import { createRequire } from 'module'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'
import semver from 'semver'
import { writeFile, access, readFile, rm } from 'fs/promises'

const { copy, mkdir } = fsExtra
Expand Down Expand Up @@ -29,12 +30,13 @@ export async function prepareVueTsc() {
// 1. copy typescript to folder
const targetTsDir = path.resolve(_dirname, 'typescript-vue-tsc')
const vueTscFlagFile = path.resolve(targetTsDir, 'vue-tsc-resolve-path')
// let currTsVersion: string = ''
const currTsVersion = _require('typescript/package.json').version

let shouldBuildFixture = true
try {
await access(targetTsDir)
const targetTsVersion = _require(path.resolve(targetTsDir, 'package.json')).version
const currTsVersion = _require('typescript/package.json').version
// check fixture versions before re-use
await access(vueTscFlagFile)
const fixtureFlagContent = await readFile(vueTscFlagFile, 'utf8')
Expand All @@ -54,39 +56,50 @@ export async function prepareVueTsc() {
await writeFile(vueTscFlagFile, proxyApiPath)

// 2. sync modification of lib/tsc.js with vue-tsc
await overrideTscJs(_require.resolve(path.resolve(targetTsDir, 'lib/tsc.js')))
await overrideTscJs(
_require.resolve(path.resolve(targetTsDir, 'lib/typescript.js')),
currTsVersion
)
}

return { targetTsDir }
}

async function overrideTscJs(tscJsPath: string) {
let result = await readFile(tscJsPath, 'utf8')
async function overrideTscJs(tscJsPath: string, version: string) {
let tsc = await readFile(tscJsPath, 'utf8')
// #region copied from https://github.com/johnsoncodehk/volar/blob/54f7186485d79bc0e9b7ec59ecbc01d681ee5310/vue-language-tools/vue-tsc/bin/vue-tsc.js
const tryReplace = (search: RegExp | string, replace: string | ((v: string) => string)) => {
const before = result
// @ts-ignore
result = result.replace(search, replace)
if (before === result) {
throw 'Search string not found: ' + JSON.stringify(search.toString())
}
}

// add *.vue files to allow extensions
tryReplace(/supportedTSExtensions = .*(?=;)/, (s) => s + '.concat([[".vue"]])')
tryReplace(/supportedJSExtensions = .*(?=;)/, (s) => s + '.concat([[".vue"]])')
tryReplace(/allSupportedExtensions = .*(?=;)/, (s) => s + '.concat([[".vue"]])')
tryReplace(/supportedTSExtensions = .*(?=;)/, (s: string) => s + '.concat([[".vue"]])')
tryReplace(/supportedJSExtensions = .*(?=;)/, (s: string) => s + '.concat([[".vue"]])')
tryReplace(/allSupportedExtensions = .*(?=;)/, (s: string) => s + '.concat([[".vue"]])')

// proxy createProgram apis
tryReplace(
/function createProgram\(.+\) {/,
(s) =>
s + ` return require(${JSON.stringify(proxyApiPath)}).${createProgramFunction}(...arguments);` // tweak for compatibility, will be removed in 0.6.0
(s: string) =>
s + ` return require(${JSON.stringify(proxyApiPath)}).${createProgramFunction}(...arguments);`
)
// #endregion

// change tsc command to module.exports
tryReplace(`ts.executeCommandLine(ts.sys, ts.noop, ts.sys.args);`, `module.exports = ts`)
// patches logic for checking root file extension in build program for incremental builds
if (semver.gt(version, '5.0.0')) {
tryReplace(
`for (const existingRoot of buildInfoVersionMap.roots) {`,
`for (const existingRoot of buildInfoVersionMap.roots
.filter(file => !file.toLowerCase().includes('__vls_'))
.map(file => file.replace(/\.vue\.(j|t)sx?$/i, '.vue'))
) {`
)
}

function tryReplace(search: any, replace: any) {
const before = tsc
tsc = tsc.replace(search, replace)
const after = tsc
if (after === before) {
throw 'Search string not found: ' + JSON.stringify(search.toString())
}
}
// #endregion

await writeFile(tscJsPath, result)
await writeFile(tscJsPath, tsc)
}
2 changes: 1 addition & 1 deletion packages/vite-plugin-checker/src/codeFrame.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os from 'os'
import type ts from 'typescript'

import { codeFrameColumns, SourceLocation } from '@babel/code-frame'
import { codeFrameColumns, type SourceLocation } from '@babel/code-frame'

export function createFrame({
source,
Expand Down
6 changes: 3 additions & 3 deletions packages/vite-plugin-checker/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import * as _vscodeUri from 'vscode-uri'
const URI = _vscodeUri?.default?.URI ?? _vscodeUri.URI
import { parentPort } from 'worker_threads'

import { codeFrameColumns, SourceLocation } from '@babel/code-frame'
import { codeFrameColumns, type SourceLocation } from '@babel/code-frame'

import { WS_CHECKER_ERROR_EVENT } from './client/index.js'
import {
ACTION_TYPES,
DiagnosticLevel,
DiagnosticToRuntime,
ClientDiagnosticPayload,
type DiagnosticToRuntime,
type ClientDiagnosticPayload,
} from './types.js'
import { isMainThread } from './utils.js'

Expand Down
18 changes: 9 additions & 9 deletions packages/vite-plugin-checker/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import {
} from './client/index.js'
import {
ACTION_TYPES,
BuildCheckBinStr,
BuildInCheckerNames,
ClientDiagnosticPayload,
ClientReconnectPayload,
Action,
PluginConfig,
ServeAndBuildChecker,
SharedConfig,
UserPluginConfig,
type BuildCheckBinStr,
type BuildInCheckerNames,
type ClientDiagnosticPayload,
type ClientReconnectPayload,
type Action,
type PluginConfig,
type ServeAndBuildChecker,
type SharedConfig,
type UserPluginConfig,
} from './types.js'

import type { ConfigEnv, Plugin, Logger } from 'vite'
Expand Down
6 changes: 5 additions & 1 deletion packages/vite-plugin-checker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"extends": "@tsconfig/node16-strictest-esm/tsconfig.json",
"extends": [
"@tsconfig/strictest/tsconfig",
"@tsconfig/node18/tsconfig",
"@tsconfig/esm/tsconfig"
],
"compilerOptions": {
"isolatedModules": true,
"noUnusedParameters": false,
Expand Down
4 changes: 2 additions & 2 deletions playground/backend-integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"@vitejs/plugin-react": "^2.0.0",
"express": "^4.18.2",
"http-proxy-middleware": "^2.0.6",
"typescript": "~4.5.5",
"vite": "^4.0.4",
"typescript": "^5.0.4",
"vite": "^4.3.0",
"vite-plugin-checker": "workspace:*"
}
}
8 changes: 4 additions & 4 deletions playground/config-default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"devDependencies": {
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.30.7",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"eslint": "^8.11.0",
"typescript": "~4.5.5",
"vite": "^4.0.4",
"typescript": "^5.0.4",
"vite": "^4.3.0",
"vite-plugin-checker": "workspace:*"
}
}
8 changes: 4 additions & 4 deletions playground/config-enableBuild-false/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"devDependencies": {
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.30.7",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"eslint": "^8.11.0",
"typescript": "~4.5.5",
"vite": "^4.0.4",
"typescript": "^5.0.4",
"vite": "^4.3.0",
"vite-plugin-checker": "workspace:*"
}
}
8 changes: 4 additions & 4 deletions playground/config-initialIsOpen-false/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"devDependencies": {
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.30.7",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"eslint": "^8.11.0",
"typescript": "~4.5.5",
"vite": "^4.0.4",
"typescript": "^5.0.4",
"vite": "^4.3.0",
"vite-plugin-checker": "workspace:*"
}
}
Loading

0 comments on commit ebf8996

Please sign in to comment.