Skip to content

Commit 335eca3

Browse files
committed
refactor(language-core): reimplement writeGlobalTypes without side effects
1 parent 7f65ebb commit 335eca3

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

packages/component-meta/lib/base.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,21 @@ function baseCreate(
9393
rootPath: string,
9494
globalComponentName: string,
9595
) {
96-
let [commandLine, _fileNames] = getConfigAndFiles();
96+
let [{ vueOptions, options, projectReferences }, fileNames] = getConfigAndFiles();
9797
/**
9898
* Used to lookup if a file is referenced.
9999
*/
100-
let fileNames = new Set(_fileNames.map(path => path.replace(windowsPathReg, '/')));
100+
let fileNamesSet = new Set(fileNames.map(path => path.replace(windowsPathReg, '/')));
101101
let projectVersion = 0;
102102

103-
vue.writeGlobalTypes(commandLine.vueOptions, ts.sys.writeFile);
103+
vueOptions.globalTypesPath = vue.createGlobalTypesWriter(vueOptions, ts.sys.writeFile);
104104

105105
const projectHost: TypeScriptProjectHost = {
106106
getCurrentDirectory: () => rootPath,
107107
getProjectVersion: () => projectVersion.toString(),
108-
getCompilationSettings: () => commandLine.options,
109-
getScriptFileNames: () => [...fileNames],
110-
getProjectReferences: () => commandLine.projectReferences,
108+
getCompilationSettings: () => options,
109+
getScriptFileNames: () => [...fileNamesSet],
110+
getProjectReferences: () => projectReferences,
111111
};
112112
const globalComponentSnapshot = ts.ScriptSnapshot.fromString('<script setup lang="ts"></script>');
113113
const scriptSnapshots = new Map<string, ts.IScriptSnapshot | undefined>();
@@ -126,7 +126,7 @@ function baseCreate(
126126
const vueLanguagePlugin = vue.createVueLanguagePlugin<string>(
127127
ts,
128128
projectHost.getCompilationSettings(),
129-
commandLine.vueOptions,
129+
vueOptions,
130130
id => id,
131131
);
132132
const language = vue.createLanguage(
@@ -179,7 +179,7 @@ function baseCreate(
179179
const getScriptKind = languageServiceHost.getScriptKind?.bind(languageServiceHost);
180180
languageServiceHost.getScriptKind = fileName => {
181181
const scriptKind = getScriptKind!(fileName);
182-
if (commandLine.vueOptions.extensions.some(ext => fileName.endsWith(ext))) {
182+
if (vueOptions.extensions.some(ext => fileName.endsWith(ext))) {
183183
if (scriptKind === ts.ScriptKind.JS) {
184184
return ts.ScriptKind.TS;
185185
}
@@ -200,17 +200,18 @@ function baseCreate(
200200
fileName = fileName.replace(windowsPathReg, '/');
201201
scriptSnapshots.set(fileName, ts.ScriptSnapshot.fromString(text));
202202
// Ensure the file is referenced
203-
fileNames.add(fileName);
203+
fileNamesSet.add(fileName);
204204
projectVersion++;
205205
},
206206
deleteFile(fileName: string) {
207207
fileName = fileName.replace(windowsPathReg, '/');
208-
fileNames.delete(fileName);
208+
fileNamesSet.delete(fileName);
209209
projectVersion++;
210210
},
211211
reload() {
212-
[commandLine, _fileNames] = getConfigAndFiles();
213-
fileNames = new Set(_fileNames.map(path => path.replace(windowsPathReg, '/')));
212+
[{ vueOptions, options, projectReferences }, fileNames] = getConfigAndFiles();
213+
vueOptions.globalTypesPath = vue.createGlobalTypesWriter(vueOptions, ts.sys.writeFile);
214+
fileNamesSet = new Set(fileNames.map(path => path.replace(windowsPathReg, '/')));
214215
this.clearCache();
215216
},
216217
clearCache() {
@@ -228,7 +229,7 @@ function baseCreate(
228229

229230
function getMetaFileName(fileName: string) {
230231
return (
231-
commandLine.vueOptions.extensions.some(ext => fileName.endsWith(ext))
232+
vueOptions.extensions.some(ext => fileName.endsWith(ext))
232233
? fileName
233234
: fileName.slice(0, fileName.lastIndexOf('.'))
234235
) + '.meta.ts';

packages/language-core/lib/compilerOptions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,13 @@ export function getDefaultCompilerOptions(target = 99, lib = 'vue', strictTempla
323323
};
324324
}
325325

326-
export function writeGlobalTypes(
326+
export function createGlobalTypesWriter(
327327
vueOptions: VueCompilerOptions,
328328
writeFile: (fileName: string, data: string) => void,
329329
) {
330330
const writed = new Set<string>();
331331
const { globalTypesPath } = vueOptions;
332-
vueOptions.globalTypesPath = (fileName: string) => {
332+
return (fileName: string) => {
333333
const result = globalTypesPath(fileName);
334334
if (result && !writed.has(result)) {
335335
writed.add(result);

packages/tsc/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function run(tscPath = require.resolve('typescript/lib/tsc')) {
1717
? vue.createParsedCommandLine(ts, ts.sys, configFilePath.replace(windowsPathReg, '/')).vueOptions
1818
: vue.createParsedCommandLineByJson(ts, ts.sys, (options.host ?? ts.sys).getCurrentDirectory(), {})
1919
.vueOptions;
20-
vue.writeGlobalTypes(vueOptions, ts.sys.writeFile);
20+
vueOptions.globalTypesPath = vue.createGlobalTypesWriter(vueOptions, ts.sys.writeFile);
2121
const allExtensions = vue.getAllExtensions(vueOptions);
2222
if (
2323
runExtensions.length === allExtensions.length

packages/tsc/tests/dts.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('vue-tsc-dts', () => {
3636
vueOptions.target = 99;
3737
vueOptions.extensions = ['vue', 'cext'];
3838
}
39-
vue.writeGlobalTypes(vueOptions, ts.sys.writeFile);
39+
vueOptions.globalTypesPath = vue.createGlobalTypesWriter(vueOptions, ts.sys.writeFile);
4040
const vueLanguagePlugin = vue.createVueLanguagePlugin<string>(
4141
ts,
4242
options.options,

packages/typescript-plugin/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ export = createLanguageServicePlugin(
2323
vueOptions,
2424
id => id,
2525
);
26-
27-
vue.writeGlobalTypes(vueOptions, ts.sys.writeFile);
26+
vueOptions.globalTypesPath = vue.createGlobalTypesWriter(vueOptions, ts.sys.writeFile);
2827
addVueCommands();
2928

3029
return {

0 commit comments

Comments
 (0)