|
| 1 | +import fs = require('fs'); |
| 2 | +import path = require('path'); |
| 3 | +import ts = require('typescript'); |
| 4 | +import FilesRegister = require('./FilesRegister'); |
| 5 | +import FilesWatcher = require('./FilesWatcher'); |
| 6 | +import vueParser = require('vue-parser'); |
| 7 | + |
| 8 | +class VueProgram { |
| 9 | + static loadProgramConfig(configFile: string) { |
| 10 | + const extraExtensions = ['vue']; |
| 11 | + |
| 12 | + const parseConfigHost: ts.ParseConfigHost = { |
| 13 | + fileExists: ts.sys.fileExists, |
| 14 | + readFile: ts.sys.readFile, |
| 15 | + useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames, |
| 16 | + readDirectory: (rootDir, extensions, excludes, includes, depth) => { |
| 17 | + return ts.sys.readDirectory(rootDir, extensions.concat(extraExtensions), excludes, includes, depth); |
| 18 | + } |
| 19 | + }; |
| 20 | + |
| 21 | + const parsed = ts.parseJsonConfigFileContent( |
| 22 | + // Regardless of the setting in the tsconfig.json we want isolatedModules to be false |
| 23 | + Object.assign(ts.readConfigFile(configFile, ts.sys.readFile).config, { isolatedModules: false }), |
| 24 | + parseConfigHost, |
| 25 | + path.dirname(configFile) |
| 26 | + ); |
| 27 | + |
| 28 | + parsed.options.allowNonTsExtensions = true; |
| 29 | + |
| 30 | + return parsed; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Since 99.9% of Vue projects use the wildcard '@/*', we only search for that in tsconfig CompilerOptions.paths. |
| 35 | + * The path is resolved with thie given substitution and includes the CompilerOptions.baseUrl (if given). |
| 36 | + * If no paths given in tsconfig, then the default substitution is '[tsconfig directory]/src'. |
| 37 | + * (This is a fast, simplified inspiration of what's described here: https://github.com/Microsoft/TypeScript/issues/5039) |
| 38 | + */ |
| 39 | + public static resolveNonTsModuleName(moduleName: string, containingFile: string, basedir: string, options: ts.CompilerOptions) { |
| 40 | + const baseUrl = options.baseUrl ? options.baseUrl : basedir; |
| 41 | + const pattern = options.paths ? options.paths['@/*'] : undefined; |
| 42 | + const substitution = pattern ? options.paths['@/*'][0].replace('*', '') : 'src'; |
| 43 | + const isWildcard = moduleName.substr(0, 2) === '@/'; |
| 44 | + const isRelative = !path.isAbsolute(moduleName); |
| 45 | + |
| 46 | + if (isWildcard) { |
| 47 | + moduleName = path.resolve(baseUrl, substitution, moduleName.substr(2)); |
| 48 | + } else if (isRelative) { |
| 49 | + moduleName = path.resolve(path.dirname(containingFile), moduleName); |
| 50 | + } |
| 51 | + |
| 52 | + return moduleName; |
| 53 | + } |
| 54 | + |
| 55 | + public static isVue(filePath: string) { |
| 56 | + return path.extname(filePath) === '.vue'; |
| 57 | + } |
| 58 | + |
| 59 | + static createProgram( |
| 60 | + programConfig: ts.ParsedCommandLine, |
| 61 | + basedir: string, |
| 62 | + files: FilesRegister, |
| 63 | + watcher: FilesWatcher, |
| 64 | + oldProgram: ts.Program |
| 65 | + ) { |
| 66 | + const host = ts.createCompilerHost(programConfig.options); |
| 67 | + const realGetSourceFile = host.getSourceFile; |
| 68 | + |
| 69 | + // We need a host that can parse Vue SFCs (single file components). |
| 70 | + host.getSourceFile = (filePath, languageVersion, onError) => { |
| 71 | + // first check if watcher is watching file - if not - check it's mtime |
| 72 | + if (!watcher.isWatchingFile(filePath)) { |
| 73 | + try { |
| 74 | + const stats = fs.statSync(filePath); |
| 75 | + |
| 76 | + files.setMtime(filePath, stats.mtime.valueOf()); |
| 77 | + } catch (e) { |
| 78 | + // probably file does not exists |
| 79 | + files.remove(filePath); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // get source file only if there is no source in files register |
| 84 | + if (!files.has(filePath) || !files.getData(filePath).source) { |
| 85 | + files.mutateData(filePath, (data) => { |
| 86 | + data.source = realGetSourceFile(filePath, languageVersion, onError); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + let source = files.getData(filePath).source; |
| 91 | + |
| 92 | + // get typescript contents from Vue file |
| 93 | + if (source && VueProgram.isVue(filePath)) { |
| 94 | + const parsed = vueParser.parse(source.text, 'script', { lang: ['ts', 'tsx', 'js', 'jsx'] }); |
| 95 | + source = ts.createSourceFile(filePath, parsed, languageVersion, true); |
| 96 | + } |
| 97 | + |
| 98 | + return source; |
| 99 | + }; |
| 100 | + |
| 101 | + // We need a host with special module resolution for Vue files. |
| 102 | + host.resolveModuleNames = (moduleNames, containingFile) => { |
| 103 | + const resolvedModules: ts.ResolvedModule[] = []; |
| 104 | + |
| 105 | + for (const moduleName of moduleNames) { |
| 106 | + // Try to use standard resolution. |
| 107 | + const result = ts.resolveModuleName(moduleName, containingFile, programConfig.options, { |
| 108 | + fileExists: host.fileExists, |
| 109 | + readFile: host.readFile |
| 110 | + }); |
| 111 | + |
| 112 | + if (result.resolvedModule) { |
| 113 | + resolvedModules.push(result.resolvedModule); |
| 114 | + } else { |
| 115 | + // For non-ts extensions. |
| 116 | + const absolutePath = VueProgram.resolveNonTsModuleName(moduleName, containingFile, basedir, programConfig.options); |
| 117 | + |
| 118 | + if (VueProgram.isVue(moduleName)) { |
| 119 | + resolvedModules.push({ |
| 120 | + resolvedFileName: absolutePath, |
| 121 | + extension: '.ts' |
| 122 | + } as ts.ResolvedModuleFull); |
| 123 | + } else { |
| 124 | + resolvedModules.push({ |
| 125 | + // If the file does exist, return an empty string (because we assume user has provided a ".d.ts" file for it). |
| 126 | + resolvedFileName: host.fileExists(absolutePath) ? '' : absolutePath, |
| 127 | + extension: '.ts' |
| 128 | + } as ts.ResolvedModuleFull); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return resolvedModules; |
| 134 | + }; |
| 135 | + |
| 136 | + return ts.createProgram( |
| 137 | + programConfig.fileNames, |
| 138 | + programConfig.options, |
| 139 | + host, |
| 140 | + oldProgram // re-use old program |
| 141 | + ); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +export = VueProgram; |
0 commit comments