diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
commit | de98e0b232509d5f40c135d540a70e415272ff85 (patch) | |
tree | a79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/ts-loader/dist/instances.js | |
parent | e0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff) |
node_modules
Diffstat (limited to 'node_modules/ts-loader/dist/instances.js')
-rw-r--r-- | node_modules/ts-loader/dist/instances.js | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/node_modules/ts-loader/dist/instances.js b/node_modules/ts-loader/dist/instances.js new file mode 100644 index 000000000..fc9c13cc5 --- /dev/null +++ b/node_modules/ts-loader/dist/instances.js @@ -0,0 +1,87 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var path = require("path"); +var fs = require("fs"); +require('colors'); +var afterCompile = require("./after-compile"); +var config = require("./config"); +var compilerSetup = require("./compilerSetup"); +var utils = require("./utils"); +var logger = require("./logger"); +var makeServicesHost = require("./servicesHost"); +var watchRun = require("./watch-run"); +var instances = {}; +/** + * The loader is executed once for each file seen by webpack. However, we need to keep + * a persistent instance of TypeScript that contains all of the files in the program + * along with definition files and options. This function either creates an instance + * or returns the existing one. Multiple instances are possible by using the + * `instance` property. + */ +function getTypeScriptInstance(loaderOptions, loader) { + if (utils.hasOwnProperty(instances, loaderOptions.instance)) { + return { instance: instances[loaderOptions.instance] }; + } + var log = logger.makeLogger(loaderOptions); + var _a = compilerSetup.getCompiler(loaderOptions, log), compiler = _a.compiler, compilerCompatible = _a.compilerCompatible, compilerDetailsLogMessage = _a.compilerDetailsLogMessage, errorMessage = _a.errorMessage; + if (errorMessage) { + return { error: utils.makeError({ rawMessage: errorMessage }) }; + } + var _b = config.getConfigFile(compiler, loader, loaderOptions, compilerCompatible, log, compilerDetailsLogMessage), configFilePath = _b.configFilePath, configFile = _b.configFile, configFileError = _b.configFileError; + if (configFileError) { + return { error: configFileError }; + } + var configParseResult = config.getConfigParseResult(compiler, configFile, configFilePath); + if (configParseResult.errors.length) { + utils.registerWebpackErrors(loader._module.errors, utils.formatErrors(configParseResult.errors, loaderOptions, compiler, { file: configFilePath })); + return { error: utils.makeError({ rawMessage: 'error while parsing tsconfig.json', file: configFilePath }) }; + } + var compilerOptions = compilerSetup.getCompilerOptions(compilerCompatible, compiler, configParseResult); + var files = {}; + if (loaderOptions.transpileOnly) { + // quick return for transpiling + // we do need to check for any issues with TS options though + var program = compiler.createProgram([], compilerOptions); + var diagnostics = program.getOptionsDiagnostics(); + utils.registerWebpackErrors(loader._module.errors, utils.formatErrors(diagnostics, loaderOptions, compiler, { file: configFilePath || 'tsconfig.json' })); + return { instance: instances[loaderOptions.instance] = { compiler: compiler, compilerOptions: compilerOptions, loaderOptions: loaderOptions, files: files, dependencyGraph: {}, reverseDependencyGraph: {} } }; + } + // Load initial files (core lib files, any files specified in tsconfig.json) + var normalizedFilePath; + try { + var filesToLoad = configParseResult.fileNames; + filesToLoad.forEach(function (filePath) { + normalizedFilePath = path.normalize(filePath); + files[normalizedFilePath] = { + text: fs.readFileSync(normalizedFilePath, 'utf-8'), + version: 0 + }; + }); + } + catch (exc) { + return { error: utils.makeError({ + rawMessage: "A file specified in tsconfig.json could not be found: " + normalizedFilePath + }) }; + } + // if allowJs is set then we should accept js(x) files + var scriptRegex = configParseResult.options.allowJs && loaderOptions.entryFileIsJs + ? /\.tsx?$|\.jsx?$/i + : /\.tsx?$/i; + var instance = instances[loaderOptions.instance] = { + compiler: compiler, + compilerOptions: compilerOptions, + loaderOptions: loaderOptions, + files: files, + languageService: null, + version: 0, + dependencyGraph: {}, + reverseDependencyGraph: {}, + modifiedFiles: null, + }; + var servicesHost = makeServicesHost(scriptRegex, log, loader, instance, loaderOptions.appendTsSuffixTo); + instance.languageService = compiler.createLanguageService(servicesHost, compiler.createDocumentRegistry()); + loader._compiler.plugin("after-compile", afterCompile(instance, configFilePath)); + loader._compiler.plugin("watch-run", watchRun(instance)); + return { instance: instance }; +} +exports.getTypeScriptInstance = getTypeScriptInstance; |