vue-component-meta
Advanced tools
Comparing version 2.0.29 to 2.1.0
import * as ts from 'typescript'; | ||
import type { MetaCheckerOptions } from './lib/types'; | ||
export * from './lib/types'; | ||
/** | ||
* @deprecated Use `createCheckerByJson` instead. | ||
*/ | ||
export declare const createComponentMetaCheckerByJsonConfig: typeof createCheckerByJson; | ||
/** | ||
* @deprecated Use `createChecker` instead. | ||
*/ | ||
export declare const createComponentMetaChecker: typeof createChecker; | ||
export declare function createCheckerByJson(rootPath: string, json: any, checkerOptions?: MetaCheckerOptions): { | ||
getExportNames: (componentPath: string) => string[]; | ||
getComponentMeta: (componentPath: string, exportName?: string) => import("./lib/types").ComponentMeta; | ||
updateFile(fileName: string, text: string): void; | ||
@@ -17,4 +11,2 @@ deleteFile(fileName: string): void; | ||
clearCache(): void; | ||
getExportNames: (componentPath: string) => string[]; | ||
getComponentMeta: (componentPath: string, exportName?: string) => import("./lib/types").ComponentMeta; | ||
__internal__: { | ||
@@ -25,2 +17,4 @@ tsLs: ts.LanguageService; | ||
export declare function createChecker(tsconfig: string, checkerOptions?: MetaCheckerOptions): { | ||
getExportNames: (componentPath: string) => string[]; | ||
getComponentMeta: (componentPath: string, exportName?: string) => import("./lib/types").ComponentMeta; | ||
updateFile(fileName: string, text: string): void; | ||
@@ -30,4 +24,2 @@ deleteFile(fileName: string): void; | ||
clearCache(): void; | ||
getExportNames: (componentPath: string) => string[]; | ||
getComponentMeta: (componentPath: string, exportName?: string) => import("./lib/types").ComponentMeta; | ||
__internal__: { | ||
@@ -34,0 +26,0 @@ tsLs: ts.LanguageService; |
@@ -17,3 +17,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createComponentMetaChecker = exports.createComponentMetaCheckerByJsonConfig = void 0; | ||
exports.createCheckerByJson = createCheckerByJson; | ||
@@ -24,10 +23,2 @@ exports.createChecker = createChecker; | ||
__exportStar(require("./lib/types"), exports); | ||
/** | ||
* @deprecated Use `createCheckerByJson` instead. | ||
*/ | ||
exports.createComponentMetaCheckerByJsonConfig = createCheckerByJson; | ||
/** | ||
* @deprecated Use `createChecker` instead. | ||
*/ | ||
exports.createComponentMetaChecker = createChecker; | ||
function createCheckerByJson(rootPath, json, checkerOptions = {}) { | ||
@@ -34,0 +25,0 @@ return (0, base_1.createCheckerByJsonConfigBase)(ts, rootPath, json, checkerOptions); |
import * as vue from '@vue/language-core'; | ||
import type * as ts from 'typescript'; | ||
import { TypeScriptProjectHost } from '@volar/typescript'; | ||
import type { MetaCheckerOptions, ComponentMeta } from './types'; | ||
import type { ComponentMeta, MetaCheckerOptions } from './types'; | ||
export * from './types'; | ||
export declare function createCheckerByJsonConfigBase(ts: typeof import('typescript'), rootDir: string, json: any, checkerOptions?: MetaCheckerOptions): { | ||
getExportNames: (componentPath: string) => string[]; | ||
getComponentMeta: (componentPath: string, exportName?: string) => ComponentMeta; | ||
updateFile(fileName: string, text: string): void; | ||
@@ -11,4 +12,2 @@ deleteFile(fileName: string): void; | ||
clearCache(): void; | ||
getExportNames: (componentPath: string) => string[]; | ||
getComponentMeta: (componentPath: string, exportName?: string) => ComponentMeta; | ||
__internal__: { | ||
@@ -19,2 +18,4 @@ tsLs: ts.LanguageService; | ||
export declare function createCheckerBase(ts: typeof import('typescript'), tsconfig: string, checkerOptions?: MetaCheckerOptions): { | ||
getExportNames: (componentPath: string) => string[]; | ||
getComponentMeta: (componentPath: string, exportName?: string) => ComponentMeta; | ||
updateFile(fileName: string, text: string): void; | ||
@@ -24,4 +25,2 @@ deleteFile(fileName: string): void; | ||
clearCache(): void; | ||
getExportNames: (componentPath: string) => string[]; | ||
getComponentMeta: (componentPath: string, exportName?: string) => ComponentMeta; | ||
__internal__: { | ||
@@ -31,5 +30,9 @@ tsLs: ts.LanguageService; | ||
}; | ||
export declare function baseCreate(ts: typeof import('typescript'), projectHost: TypeScriptProjectHost, vueCompilerOptions: vue.VueCompilerOptions, checkerOptions: MetaCheckerOptions, globalComponentName: string): { | ||
export declare function baseCreate(ts: typeof import('typescript'), getCommandLine: () => vue.ParsedCommandLine, checkerOptions: MetaCheckerOptions, rootPath: string, globalComponentName: string): { | ||
getExportNames: (componentPath: string) => string[]; | ||
getComponentMeta: (componentPath: string, exportName?: string) => ComponentMeta; | ||
updateFile(fileName: string, text: string): void; | ||
deleteFile(fileName: string): void; | ||
reload(): void; | ||
clearCache(): void; | ||
__internal__: { | ||
@@ -36,0 +39,0 @@ tsLs: ts.LanguageService; |
149
lib/base.js
@@ -20,2 +20,3 @@ "use strict"; | ||
exports.baseCreate = baseCreate; | ||
const typescript_1 = require("@volar/typescript"); | ||
const vue = require("@vue/language-core"); | ||
@@ -25,3 +26,2 @@ const path = require("path-browserify"); | ||
const vue2_1 = require("vue-component-type-helpers/vue2"); | ||
const typescript_1 = require("@volar/typescript"); | ||
__exportStar(require("./types"), exports); | ||
@@ -31,60 +31,23 @@ const windowsPathReg = /\\/g; | ||
rootDir = rootDir.replace(windowsPathReg, '/'); | ||
return createCheckerWorker(ts, () => vue.createParsedCommandLineByJson(ts, ts.sys, rootDir, json), checkerOptions, rootDir, path.join(rootDir, 'jsconfig.json.global.vue')); | ||
return baseCreate(ts, () => vue.createParsedCommandLineByJson(ts, ts.sys, rootDir, json), checkerOptions, rootDir, path.join(rootDir, 'jsconfig.json.global.vue')); | ||
} | ||
function createCheckerBase(ts, tsconfig, checkerOptions = {}) { | ||
tsconfig = tsconfig.replace(windowsPathReg, '/'); | ||
return createCheckerWorker(ts, () => vue.createParsedCommandLine(ts, ts.sys, tsconfig), checkerOptions, path.dirname(tsconfig), tsconfig + '.global.vue'); | ||
return baseCreate(ts, () => vue.createParsedCommandLine(ts, ts.sys, tsconfig), checkerOptions, path.dirname(tsconfig), tsconfig + '.global.vue'); | ||
} | ||
function createCheckerWorker(ts, loadParsedCommandLine, checkerOptions, rootPath, globalComponentName) { | ||
/** | ||
* Original Host | ||
*/ | ||
let parsedCommandLine = loadParsedCommandLine(); | ||
let fileNames = parsedCommandLine.fileNames.map(path => path.replace(windowsPathReg, '/')); | ||
function baseCreate(ts, getCommandLine, checkerOptions, rootPath, globalComponentName) { | ||
let commandLine = getCommandLine(); | ||
let fileNames = commandLine.fileNames.map(path => path.replace(windowsPathReg, '/')); | ||
let projectVersion = 0; | ||
const scriptSnapshots = new Map(); | ||
const projectHost = { | ||
getCurrentDirectory: () => rootPath, | ||
getProjectVersion: () => projectVersion.toString(), | ||
getCompilationSettings: () => parsedCommandLine.options, | ||
getCompilationSettings: () => commandLine.options, | ||
getScriptFileNames: () => fileNames, | ||
getProjectReferences: () => parsedCommandLine.projectReferences, | ||
getScriptSnapshot: fileName => { | ||
if (!scriptSnapshots.has(fileName)) { | ||
const fileText = ts.sys.readFile(fileName); | ||
if (fileText !== undefined) { | ||
scriptSnapshots.set(fileName, ts.ScriptSnapshot.fromString(fileText)); | ||
} | ||
} | ||
return scriptSnapshots.get(fileName); | ||
}, | ||
getProjectReferences: () => commandLine.projectReferences, | ||
}; | ||
return { | ||
...baseCreate(ts, projectHost, parsedCommandLine.vueOptions, checkerOptions, globalComponentName), | ||
updateFile(fileName, text) { | ||
fileName = fileName.replace(windowsPathReg, '/'); | ||
scriptSnapshots.set(fileName, ts.ScriptSnapshot.fromString(text)); | ||
projectVersion++; | ||
}, | ||
deleteFile(fileName) { | ||
fileName = fileName.replace(windowsPathReg, '/'); | ||
fileNames = fileNames.filter(f => f !== fileName); | ||
projectVersion++; | ||
}, | ||
reload() { | ||
parsedCommandLine = loadParsedCommandLine(); | ||
fileNames = parsedCommandLine.fileNames.map(path => path.replace(windowsPathReg, '/')); | ||
this.clearCache(); | ||
}, | ||
clearCache() { | ||
scriptSnapshots.clear(); | ||
projectVersion++; | ||
}, | ||
}; | ||
} | ||
function baseCreate(ts, projectHost, vueCompilerOptions, checkerOptions, globalComponentName) { | ||
const globalComponentSnapshot = ts.ScriptSnapshot.fromString('<script setup lang="ts"></script>'); | ||
const metaSnapshots = {}; | ||
const scriptSnapshots = new Map(); | ||
const metaSnapshots = new Map(); | ||
const getScriptFileNames = projectHost.getScriptFileNames; | ||
const getScriptSnapshot = projectHost.getScriptSnapshot; | ||
projectHost.getScriptFileNames = () => { | ||
@@ -99,17 +62,3 @@ const names = getScriptFileNames(); | ||
}; | ||
projectHost.getScriptSnapshot = fileName => { | ||
if (isMetaFileName(fileName)) { | ||
if (!metaSnapshots[fileName]) { | ||
metaSnapshots[fileName] = ts.ScriptSnapshot.fromString(getMetaScriptContent(fileName)); | ||
} | ||
return metaSnapshots[fileName]; | ||
} | ||
else if (fileName === globalComponentName) { | ||
return globalComponentSnapshot; | ||
} | ||
else { | ||
return getScriptSnapshot(fileName); | ||
} | ||
}; | ||
const vueLanguagePlugin = vue.createVueLanguagePlugin2(ts, id => id, vue.createRootFileChecker(projectHost.getProjectVersion ? () => projectHost.getProjectVersion() : undefined, () => projectHost.getScriptFileNames(), ts.sys.useCaseSensitiveFileNames), projectHost.getCompilationSettings(), vueCompilerOptions); | ||
const vueLanguagePlugin = vue.createVueLanguagePlugin(ts, projectHost.getCompilationSettings(), commandLine.vueOptions, id => id); | ||
const language = vue.createLanguage([ | ||
@@ -123,3 +72,24 @@ vueLanguagePlugin, | ||
], new vue.FileMap(ts.sys.useCaseSensitiveFileNames), fileName => { | ||
const snapshot = projectHost.getScriptSnapshot(fileName); | ||
let snapshot = scriptSnapshots.get(fileName); | ||
if (fileName === globalComponentName) { | ||
snapshot = globalComponentSnapshot; | ||
} | ||
else if (isMetaFileName(fileName)) { | ||
if (!metaSnapshots.has(fileName)) { | ||
metaSnapshots.set(fileName, ts.ScriptSnapshot.fromString(getMetaScriptContent(fileName))); | ||
} | ||
snapshot = metaSnapshots.get(fileName); | ||
} | ||
else { | ||
if (!scriptSnapshots.has(fileName)) { | ||
const fileText = ts.sys.readFile(fileName); | ||
if (fileText !== undefined) { | ||
scriptSnapshots.set(fileName, ts.ScriptSnapshot.fromString(fileText)); | ||
} | ||
else { | ||
scriptSnapshots.set(fileName, undefined); | ||
} | ||
} | ||
snapshot = scriptSnapshots.get(fileName); | ||
} | ||
if (snapshot) { | ||
@@ -134,2 +104,26 @@ language.scripts.set(fileName, snapshot); | ||
const tsLs = ts.createLanguageService(languageServiceHost); | ||
const fileExists = languageServiceHost.fileExists.bind(languageServiceHost); | ||
const getScriptSnapshot = languageServiceHost.getScriptSnapshot.bind(languageServiceHost); | ||
const globalTypesName = `__globalTypes_${commandLine.vueOptions.target}_${commandLine.vueOptions.strictTemplates}.d.ts`; | ||
const snapshots = new Map(); | ||
languageServiceHost.fileExists = path => { | ||
if (path.endsWith(globalTypesName)) { | ||
return true; | ||
} | ||
return fileExists(path); | ||
}; | ||
languageServiceHost.getScriptSnapshot = path => { | ||
if (path.endsWith(globalTypesName)) { | ||
if (!snapshots.has(path)) { | ||
const contents = vue.generateGlobalTypes(commandLine.vueOptions.lib, commandLine.vueOptions.target, commandLine.vueOptions.strictTemplates); | ||
snapshots.set(path, { | ||
getText: (start, end) => contents.substring(start, end), | ||
getLength: () => contents.length, | ||
getChangeRange: () => undefined, | ||
}); | ||
} | ||
return snapshots.get(path); | ||
} | ||
return getScriptSnapshot(path); | ||
}; | ||
if (checkerOptions.forceUseTs) { | ||
@@ -139,3 +133,3 @@ const getScriptKind = languageServiceHost.getScriptKind?.bind(languageServiceHost); | ||
const scriptKind = getScriptKind(fileName); | ||
if (vueCompilerOptions.extensions.some(ext => fileName.endsWith(ext))) { | ||
if (commandLine.vueOptions.extensions.some(ext => fileName.endsWith(ext))) { | ||
if (scriptKind === ts.ScriptKind.JS) { | ||
@@ -155,2 +149,21 @@ return ts.ScriptKind.TS; | ||
getComponentMeta, | ||
updateFile(fileName, text) { | ||
fileName = fileName.replace(windowsPathReg, '/'); | ||
scriptSnapshots.set(fileName, ts.ScriptSnapshot.fromString(text)); | ||
projectVersion++; | ||
}, | ||
deleteFile(fileName) { | ||
fileName = fileName.replace(windowsPathReg, '/'); | ||
fileNames = fileNames.filter(f => f !== fileName); | ||
projectVersion++; | ||
}, | ||
reload() { | ||
commandLine = getCommandLine(); | ||
fileNames = commandLine.fileNames.map(path => path.replace(windowsPathReg, '/')); | ||
this.clearCache(); | ||
}, | ||
clearCache() { | ||
scriptSnapshots.clear(); | ||
projectVersion++; | ||
}, | ||
__internal__: { | ||
@@ -164,3 +177,3 @@ tsLs, | ||
function getMetaFileName(fileName) { | ||
return (vueCompilerOptions.extensions.some(ext => fileName.endsWith(ext)) | ||
return (commandLine.vueOptions.extensions.some(ext => fileName.endsWith(ext)) | ||
? fileName | ||
@@ -182,3 +195,3 @@ : fileName.substring(0, fileName.lastIndexOf('.'))) + '.meta.ts'; | ||
${vueCompilerOptions.target < 3 ? vue2_1.code : vue_component_type_helpers_1.code} | ||
${commandLine.vueOptions.target < 3 ? vue2_1.code : vue_component_type_helpers_1.code} | ||
`.trim(); | ||
@@ -255,6 +268,6 @@ return code; | ||
const printer = ts.createPrinter(checkerOptions.printer); | ||
const snapshot = projectHost.getScriptSnapshot(componentPath); | ||
const snapshot = language.scripts.get(componentPath)?.snapshot; | ||
const vueFile = language.scripts.get(componentPath)?.generated?.root; | ||
const vueDefaults = vueFile && exportName === 'default' | ||
? (vueFile instanceof vue.VueVirtualCode ? readVueComponentDefaultProps(vueFile, printer, ts, vueCompilerOptions) : {}) | ||
? (vueFile instanceof vue.VueVirtualCode ? readVueComponentDefaultProps(vueFile, printer, ts, commandLine.vueOptions) : {}) | ||
: {}; | ||
@@ -261,0 +274,0 @@ const tsDefaults = !vueFile ? readTsComponentDefaultProps(componentPath.substring(componentPath.lastIndexOf('.') + 1), // ts | js | tsx | jsx |
{ | ||
"name": "vue-component-meta", | ||
"version": "2.0.29", | ||
"version": "2.1.0", | ||
"license": "MIT", | ||
@@ -15,6 +15,6 @@ "files": [ | ||
"dependencies": { | ||
"@volar/typescript": "~2.4.0-alpha.18", | ||
"@vue/language-core": "2.0.29", | ||
"@volar/typescript": "~2.4.1", | ||
"@vue/language-core": "2.1.0", | ||
"path-browserify": "^1.0.1", | ||
"vue-component-type-helpers": "2.0.29" | ||
"vue-component-type-helpers": "2.1.0" | ||
}, | ||
@@ -33,3 +33,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "49ad9563e6f2677595878a000179dfea83fb910c" | ||
"gitHead": "510063740b90b64caedaee1f0bde70974613a92c" | ||
} |
43364
898
+ Added@vue/language-core@2.1.0(transitive)
+ Addedvue-component-type-helpers@2.1.0(transitive)
- Removed@vue/language-core@2.0.29(transitive)
- Removedvue-component-type-helpers@2.0.29(transitive)
Updated@volar/typescript@~2.4.1
Updated@vue/language-core@2.1.0