Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can load tsconfig from subfolders #349

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion server/src/modes/script/serviceHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as path from 'path';
import * as ts from 'typescript';
import Uri from 'vscode-uri';
import { TextDocument } from 'vscode-languageserver-types';
import { dirname } from 'path';

import { LanguageModelCache } from '../languageModelCache';
import { createUpdater, parseVue, isVue, getFileFsPath, getFilePath } from './preprocess';
Expand Down Expand Up @@ -81,6 +82,7 @@ export function getServiceHost(workspacePath: string, jsDocuments: LanguageModel
const files = parsedConfig.fileNames;
compilerOptions = parsedConfig.options;
compilerOptions.allowNonTsExtensions = true;
let lastFile: string;

function updateCurrentTextDocument (doc: TextDocument) {
const fileFsPath = getFileFsPath(doc.uri);
Expand Down Expand Up @@ -109,7 +111,9 @@ export function getServiceHost(workspacePath: string, jsDocuments: LanguageModel
}

const host: ts.LanguageServiceHost = {
getCompilationSettings: () => compilerOptions,
getCompilationSettings: () => {
return getSettingsForFile(workspacePath, lastFile);
},
getScriptFileNames: () => files,
getScriptVersion (fileName) {
if (fileName === bridge.fileName) {
Expand All @@ -120,6 +124,7 @@ export function getServiceHost(workspacePath: string, jsDocuments: LanguageModel
return version ? version.toString() : '0';
},
getScriptKind (fileName) {
lastFile = fileName;
if (isVue(fileName)) {
const uri = Uri.file(fileName);
fileName = uri.fsPath;
Expand Down Expand Up @@ -211,3 +216,40 @@ function getNormalizedFileFsPath (fileName: string): string {
return Uri.file(fileName).fsPath;
}

function getSettingsForFile (workspacePath: string, fileName: string): ts.CompilerOptions {
let configJson: string;
const path = dirname(fileName);
let configFilename;

configFilename = ts.findConfigFile(path, ts.sys.fileExists, 'tsconfig.json') ||
ts.findConfigFile(path, ts.sys.fileExists, 'jsconfig.json');

configJson = configFilename && ts.readConfigFile(configFilename, ts.sys.readFile).config || {
exclude: ['node_modules', '**/node_modules/*']
};

let compilerOptions: ts.CompilerOptions = {
allowNonTsExtensions: true,
allowJs: true,
lib: ['lib.dom.d.ts', 'lib.es2017.d.ts'],
target: ts.ScriptTarget.Latest,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
module: ts.ModuleKind.CommonJS,
jsx: ts.JsxEmit.Preserve,
allowSyntheticDefaultImports: true
};

let parsedConfig: ts.ParsedCommandLine;
parsedConfig = ts.parseJsonConfigFileContent(configJson,
ts.sys,
workspacePath,
compilerOptions,
configFilename,
undefined,
[{ extension: 'vue', isMixedContent: true }]);

compilerOptions = parsedConfig.options;
compilerOptions.allowNonTsExtensions = true;

return compilerOptions;
}