-
-
Notifications
You must be signed in to change notification settings - Fork 243
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
Adds .vue functionality #77
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cda4341
add vue functionality, add tests
prograhammer 7ae27a8
remove unnecessary import
prograhammer c0340ed
remove extra spacing
prograhammer b3cfe27
remove some debugging
prograhammer 45abd0e
restore original travis key
prograhammer 39c9012
fix parsing bug found after modifying non ts-vue files
prograhammer 1f8ec52
Merge branch 'next' into feature/vue
piotr-oles c09b16d
fix import issue, update tests, update README
prograhammer 946eb24
Merge branch 'feature/vue' of github.com:prograhammer/fork-ts-checker…
prograhammer 8776b9f
trigger Travis to try again
prograhammer 57f2320
Merge branch 'master' into feature/vue
piotr-oles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import fs = require('fs'); | ||
import path = require('path'); | ||
import ts = require('typescript'); | ||
import FilesRegister = require('./FilesRegister'); | ||
import FilesWatcher = require('./FilesWatcher'); | ||
import vueParser = require('vue-parser'); | ||
|
||
class VueProgram { | ||
static loadProgramConfig(configFile: string) { | ||
const extraExtensions = ['vue']; | ||
|
||
const parseConfigHost: ts.ParseConfigHost = { | ||
fileExists: ts.sys.fileExists, | ||
readFile: ts.sys.readFile, | ||
useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames, | ||
readDirectory: (rootDir, extensions, excludes, includes, depth) => { | ||
return ts.sys.readDirectory(rootDir, extensions.concat(extraExtensions), excludes, includes, depth); | ||
} | ||
}; | ||
|
||
const parsed = ts.parseJsonConfigFileContent( | ||
// Regardless of the setting in the tsconfig.json we want isolatedModules to be false | ||
Object.assign(ts.readConfigFile(configFile, ts.sys.readFile).config, { isolatedModules: false }), | ||
parseConfigHost, | ||
path.dirname(configFile) | ||
); | ||
|
||
parsed.options.allowNonTsExtensions = true; | ||
|
||
return parsed; | ||
} | ||
|
||
/** | ||
* Since 99.9% of Vue projects use the wildcard '@/*', we only search for that in tsconfig CompilerOptions.paths. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, add information about vue support and how it resolves "@/*" paths in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
* The path is resolved with thie given substitution and includes the CompilerOptions.baseUrl (if given). | ||
* If no paths given in tsconfig, then the default substitution is '[tsconfig directory]/src'. | ||
* (This is a fast, simplified inspiration of what's described here: https://github.com/Microsoft/TypeScript/issues/5039) | ||
*/ | ||
public static resolveNonTsModuleName(moduleName: string, containingFile: string, basedir: string, options: ts.CompilerOptions) { | ||
const baseUrl = options.baseUrl ? options.baseUrl : basedir; | ||
const pattern = options.paths ? options.paths['@/*'] : undefined; | ||
const substitution = pattern ? options.paths['@/*'][0].replace('*', '') : 'src'; | ||
const isWildcard = moduleName.substr(0, 2) === '@/'; | ||
const isRelative = !path.isAbsolute(moduleName); | ||
|
||
if (isWildcard) { | ||
moduleName = path.resolve(baseUrl, substitution, moduleName.substr(2)); | ||
} else if (isRelative) { | ||
moduleName = path.resolve(path.dirname(containingFile), moduleName); | ||
} | ||
|
||
return moduleName; | ||
} | ||
|
||
public static isVue(filePath: string) { | ||
return path.extname(filePath) === '.vue'; | ||
} | ||
|
||
static createProgram( | ||
programConfig: ts.ParsedCommandLine, | ||
basedir: string, | ||
files: FilesRegister, | ||
watcher: FilesWatcher, | ||
oldProgram: ts.Program | ||
) { | ||
const host = ts.createCompilerHost(programConfig.options); | ||
const realGetSourceFile = host.getSourceFile; | ||
|
||
// We need a host that can parse Vue SFCs (single file components). | ||
host.getSourceFile = (filePath, languageVersion, onError) => { | ||
// first check if watcher is watching file - if not - check it's mtime | ||
if (!watcher.isWatchingFile(filePath)) { | ||
try { | ||
const stats = fs.statSync(filePath); | ||
|
||
files.setMtime(filePath, stats.mtime.valueOf()); | ||
} catch (e) { | ||
// probably file does not exists | ||
files.remove(filePath); | ||
} | ||
} | ||
|
||
// get source file only if there is no source in files register | ||
if (!files.has(filePath) || !files.getData(filePath).source) { | ||
files.mutateData(filePath, (data) => { | ||
data.source = realGetSourceFile(filePath, languageVersion, onError); | ||
}); | ||
} | ||
|
||
let source = files.getData(filePath).source; | ||
|
||
// get typescript contents from Vue file | ||
if (source && VueProgram.isVue(filePath)) { | ||
const parsed = vueParser.parse(source.text, 'script', { lang: ['ts', 'tsx', 'js', 'jsx'] }); | ||
source = ts.createSourceFile(filePath, parsed, languageVersion, true); | ||
} | ||
|
||
return source; | ||
}; | ||
|
||
// We need a host with special module resolution for Vue files. | ||
host.resolveModuleNames = (moduleNames, containingFile) => { | ||
const resolvedModules: ts.ResolvedModule[] = []; | ||
|
||
for (const moduleName of moduleNames) { | ||
// Try to use standard resolution. | ||
const result = ts.resolveModuleName(moduleName, containingFile, programConfig.options, { | ||
fileExists: host.fileExists, | ||
readFile: host.readFile | ||
}); | ||
|
||
if (result.resolvedModule) { | ||
resolvedModules.push(result.resolvedModule); | ||
} else { | ||
// For non-ts extensions. | ||
const absolutePath = VueProgram.resolveNonTsModuleName(moduleName, containingFile, basedir, programConfig.options); | ||
|
||
if (VueProgram.isVue(moduleName)) { | ||
resolvedModules.push({ | ||
resolvedFileName: absolutePath, | ||
extension: '.ts' | ||
} as ts.ResolvedModuleFull); | ||
} else { | ||
resolvedModules.push({ | ||
// If the file does exist, return an empty string (because we assume user has provided a ".d.ts" file for it). | ||
resolvedFileName: host.fileExists(absolutePath) ? '' : absolutePath, | ||
extension: '.ts' | ||
} as ts.ResolvedModuleFull); | ||
} | ||
} | ||
} | ||
|
||
return resolvedModules; | ||
}; | ||
|
||
return ts.createProgram( | ||
programConfig.fileNames, | ||
programConfig.options, | ||
host, | ||
oldProgram // re-use old program | ||
); | ||
} | ||
} | ||
|
||
export = VueProgram; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need css-loader here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah,
css-loader
is for the Vue integration test. Since Vue uses single-file-components, I wanted to include a simple css section in the example files just to ensure everything is working. Also, I added animport exampleCss from "./example.css"
to ensure other "non-vue" imports work (as reported by @aweiu @CKGrafico @jvbianchi).