generated from mizdra/npm-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from mizdra/update-volar
Update volar
- Loading branch information
Showing
8 changed files
with
112 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"recommendations": ["esbenp.prettier-vscode", "dbaeumer.vscode-eslint", "ZixuanChen.vitest-explorer"] | ||
"recommendations": ["esbenp.prettier-vscode", "dbaeumer.vscode-eslint", "vitest.explorer"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,6 @@ | |
"next": "13.4.1", | ||
"react": "18.2.0", | ||
"react-dom": "18.2.0", | ||
"typescript": "^5.3.0" | ||
"typescript": "^5.7.2" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,62 @@ | ||
import path from 'node:path'; | ||
import type { LanguagePlugin } from '@volar/language-core'; | ||
import type * as ts from 'typescript/lib/tsserverlibrary'; | ||
import type {} from '@volar/typescript'; | ||
import ts from 'typescript/lib/tsserverlibrary'; | ||
import { getDtsContent } from '../dts'; | ||
import { AssetPluginOptions } from '../option'; | ||
|
||
export function createAssetLanguage(sys: ts.System, assetPluginOptions: AssetPluginOptions): LanguagePlugin { | ||
export function createAssetLanguage(sys: ts.System, assetPluginOptions: AssetPluginOptions): LanguagePlugin<string> { | ||
return { | ||
createVirtualCode(fileId) { | ||
const fileName = fileId.includes('://') ? fileId.split('://')[1] ?? '' : fileId; | ||
if ( | ||
isMatchFile(fileName, assetPluginOptions.extensions, assetPluginOptions.exclude, assetPluginOptions.include) | ||
) { | ||
const dtsContent = getDtsContent( | ||
fileName, | ||
assetPluginOptions.exportedNameCase, | ||
assetPluginOptions.exportedNamePrefix, | ||
); | ||
return { | ||
id: 'main', | ||
mappings: [], | ||
embeddedCodes: [], | ||
languageId: 'typescript', | ||
snapshot: { | ||
getText: (start, end) => dtsContent.substring(start, end), | ||
getLength: () => dtsContent.length, | ||
getChangeRange: () => undefined, | ||
}, | ||
}; | ||
} | ||
getLanguageId(scriptId) { | ||
if (isMatchFile(scriptId)) return 'asset'; | ||
return undefined; | ||
}, | ||
updateVirtualCode(_fileId, virtualCode) { | ||
return virtualCode; // asset file content update does not affect virtual code | ||
createVirtualCode(scriptId, languageId) { | ||
if (languageId !== 'asset') return undefined; | ||
const dtsContent = getDtsContent( | ||
scriptId, | ||
assetPluginOptions.exportedNameCase, | ||
assetPluginOptions.exportedNamePrefix, | ||
); | ||
return { | ||
id: 'main', | ||
languageId: 'asset', | ||
snapshot: { | ||
getText: (start, end) => dtsContent.substring(start, end), | ||
getLength: () => dtsContent.length, | ||
getChangeRange: () => undefined, | ||
}, | ||
mappings: [], | ||
}; | ||
}, | ||
typescript: { | ||
extraFileExtensions: assetPluginOptions.extensions.map((ext) => ({ | ||
extension: ext.slice(1), | ||
isMixedContent: true, | ||
scriptKind: 7, | ||
})), | ||
getScript(virtualCode) { | ||
extraFileExtensions: assetPluginOptions.extensions.map((ext) => { | ||
return { | ||
extension: ext.slice(1), | ||
isMixedContent: true, | ||
scriptKind: ts.ScriptKind.TS, | ||
}; | ||
}), | ||
getServiceScript(root) { | ||
return { | ||
code: virtualCode, | ||
extension: '.ts', | ||
scriptKind: 3, | ||
code: root, | ||
extension: ts.Extension.Ts, | ||
scriptKind: ts.ScriptKind.TS, | ||
}; | ||
}, | ||
}, | ||
}; | ||
|
||
function isMatchFile(fileName: string, extensions: string[], exclude: string[], include: string[]): boolean { | ||
if (!extensions.includes(path.extname(fileName))) return false; | ||
return sys.readDirectory(path.dirname(fileName), extensions, exclude, include, 1).includes(fileName); | ||
function isMatchFile(fileName: string): boolean { | ||
if (!assetPluginOptions.extensions.includes(path.extname(fileName))) return false; | ||
return sys | ||
.readDirectory( | ||
path.dirname(fileName), | ||
assetPluginOptions.extensions, | ||
assetPluginOptions.exclude, | ||
assetPluginOptions.include, | ||
1, | ||
) | ||
.includes(fileName); | ||
} | ||
} |