From b948b49d7575c64bf230b4b5edd78bf54e726542 Mon Sep 17 00:00:00 2001 From: Bryan Ross Date: Mon, 29 May 2017 08:47:37 -0600 Subject: [PATCH] write out DTS file structure --- README.md | 7 +- dist/rollup-plugin-typescript2.cjs.js | 1150 +++++++++++++------------ dist/rollup-plugin-typescript2.es.js | 1150 +++++++++++++------------ src/index.ts | 14 + src/tscache.ts | 1 + 5 files changed, 1183 insertions(+), 1139 deletions(-) diff --git a/README.md b/README.md index 12fbf7b..9159c57 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,12 @@ Plugin takes following options: * `rollupCommonJSResolveHack`: false - On windows typescript resolver favors POSIX path, while commonjs plugin (and maybe others?) uses native path as module id. This can result in `namedExports` being ignored if rollup happened to use typescript's resolution. Set to true to pass resolved module path through `resolve()` to match up with `rollup-plugin-commonjs`. + On windows typescript resolver favors POSIX path, while commonjs plugin (and maybe others?) uses native path as module id. This can result in `namedExports` being ignored if rollup happened to use typescript's resolution. Set to true to pass resolved module path through `resolve()` to match up with `rollup-plugin-commonjs`. + +### Declarations + +This plugin respects `declaration: true` in your `tsconfig.json` file. When set, it will emit `*.d.ts` files for your bundle. The resulting file(s) can then be used with the `types` +property in your `package.json` file as described [here](https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html). ### Watch mode diff --git a/dist/rollup-plugin-typescript2.cjs.js b/dist/rollup-plugin-typescript2.cjs.js index 79a1dc9..3eb6b6e 100644 --- a/dist/rollup-plugin-typescript2.cjs.js +++ b/dist/rollup-plugin-typescript2.cjs.js @@ -10,587 +10,599 @@ var colors = require('colors/safe'); var path = require('path'); var resolve = require('resolve'); -/*! ***************************************************************************** -Copyright (c) Microsoft Corporation. All rights reserved. -Licensed under the Apache License, Version 2.0 (the "License"); you may not use -this file except in compliance with the License. You may obtain a copy of the -License at http://www.apache.org/licenses/LICENSE-2.0 - -THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED -WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, -MERCHANTABLITY OR NON-INFRINGEMENT. - -See the Apache Version 2.0 License for specific language governing permissions -and limitations under the License. -***************************************************************************** */ -/* global Reflect, Promise */ - - - -var __assign = Object.assign || function __assign(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; - } - return t; +/*! ***************************************************************************** +Copyright (c) Microsoft Corporation. All rights reserved. +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy of the +License at http://www.apache.org/licenses/LICENSE-2.0 + +THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +MERCHANTABLITY OR NON-INFRINGEMENT. + +See the Apache Version 2.0 License for specific language governing permissions +and limitations under the License. +***************************************************************************** */ +/* global Reflect, Promise */ + + + +var __assign = Object.assign || function __assign(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; + } + return t; }; -var VerbosityLevel; -(function (VerbosityLevel) { - VerbosityLevel[VerbosityLevel["Error"] = 0] = "Error"; - VerbosityLevel[VerbosityLevel["Warning"] = 1] = "Warning"; - VerbosityLevel[VerbosityLevel["Info"] = 2] = "Info"; - VerbosityLevel[VerbosityLevel["Debug"] = 3] = "Debug"; -})(VerbosityLevel || (VerbosityLevel = {})); -var ConsoleContext = (function () { - function ConsoleContext(verbosity, prefix) { - if (prefix === void 0) { prefix = ""; } - this.verbosity = verbosity; - this.prefix = prefix; - } - ConsoleContext.prototype.warn = function (message) { - if (this.verbosity < VerbosityLevel.Warning) - return; - console.log("" + this.prefix + message); - }; - ConsoleContext.prototype.error = function (message) { - if (this.verbosity < VerbosityLevel.Error) - return; - console.log("" + this.prefix + message); - }; - ConsoleContext.prototype.info = function (message) { - if (this.verbosity < VerbosityLevel.Info) - return; - console.log("" + this.prefix + message); - }; - ConsoleContext.prototype.debug = function (message) { - if (this.verbosity < VerbosityLevel.Debug) - return; - console.log("" + this.prefix + message); - }; - return ConsoleContext; +var VerbosityLevel; +(function (VerbosityLevel) { + VerbosityLevel[VerbosityLevel["Error"] = 0] = "Error"; + VerbosityLevel[VerbosityLevel["Warning"] = 1] = "Warning"; + VerbosityLevel[VerbosityLevel["Info"] = 2] = "Info"; + VerbosityLevel[VerbosityLevel["Debug"] = 3] = "Debug"; +})(VerbosityLevel || (VerbosityLevel = {})); +var ConsoleContext = (function () { + function ConsoleContext(verbosity, prefix) { + if (prefix === void 0) { prefix = ""; } + this.verbosity = verbosity; + this.prefix = prefix; + } + ConsoleContext.prototype.warn = function (message) { + if (this.verbosity < VerbosityLevel.Warning) + return; + console.log("" + this.prefix + message); + }; + ConsoleContext.prototype.error = function (message) { + if (this.verbosity < VerbosityLevel.Error) + return; + console.log("" + this.prefix + message); + }; + ConsoleContext.prototype.info = function (message) { + if (this.verbosity < VerbosityLevel.Info) + return; + console.log("" + this.prefix + message); + }; + ConsoleContext.prototype.debug = function (message) { + if (this.verbosity < VerbosityLevel.Debug) + return; + console.log("" + this.prefix + message); + }; + return ConsoleContext; }()); -var RollupContext = (function () { - function RollupContext(verbosity, bail, context, prefix) { - if (prefix === void 0) { prefix = ""; } - this.verbosity = verbosity; - this.bail = bail; - this.context = context; - this.prefix = prefix; - this.hasContext = true; - this.hasContext = _.isFunction(this.context.warn) && _.isFunction(this.context.error); - } - RollupContext.prototype.warn = function (message) { - if (this.verbosity < VerbosityLevel.Warning) - return; - if (this.hasContext) - this.context.warn("" + this.prefix + message); - else - console.log("" + this.prefix + message); - }; - RollupContext.prototype.error = function (message) { - if (this.verbosity < VerbosityLevel.Error) - return; - if (this.hasContext) { - if (this.bail) - this.context.error("" + this.prefix + message); - else - this.context.warn("" + this.prefix + message); - } - else - console.log("" + this.prefix + message); - }; - RollupContext.prototype.info = function (message) { - if (this.verbosity < VerbosityLevel.Info) - return; - console.log("" + this.prefix + message); - }; - RollupContext.prototype.debug = function (message) { - if (this.verbosity < VerbosityLevel.Debug) - return; - console.log("" + this.prefix + message); - }; - return RollupContext; +var RollupContext = (function () { + function RollupContext(verbosity, bail, context, prefix) { + if (prefix === void 0) { prefix = ""; } + this.verbosity = verbosity; + this.bail = bail; + this.context = context; + this.prefix = prefix; + this.hasContext = true; + this.hasContext = _.isFunction(this.context.warn) && _.isFunction(this.context.error); + } + RollupContext.prototype.warn = function (message) { + if (this.verbosity < VerbosityLevel.Warning) + return; + if (this.hasContext) + this.context.warn("" + this.prefix + message); + else + console.log("" + this.prefix + message); + }; + RollupContext.prototype.error = function (message) { + if (this.verbosity < VerbosityLevel.Error) + return; + if (this.hasContext) { + if (this.bail) + this.context.error("" + this.prefix + message); + else + this.context.warn("" + this.prefix + message); + } + else + console.log("" + this.prefix + message); + }; + RollupContext.prototype.info = function (message) { + if (this.verbosity < VerbosityLevel.Info) + return; + console.log("" + this.prefix + message); + }; + RollupContext.prototype.debug = function (message) { + if (this.verbosity < VerbosityLevel.Debug) + return; + console.log("" + this.prefix + message); + }; + return RollupContext; }()); -var LanguageServiceHost = (function () { - function LanguageServiceHost(parsedConfig) { - this.parsedConfig = parsedConfig; - this.cwd = process.cwd(); - this.snapshots = {}; - this.versions = {}; - } - LanguageServiceHost.prototype.reset = function () { - this.snapshots = {}; - this.versions = {}; - }; - LanguageServiceHost.prototype.setSnapshot = function (fileName, data) { - fileName = this.normalize(fileName); - var snapshot = ts.ScriptSnapshot.fromString(data); - this.snapshots[fileName] = snapshot; - this.versions[fileName] = (this.versions[fileName] || 0) + 1; - return snapshot; - }; - LanguageServiceHost.prototype.getScriptSnapshot = function (fileName) { - fileName = this.normalize(fileName); - if (_.has(this.snapshots, fileName)) - return this.snapshots[fileName]; - if (fs.existsSync(fileName)) { - this.snapshots[fileName] = ts.ScriptSnapshot.fromString(ts.sys.readFile(fileName)); - this.versions[fileName] = (this.versions[fileName] || 0) + 1; - return this.snapshots[fileName]; - } - return undefined; - }; - LanguageServiceHost.prototype.getCurrentDirectory = function () { - return this.cwd; - }; - LanguageServiceHost.prototype.getScriptVersion = function (fileName) { - fileName = this.normalize(fileName); - return (this.versions[fileName] || 0).toString(); - }; - LanguageServiceHost.prototype.getScriptFileNames = function () { - return this.parsedConfig.fileNames; - }; - LanguageServiceHost.prototype.getCompilationSettings = function () { - return this.parsedConfig.options; - }; - LanguageServiceHost.prototype.getDefaultLibFileName = function (opts) { - return ts.getDefaultLibFilePath(opts); - }; - LanguageServiceHost.prototype.normalize = function (fileName) { - return fileName.split("\\").join("/"); - }; - return LanguageServiceHost; +var LanguageServiceHost = (function () { + function LanguageServiceHost(parsedConfig) { + this.parsedConfig = parsedConfig; + this.cwd = process.cwd(); + this.snapshots = {}; + this.versions = {}; + } + LanguageServiceHost.prototype.reset = function () { + this.snapshots = {}; + this.versions = {}; + }; + LanguageServiceHost.prototype.setSnapshot = function (fileName, data) { + fileName = this.normalize(fileName); + var snapshot = ts.ScriptSnapshot.fromString(data); + this.snapshots[fileName] = snapshot; + this.versions[fileName] = (this.versions[fileName] || 0) + 1; + return snapshot; + }; + LanguageServiceHost.prototype.getScriptSnapshot = function (fileName) { + fileName = this.normalize(fileName); + if (_.has(this.snapshots, fileName)) + return this.snapshots[fileName]; + if (fs.existsSync(fileName)) { + this.snapshots[fileName] = ts.ScriptSnapshot.fromString(ts.sys.readFile(fileName)); + this.versions[fileName] = (this.versions[fileName] || 0) + 1; + return this.snapshots[fileName]; + } + return undefined; + }; + LanguageServiceHost.prototype.getCurrentDirectory = function () { + return this.cwd; + }; + LanguageServiceHost.prototype.getScriptVersion = function (fileName) { + fileName = this.normalize(fileName); + return (this.versions[fileName] || 0).toString(); + }; + LanguageServiceHost.prototype.getScriptFileNames = function () { + return this.parsedConfig.fileNames; + }; + LanguageServiceHost.prototype.getCompilationSettings = function () { + return this.parsedConfig.options; + }; + LanguageServiceHost.prototype.getDefaultLibFileName = function (opts) { + return ts.getDefaultLibFilePath(opts); + }; + LanguageServiceHost.prototype.normalize = function (fileName) { + return fileName.split("\\").join("/"); + }; + return LanguageServiceHost; }()); -/** - * Saves data in new cache folder or reads it from old one. - * Avoids perpetually growing cache and situations when things need to consider changed and then reverted data to be changed. - */ -var RollingCache = (function () { - /** - * @param cacheRoot: root folder for the cache - * @param checkNewCache: whether to also look in new cache when reading from cache - */ - function RollingCache(cacheRoot, checkNewCache) { - this.cacheRoot = cacheRoot; - this.checkNewCache = checkNewCache; - this.rolled = false; - this.oldCacheRoot = this.cacheRoot + "/cache"; - this.newCacheRoot = this.cacheRoot + "/cache_"; - fs.emptyDirSync(this.newCacheRoot); - } - /** - * @returns true if name exist in old cache (or either old of new cache if checkNewCache is true) - */ - RollingCache.prototype.exists = function (name) { - if (this.rolled) - return false; - if (this.checkNewCache && fs.existsSync(this.newCacheRoot + "/" + name)) - return true; - return fs.existsSync(this.oldCacheRoot + "/" + name); - }; - RollingCache.prototype.path = function (name) { - return this.oldCacheRoot + "/" + name; - }; - /** - * @returns true if old cache contains all names and nothing more - */ - RollingCache.prototype.match = function (names) { - if (this.rolled) - return false; - if (!fs.existsSync(this.oldCacheRoot)) - return names.length === 0; // empty folder matches - return _.isEqual(fs.readdirSync(this.oldCacheRoot).sort(), names.sort()); - }; - /** - * @returns data for name, must exist in old cache (or either old of new cache if checkNewCache is true) - */ - RollingCache.prototype.read = function (name) { - if (this.checkNewCache && fs.existsSync(this.newCacheRoot + "/" + name)) - return fs.readJsonSync(this.newCacheRoot + "/" + name, "utf8"); - return fs.readJsonSync(this.oldCacheRoot + "/" + name, "utf8"); - }; - RollingCache.prototype.write = function (name, data) { - if (this.rolled) - return; - if (data === undefined) - return; - fs.writeJsonSync(this.newCacheRoot + "/" + name, data); - }; - RollingCache.prototype.touch = function (name) { - if (this.rolled) - return; - fs.ensureFileSync(this.newCacheRoot + "/" + name); - }; - /** - * clears old cache and moves new in its place - */ - RollingCache.prototype.roll = function () { - if (this.rolled) - return; - this.rolled = true; - fs.removeSync(this.oldCacheRoot); - fs.renameSync(this.newCacheRoot, this.oldCacheRoot); - }; - return RollingCache; +/** + * Saves data in new cache folder or reads it from old one. + * Avoids perpetually growing cache and situations when things need to consider changed and then reverted data to be changed. + */ +var RollingCache = (function () { + /** + * @param cacheRoot: root folder for the cache + * @param checkNewCache: whether to also look in new cache when reading from cache + */ + function RollingCache(cacheRoot, checkNewCache) { + this.cacheRoot = cacheRoot; + this.checkNewCache = checkNewCache; + this.rolled = false; + this.oldCacheRoot = this.cacheRoot + "/cache"; + this.newCacheRoot = this.cacheRoot + "/cache_"; + fs.emptyDirSync(this.newCacheRoot); + } + /** + * @returns true if name exist in old cache (or either old of new cache if checkNewCache is true) + */ + RollingCache.prototype.exists = function (name) { + if (this.rolled) + return false; + if (this.checkNewCache && fs.existsSync(this.newCacheRoot + "/" + name)) + return true; + return fs.existsSync(this.oldCacheRoot + "/" + name); + }; + RollingCache.prototype.path = function (name) { + return this.oldCacheRoot + "/" + name; + }; + /** + * @returns true if old cache contains all names and nothing more + */ + RollingCache.prototype.match = function (names) { + if (this.rolled) + return false; + if (!fs.existsSync(this.oldCacheRoot)) + return names.length === 0; // empty folder matches + return _.isEqual(fs.readdirSync(this.oldCacheRoot).sort(), names.sort()); + }; + /** + * @returns data for name, must exist in old cache (or either old of new cache if checkNewCache is true) + */ + RollingCache.prototype.read = function (name) { + if (this.checkNewCache && fs.existsSync(this.newCacheRoot + "/" + name)) + return fs.readJsonSync(this.newCacheRoot + "/" + name, "utf8"); + return fs.readJsonSync(this.oldCacheRoot + "/" + name, "utf8"); + }; + RollingCache.prototype.write = function (name, data) { + if (this.rolled) + return; + if (data === undefined) + return; + fs.writeJsonSync(this.newCacheRoot + "/" + name, data); + }; + RollingCache.prototype.touch = function (name) { + if (this.rolled) + return; + fs.ensureFileSync(this.newCacheRoot + "/" + name); + }; + /** + * clears old cache and moves new in its place + */ + RollingCache.prototype.roll = function () { + if (this.rolled) + return; + this.rolled = true; + fs.removeSync(this.oldCacheRoot); + fs.renameSync(this.newCacheRoot, this.oldCacheRoot); + }; + return RollingCache; }()); -function convertDiagnostic(type, data) { - return _.map(data, function (diagnostic) { - var entry = { - flatMessage: ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"), - category: diagnostic.category, - code: diagnostic.code, - type: type, - }; - if (diagnostic.file) { - var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; - entry.fileLine = diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + ")"; - } - return entry; - }); -} -var TsCache = (function () { - function TsCache(host, cache, options, rollupConfig, rootFilenames, context) { - var _this = this; - this.host = host; - this.options = options; - this.rollupConfig = rollupConfig; - this.context = context; - this.cacheVersion = "5"; - this.ambientTypesDirty = false; - this.cacheDir = cache + "/" + hash.sha1({ - version: this.cacheVersion, - rootFilenames: rootFilenames, - options: this.options, - rollupConfig: this.rollupConfig, - tsVersion: ts.version, - }); - this.dependencyTree = new graph.Graph({ directed: true }); - this.dependencyTree.setDefaultNodeLabel(function (_node) { return ({ dirty: false }); }); - var automaticTypes = _.map(ts.getAutomaticTypeDirectiveNames(options, ts.sys), function (entry) { return ts.resolveTypeReferenceDirective(entry, undefined, options, ts.sys); }) - .filter(function (entry) { return entry.resolvedTypeReferenceDirective && entry.resolvedTypeReferenceDirective.resolvedFileName; }) - .map(function (entry) { return entry.resolvedTypeReferenceDirective.resolvedFileName; }); - this.ambientTypes = _.filter(rootFilenames, function (file) { return _.endsWith(file, ".d.ts"); }) - .concat(automaticTypes) - .map(function (id) { return ({ id: id, snapshot: _this.host.getScriptSnapshot(id) }); }); - this.init(); - this.checkAmbientTypes(); - } - TsCache.prototype.clean = function () { - this.context.info(colors.blue("cleaning cache: " + this.cacheDir)); - fs.emptyDirSync(this.cacheDir); - this.init(); - }; - TsCache.prototype.setDependency = function (importee, importer) { - // importee -> importer - this.context.debug(colors.blue("dependency") + " '" + importee + "'"); - this.context.debug(" imported by '" + importer + "'"); - this.dependencyTree.setEdge(importer, importee); - }; - TsCache.prototype.walkTree = function (cb) { - var acyclic = graph.alg.isAcyclic(this.dependencyTree); - if (acyclic) { - _.each(graph.alg.topsort(this.dependencyTree), function (id) { return cb(id); }); - return; - } - this.context.info(colors.yellow("import tree has cycles")); - _.each(this.dependencyTree.nodes(), function (id) { return cb(id); }); - }; - TsCache.prototype.done = function () { - this.context.info(colors.blue("rolling caches")); - this.codeCache.roll(); - this.semanticDiagnosticsCache.roll(); - this.syntacticDiagnosticsCache.roll(); - this.typesCache.roll(); - }; - TsCache.prototype.getCompiled = function (id, snapshot, transform) { - var name = this.makeName(id, snapshot); - this.context.info(colors.blue("transpiling") + " '" + id + "'"); - this.context.debug(" cache: '" + this.codeCache.path(name) + "'"); - if (!this.codeCache.exists(name) || this.isDirty(id, snapshot, false)) { - this.context.debug(colors.yellow(" cache miss")); - var data_1 = transform(); - this.codeCache.write(name, data_1); - this.markAsDirty(id, snapshot); - return data_1; - } - this.context.debug(colors.green(" cache hit")); - var data = this.codeCache.read(name); - this.codeCache.write(name, data); - return data; - }; - TsCache.prototype.getSyntacticDiagnostics = function (id, snapshot, check) { - return this.getDiagnostics("syntax", this.syntacticDiagnosticsCache, id, snapshot, check); - }; - TsCache.prototype.getSemanticDiagnostics = function (id, snapshot, check) { - return this.getDiagnostics("semantic", this.semanticDiagnosticsCache, id, snapshot, check); - }; - TsCache.prototype.checkAmbientTypes = function () { - var _this = this; - this.context.debug(colors.blue("Ambient types:")); - var typeNames = _.filter(this.ambientTypes, function (snapshot) { return snapshot.snapshot !== undefined; }) - .map(function (snapshot) { - _this.context.debug(" " + snapshot.id); - return _this.makeName(snapshot.id, snapshot.snapshot); - }); - // types dirty if any d.ts changed, added or removed - this.ambientTypesDirty = !this.typesCache.match(typeNames); - if (this.ambientTypesDirty) - this.context.info(colors.yellow("ambient types changed, redoing all semantic diagnostics")); - _.each(typeNames, function (name) { return _this.typesCache.touch(name); }); - }; - TsCache.prototype.getDiagnostics = function (type, cache, id, snapshot, check) { - var name = this.makeName(id, snapshot); - this.context.debug(" cache: '" + cache.path(name) + "'"); - if (!cache.exists(name) || this.isDirty(id, snapshot, true)) { - this.context.debug(colors.yellow(" cache miss")); - var data_2 = convertDiagnostic(type, check()); - cache.write(name, data_2); - this.markAsDirty(id, snapshot); - return data_2; - } - this.context.debug(colors.green(" cache hit")); - var data = cache.read(name); - cache.write(name, data); - return data; - }; - TsCache.prototype.init = function () { - this.codeCache = new RollingCache(this.cacheDir + "/code", true); - this.typesCache = new RollingCache(this.cacheDir + "/types", true); - this.syntacticDiagnosticsCache = new RollingCache(this.cacheDir + "/syntacticDiagnostics", true); - this.semanticDiagnosticsCache = new RollingCache(this.cacheDir + "/semanticDiagnostics", true); - }; - TsCache.prototype.markAsDirty = function (id, _snapshot) { - this.dependencyTree.setNode(id, { dirty: true }); - }; - // returns true if node or any of its imports or any of global types changed - TsCache.prototype.isDirty = function (id, _snapshot, checkImports) { - var _this = this; - var label = this.dependencyTree.node(id); - if (!label) - return false; - if (!checkImports || label.dirty) - return label.dirty; - if (this.ambientTypesDirty) - return true; - var dependencies = graph.alg.dijkstra(this.dependencyTree, id); - return _.some(dependencies, function (dependency, node) { - if (!node || dependency.distance === Infinity) - return false; - var l = _this.dependencyTree.node(node); - var dirty = l === undefined ? true : l.dirty; - if (dirty) - _this.context.debug(" import changed: " + node); - return dirty; - }); - }; - TsCache.prototype.makeName = function (id, snapshot) { - var data = snapshot.getText(0, snapshot.getLength()); - return hash.sha1({ data: data, id: id }); - }; - return TsCache; +function convertDiagnostic(type, data) { + return _.map(data, function (diagnostic) { + var entry = { + flatMessage: ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"), + category: diagnostic.category, + code: diagnostic.code, + type: type, + }; + if (diagnostic.file) { + var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; + entry.fileLine = diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + ")"; + } + return entry; + }); +} +var TsCache = (function () { + function TsCache(host, cache, options, rollupConfig, rootFilenames, context) { + var _this = this; + this.host = host; + this.options = options; + this.rollupConfig = rollupConfig; + this.context = context; + this.cacheVersion = "5"; + this.ambientTypesDirty = false; + this.cacheDir = cache + "/" + hash.sha1({ + version: this.cacheVersion, + rootFilenames: rootFilenames, + options: this.options, + rollupConfig: this.rollupConfig, + tsVersion: ts.version, + }); + this.dependencyTree = new graph.Graph({ directed: true }); + this.dependencyTree.setDefaultNodeLabel(function (_node) { return ({ dirty: false }); }); + var automaticTypes = _.map(ts.getAutomaticTypeDirectiveNames(options, ts.sys), function (entry) { return ts.resolveTypeReferenceDirective(entry, undefined, options, ts.sys); }) + .filter(function (entry) { return entry.resolvedTypeReferenceDirective && entry.resolvedTypeReferenceDirective.resolvedFileName; }) + .map(function (entry) { return entry.resolvedTypeReferenceDirective.resolvedFileName; }); + this.ambientTypes = _.filter(rootFilenames, function (file) { return _.endsWith(file, ".d.ts"); }) + .concat(automaticTypes) + .map(function (id) { return ({ id: id, snapshot: _this.host.getScriptSnapshot(id) }); }); + this.init(); + this.checkAmbientTypes(); + } + TsCache.prototype.clean = function () { + this.context.info(colors.blue("cleaning cache: " + this.cacheDir)); + fs.emptyDirSync(this.cacheDir); + this.init(); + }; + TsCache.prototype.setDependency = function (importee, importer) { + // importee -> importer + this.context.debug(colors.blue("dependency") + " '" + importee + "'"); + this.context.debug(" imported by '" + importer + "'"); + this.dependencyTree.setEdge(importer, importee); + }; + TsCache.prototype.walkTree = function (cb) { + var acyclic = graph.alg.isAcyclic(this.dependencyTree); + if (acyclic) { + _.each(graph.alg.topsort(this.dependencyTree), function (id) { return cb(id); }); + return; + } + this.context.info(colors.yellow("import tree has cycles")); + _.each(this.dependencyTree.nodes(), function (id) { return cb(id); }); + }; + TsCache.prototype.done = function () { + this.context.info(colors.blue("rolling caches")); + this.codeCache.roll(); + this.semanticDiagnosticsCache.roll(); + this.syntacticDiagnosticsCache.roll(); + this.typesCache.roll(); + }; + TsCache.prototype.getCompiled = function (id, snapshot, transform) { + var name = this.makeName(id, snapshot); + this.context.info(colors.blue("transpiling") + " '" + id + "'"); + this.context.debug(" cache: '" + this.codeCache.path(name) + "'"); + if (!this.codeCache.exists(name) || this.isDirty(id, snapshot, false)) { + this.context.debug(colors.yellow(" cache miss")); + var data_1 = transform(); + this.codeCache.write(name, data_1); + this.markAsDirty(id, snapshot); + return data_1; + } + this.context.debug(colors.green(" cache hit")); + var data = this.codeCache.read(name); + this.codeCache.write(name, data); + return data; + }; + TsCache.prototype.getSyntacticDiagnostics = function (id, snapshot, check) { + return this.getDiagnostics("syntax", this.syntacticDiagnosticsCache, id, snapshot, check); + }; + TsCache.prototype.getSemanticDiagnostics = function (id, snapshot, check) { + return this.getDiagnostics("semantic", this.semanticDiagnosticsCache, id, snapshot, check); + }; + TsCache.prototype.checkAmbientTypes = function () { + var _this = this; + this.context.debug(colors.blue("Ambient types:")); + var typeNames = _.filter(this.ambientTypes, function (snapshot) { return snapshot.snapshot !== undefined; }) + .map(function (snapshot) { + _this.context.debug(" " + snapshot.id); + return _this.makeName(snapshot.id, snapshot.snapshot); + }); + // types dirty if any d.ts changed, added or removed + this.ambientTypesDirty = !this.typesCache.match(typeNames); + if (this.ambientTypesDirty) + this.context.info(colors.yellow("ambient types changed, redoing all semantic diagnostics")); + _.each(typeNames, function (name) { return _this.typesCache.touch(name); }); + }; + TsCache.prototype.getDiagnostics = function (type, cache, id, snapshot, check) { + var name = this.makeName(id, snapshot); + this.context.debug(" cache: '" + cache.path(name) + "'"); + if (!cache.exists(name) || this.isDirty(id, snapshot, true)) { + this.context.debug(colors.yellow(" cache miss")); + var data_2 = convertDiagnostic(type, check()); + cache.write(name, data_2); + this.markAsDirty(id, snapshot); + return data_2; + } + this.context.debug(colors.green(" cache hit")); + var data = cache.read(name); + cache.write(name, data); + return data; + }; + TsCache.prototype.init = function () { + this.codeCache = new RollingCache(this.cacheDir + "/code", true); + this.typesCache = new RollingCache(this.cacheDir + "/types", true); + this.syntacticDiagnosticsCache = new RollingCache(this.cacheDir + "/syntacticDiagnostics", true); + this.semanticDiagnosticsCache = new RollingCache(this.cacheDir + "/semanticDiagnostics", true); + }; + TsCache.prototype.markAsDirty = function (id, _snapshot) { + this.dependencyTree.setNode(id, { dirty: true }); + }; + // returns true if node or any of its imports or any of global types changed + TsCache.prototype.isDirty = function (id, _snapshot, checkImports) { + var _this = this; + var label = this.dependencyTree.node(id); + if (!label) + return false; + if (!checkImports || label.dirty) + return label.dirty; + if (this.ambientTypesDirty) + return true; + var dependencies = graph.alg.dijkstra(this.dependencyTree, id); + return _.some(dependencies, function (dependency, node) { + if (!node || dependency.distance === Infinity) + return false; + var l = _this.dependencyTree.node(node); + var dirty = l === undefined ? true : l.dirty; + if (dirty) + _this.context.debug(" import changed: " + node); + return dirty; + }); + }; + TsCache.prototype.makeName = function (id, snapshot) { + var data = snapshot.getText(0, snapshot.getLength()); + return hash.sha1({ data: data, id: id }); + }; + return TsCache; }()); -// tslint:disable-next-line:no-var-requires -var createFilter = require("rollup-pluginutils").createFilter; -function getOptionsOverrides() { - return { - module: ts.ModuleKind.ES2015, - noEmitHelpers: true, - importHelpers: true, - noResolve: false, - }; -} -// The injected id for helpers. -var TSLIB = "tslib"; -var tslibSource; -try { - // tslint:disable-next-line:no-string-literal no-var-requires - var tslibPath = require.resolve("tslib/" + require("tslib/package.json")["module"]); - tslibSource = fs.readFileSync(tslibPath, "utf8"); -} -catch (e) { - console.warn("Error loading `tslib` helper library."); - throw e; -} -function parseTsConfig(context) { - var fileName = ts.findConfigFile(process.cwd(), ts.sys.fileExists, "tsconfig.json"); - if (!fileName) - throw new Error("couldn't find 'tsconfig.json' in " + process.cwd()); - var text = ts.sys.readFile(fileName); - var result = ts.parseConfigFileTextToJson(fileName, text); - if (result.error) { - printDiagnostics(context, convertDiagnostic("config", [result.error])); - throw new Error("failed to parse " + fileName); - } - var configParseResult = ts.parseJsonConfigFileContent(result.config, ts.sys, path.dirname(fileName), getOptionsOverrides(), fileName); - return configParseResult; -} -function printDiagnostics(context, diagnostics) { - _.each(diagnostics, function (diagnostic) { - var print; - var color; - var category; - switch (diagnostic.category) { - case ts.DiagnosticCategory.Message: - print = context.info; - color = colors.white; - category = ""; - break; - case ts.DiagnosticCategory.Error: - print = context.error; - color = colors.red; - category = "error"; - break; - case ts.DiagnosticCategory.Warning: - default: - print = context.warn; - color = colors.yellow; - category = "warning"; - break; - } - // const type = ""; - var type = diagnostic.type + " "; - if (diagnostic.fileLine) - print.call(context, [diagnostic.fileLine + ": " + type + category + " TS" + diagnostic.code + " " + color(diagnostic.flatMessage)]); - else - print.call(context, ["" + type + category + " TS" + diagnostic.code + " " + color(diagnostic.flatMessage)]); - }); -} -function typescript(options) { - options = __assign({}, options); - _.defaults(options, { - check: true, - verbosity: VerbosityLevel.Warning, - clean: false, - cacheRoot: process.cwd() + "/.rpt2_cache", - include: ["*.ts+(|x)", "**/*.ts+(|x)"], - exclude: ["*.d.ts", "**/*.d.ts"], - abortOnError: true, - rollupCommonJSResolveHack: false, - }); - var rollupConfig; - var watchMode = false; - var round = 0; - var targetCount = 0; - var context = new ConsoleContext(options.verbosity, "rpt2: "); - context.info("Typescript version: " + ts.version); - context.debug("Options: " + JSON.stringify(options, undefined, 4)); - var filter$$1 = createFilter(options.include, options.exclude); - var parsedConfig = parseTsConfig(context); - var servicesHost = new LanguageServiceHost(parsedConfig); - var service = ts.createLanguageService(servicesHost, ts.createDocumentRegistry()); - var _cache; - var cache = function () { - if (!_cache) - _cache = new TsCache(servicesHost, options.cacheRoot, parsedConfig.options, rollupConfig, parsedConfig.fileNames, context); - return _cache; - }; - var noErrors = true; - // printing compiler option errors - if (options.check) - printDiagnostics(context, convertDiagnostic("options", service.getCompilerOptionsDiagnostics())); - return { - options: function (config) { - rollupConfig = config; - context.debug("rollupConfig: " + JSON.stringify(rollupConfig, undefined, 4)); - if (options.clean) - cache().clean(); - }, - resolveId: function (importee, importer) { - if (importee === TSLIB) - return "\0" + TSLIB; - if (!importer) - return null; - importer = importer.split("\\").join("/"); - // TODO: use module resolution cache - var result = ts.nodeModuleNameResolver(importee, importer, parsedConfig.options, ts.sys); - if (result.resolvedModule && result.resolvedModule.resolvedFileName) { - if (filter$$1(result.resolvedModule.resolvedFileName)) - cache().setDependency(result.resolvedModule.resolvedFileName, importer); - if (_.endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) - return null; - var resolved = options.rollupCommonJSResolveHack - ? resolve.sync(result.resolvedModule.resolvedFileName) - : result.resolvedModule.resolvedFileName; - context.debug(colors.blue("resolving") + " '" + importee + "'"); - context.debug(" to '" + resolved + "'"); - return resolved; - } - return null; - }, - load: function (id) { - if (id === "\0" + TSLIB) - return tslibSource; - return undefined; - }, - transform: function (code, id) { - var _this = this; - if (!filter$$1(id)) - return undefined; - var contextWrapper = new RollupContext(options.verbosity, options.abortOnError, this, "rpt2: "); - var snapshot = servicesHost.setSnapshot(id, code); - // getting compiled file from cache or from ts - var result = cache().getCompiled(id, snapshot, function () { - var output = service.getEmitOutput(id); - if (output.emitSkipped) { - noErrors = false; - // always checking on fatal errors, even if options.check is set to false - var diagnostics = _.concat(cache().getSyntacticDiagnostics(id, snapshot, function () { - return service.getSyntacticDiagnostics(id); - }), cache().getSemanticDiagnostics(id, snapshot, function () { - return service.getSemanticDiagnostics(id); - })); - printDiagnostics(contextWrapper, diagnostics); - // since no output was generated, aborting compilation - cache().done(); - if (_.isFunction(_this.error)) - _this.error(colors.red("failed to transpile '" + id + "'")); - } - var transpiled = _.find(output.outputFiles, function (entry) { return _.endsWith(entry.name, ".js"); }); - var map$$1 = _.find(output.outputFiles, function (entry) { return _.endsWith(entry.name, ".map"); }); - return { - code: transpiled ? transpiled.text : undefined, - map: map$$1 ? JSON.parse(map$$1.text) : { mappings: "" }, - }; - }); - if (options.check) { - var diagnostics = _.concat(cache().getSyntacticDiagnostics(id, snapshot, function () { - return service.getSyntacticDiagnostics(id); - }), cache().getSemanticDiagnostics(id, snapshot, function () { - return service.getSemanticDiagnostics(id); - })); - if (diagnostics.length > 0) - noErrors = false; - printDiagnostics(contextWrapper, diagnostics); - } - return result; - }, - ongenerate: function (bundleOptions) { - targetCount = _.get(bundleOptions, "targets.length", 1); - if (round >= targetCount) { - watchMode = true; - round = 0; - } - context.debug("generating target " + (round + 1) + " of " + targetCount); - if (watchMode && round === 0) { - context.debug("running in watch mode"); - cache().walkTree(function (id) { - var diagnostics = _.concat(convertDiagnostic("syntax", service.getSyntacticDiagnostics(id)), convertDiagnostic("semantic", service.getSemanticDiagnostics(id))); - printDiagnostics(context, diagnostics); - }); - } - if (!watchMode && !noErrors) - context.info(colors.yellow("there were errors or warnings above.")); - cache().done(); - round++; - }, - }; +// tslint:disable-next-line:no-var-requires +var createFilter = require("rollup-pluginutils").createFilter; +function getOptionsOverrides() { + return { + module: ts.ModuleKind.ES2015, + noEmitHelpers: true, + importHelpers: true, + noResolve: false, + }; +} +// The injected id for helpers. +var TSLIB = "tslib"; +var tslibSource; +try { + // tslint:disable-next-line:no-string-literal no-var-requires + var tslibPath = require.resolve("tslib/" + require("tslib/package.json")["module"]); + tslibSource = fs.readFileSync(tslibPath, "utf8"); +} +catch (e) { + console.warn("Error loading `tslib` helper library."); + throw e; +} +function parseTsConfig(context) { + var fileName = ts.findConfigFile(process.cwd(), ts.sys.fileExists, "tsconfig.json"); + if (!fileName) + throw new Error("couldn't find 'tsconfig.json' in " + process.cwd()); + var text = ts.sys.readFile(fileName); + var result = ts.parseConfigFileTextToJson(fileName, text); + if (result.error) { + printDiagnostics(context, convertDiagnostic("config", [result.error])); + throw new Error("failed to parse " + fileName); + } + var configParseResult = ts.parseJsonConfigFileContent(result.config, ts.sys, path.dirname(fileName), getOptionsOverrides(), fileName); + return configParseResult; +} +function printDiagnostics(context, diagnostics) { + _.each(diagnostics, function (diagnostic) { + var print; + var color; + var category; + switch (diagnostic.category) { + case ts.DiagnosticCategory.Message: + print = context.info; + color = colors.white; + category = ""; + break; + case ts.DiagnosticCategory.Error: + print = context.error; + color = colors.red; + category = "error"; + break; + case ts.DiagnosticCategory.Warning: + default: + print = context.warn; + color = colors.yellow; + category = "warning"; + break; + } + // const type = ""; + var type = diagnostic.type + " "; + if (diagnostic.fileLine) + print.call(context, [diagnostic.fileLine + ": " + type + category + " TS" + diagnostic.code + " " + color(diagnostic.flatMessage)]); + else + print.call(context, ["" + type + category + " TS" + diagnostic.code + " " + color(diagnostic.flatMessage)]); + }); +} +function typescript(options) { + options = __assign({}, options); + _.defaults(options, { + check: true, + verbosity: VerbosityLevel.Warning, + clean: false, + cacheRoot: process.cwd() + "/.rpt2_cache", + include: ["*.ts+(|x)", "**/*.ts+(|x)"], + exclude: ["*.d.ts", "**/*.d.ts"], + abortOnError: true, + rollupCommonJSResolveHack: false, + }); + var rollupConfig; + var watchMode = false; + var round = 0; + var targetCount = 0; + var context = new ConsoleContext(options.verbosity, "rpt2: "); + context.info("Typescript version: " + ts.version); + context.debug("Options: " + JSON.stringify(options, undefined, 4)); + var filter$$1 = createFilter(options.include, options.exclude); + var parsedConfig = parseTsConfig(context); + var servicesHost = new LanguageServiceHost(parsedConfig); + var service = ts.createLanguageService(servicesHost, ts.createDocumentRegistry()); + var _cache; + var cache = function () { + if (!_cache) + _cache = new TsCache(servicesHost, options.cacheRoot, parsedConfig.options, rollupConfig, parsedConfig.fileNames, context); + return _cache; + }; + var noErrors = true; + var declarations = []; + // printing compiler option errors + if (options.check) + printDiagnostics(context, convertDiagnostic("options", service.getCompilerOptionsDiagnostics())); + return { + options: function (config) { + rollupConfig = config; + context.debug("rollupConfig: " + JSON.stringify(rollupConfig, undefined, 4)); + if (options.clean) + cache().clean(); + }, + resolveId: function (importee, importer) { + if (importee === TSLIB) + return "\0" + TSLIB; + if (!importer) + return null; + importer = importer.split("\\").join("/"); + // TODO: use module resolution cache + var result = ts.nodeModuleNameResolver(importee, importer, parsedConfig.options, ts.sys); + if (result.resolvedModule && result.resolvedModule.resolvedFileName) { + if (filter$$1(result.resolvedModule.resolvedFileName)) + cache().setDependency(result.resolvedModule.resolvedFileName, importer); + if (_.endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) + return null; + var resolved = options.rollupCommonJSResolveHack + ? resolve.sync(result.resolvedModule.resolvedFileName) + : result.resolvedModule.resolvedFileName; + context.debug(colors.blue("resolving") + " '" + importee + "'"); + context.debug(" to '" + resolved + "'"); + return resolved; + } + return null; + }, + load: function (id) { + if (id === "\0" + TSLIB) + return tslibSource; + return undefined; + }, + transform: function (code, id) { + var _this = this; + if (!filter$$1(id)) + return undefined; + var contextWrapper = new RollupContext(options.verbosity, options.abortOnError, this, "rpt2: "); + var snapshot = servicesHost.setSnapshot(id, code); + // getting compiled file from cache or from ts + var result = cache().getCompiled(id, snapshot, function () { + var output = service.getEmitOutput(id); + if (output.emitSkipped) { + noErrors = false; + // always checking on fatal errors, even if options.check is set to false + var diagnostics = _.concat(cache().getSyntacticDiagnostics(id, snapshot, function () { + return service.getSyntacticDiagnostics(id); + }), cache().getSemanticDiagnostics(id, snapshot, function () { + return service.getSemanticDiagnostics(id); + })); + printDiagnostics(contextWrapper, diagnostics); + // since no output was generated, aborting compilation + cache().done(); + if (_.isFunction(_this.error)) + _this.error(colors.red("failed to transpile '" + id + "'")); + } + var transpiled = _.find(output.outputFiles, function (entry) { return _.endsWith(entry.name, ".js"); }); + var map$$1 = _.find(output.outputFiles, function (entry) { return _.endsWith(entry.name, ".map"); }); + var dts = _.find(output.outputFiles, function (entry) { return _.endsWith(entry.name, ".d.ts"); }); + return { + code: transpiled ? transpiled.text : undefined, + map: map$$1 ? JSON.parse(map$$1.text) : { mappings: "" }, + dts: dts, + }; + }); + if (options.check) { + var diagnostics = _.concat(cache().getSyntacticDiagnostics(id, snapshot, function () { + return service.getSyntacticDiagnostics(id); + }), cache().getSemanticDiagnostics(id, snapshot, function () { + return service.getSemanticDiagnostics(id); + })); + if (diagnostics.length > 0) + noErrors = false; + printDiagnostics(contextWrapper, diagnostics); + } + if (result && result.dts) { + declarations.push(result.dts); + } + return result; + }, + ongenerate: function (bundleOptions) { + targetCount = _.get(bundleOptions, "targets.length", 1); + if (round >= targetCount) { + watchMode = true; + round = 0; + } + context.debug("generating target " + (round + 1) + " of " + targetCount); + if (watchMode && round === 0) { + context.debug("running in watch mode"); + cache().walkTree(function (id) { + var diagnostics = _.concat(convertDiagnostic("syntax", service.getSyntacticDiagnostics(id)), convertDiagnostic("semantic", service.getSemanticDiagnostics(id))); + printDiagnostics(context, diagnostics); + }); + } + if (!watchMode && !noErrors) + context.info(colors.yellow("there were errors or warnings above.")); + cache().done(); + round++; + }, + onwrite: function () { + declarations.forEach(function (_a) { + var name = _a.name, text = _a.text, writeByteOrderMark = _a.writeByteOrderMark; + ts.sys.writeFile(name, text, writeByteOrderMark); + }); + }, + }; } module.exports = typescript; diff --git a/dist/rollup-plugin-typescript2.es.js b/dist/rollup-plugin-typescript2.es.js index d4e2ca5..77107c5 100644 --- a/dist/rollup-plugin-typescript2.es.js +++ b/dist/rollup-plugin-typescript2.es.js @@ -16,587 +16,599 @@ import * as path from 'path'; import { sync } from 'resolve'; import * as resolve from 'resolve'; -/*! ***************************************************************************** -Copyright (c) Microsoft Corporation. All rights reserved. -Licensed under the Apache License, Version 2.0 (the "License"); you may not use -this file except in compliance with the License. You may obtain a copy of the -License at http://www.apache.org/licenses/LICENSE-2.0 - -THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED -WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, -MERCHANTABLITY OR NON-INFRINGEMENT. - -See the Apache Version 2.0 License for specific language governing permissions -and limitations under the License. -***************************************************************************** */ -/* global Reflect, Promise */ - - - -var __assign = Object.assign || function __assign(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; - } - return t; +/*! ***************************************************************************** +Copyright (c) Microsoft Corporation. All rights reserved. +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy of the +License at http://www.apache.org/licenses/LICENSE-2.0 + +THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +MERCHANTABLITY OR NON-INFRINGEMENT. + +See the Apache Version 2.0 License for specific language governing permissions +and limitations under the License. +***************************************************************************** */ +/* global Reflect, Promise */ + + + +var __assign = Object.assign || function __assign(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; + } + return t; }; -var VerbosityLevel; -(function (VerbosityLevel) { - VerbosityLevel[VerbosityLevel["Error"] = 0] = "Error"; - VerbosityLevel[VerbosityLevel["Warning"] = 1] = "Warning"; - VerbosityLevel[VerbosityLevel["Info"] = 2] = "Info"; - VerbosityLevel[VerbosityLevel["Debug"] = 3] = "Debug"; -})(VerbosityLevel || (VerbosityLevel = {})); -var ConsoleContext = (function () { - function ConsoleContext(verbosity, prefix) { - if (prefix === void 0) { prefix = ""; } - this.verbosity = verbosity; - this.prefix = prefix; - } - ConsoleContext.prototype.warn = function (message) { - if (this.verbosity < VerbosityLevel.Warning) - return; - console.log("" + this.prefix + message); - }; - ConsoleContext.prototype.error = function (message) { - if (this.verbosity < VerbosityLevel.Error) - return; - console.log("" + this.prefix + message); - }; - ConsoleContext.prototype.info = function (message) { - if (this.verbosity < VerbosityLevel.Info) - return; - console.log("" + this.prefix + message); - }; - ConsoleContext.prototype.debug = function (message) { - if (this.verbosity < VerbosityLevel.Debug) - return; - console.log("" + this.prefix + message); - }; - return ConsoleContext; +var VerbosityLevel; +(function (VerbosityLevel) { + VerbosityLevel[VerbosityLevel["Error"] = 0] = "Error"; + VerbosityLevel[VerbosityLevel["Warning"] = 1] = "Warning"; + VerbosityLevel[VerbosityLevel["Info"] = 2] = "Info"; + VerbosityLevel[VerbosityLevel["Debug"] = 3] = "Debug"; +})(VerbosityLevel || (VerbosityLevel = {})); +var ConsoleContext = (function () { + function ConsoleContext(verbosity, prefix) { + if (prefix === void 0) { prefix = ""; } + this.verbosity = verbosity; + this.prefix = prefix; + } + ConsoleContext.prototype.warn = function (message) { + if (this.verbosity < VerbosityLevel.Warning) + return; + console.log("" + this.prefix + message); + }; + ConsoleContext.prototype.error = function (message) { + if (this.verbosity < VerbosityLevel.Error) + return; + console.log("" + this.prefix + message); + }; + ConsoleContext.prototype.info = function (message) { + if (this.verbosity < VerbosityLevel.Info) + return; + console.log("" + this.prefix + message); + }; + ConsoleContext.prototype.debug = function (message) { + if (this.verbosity < VerbosityLevel.Debug) + return; + console.log("" + this.prefix + message); + }; + return ConsoleContext; }()); -var RollupContext = (function () { - function RollupContext(verbosity, bail, context, prefix) { - if (prefix === void 0) { prefix = ""; } - this.verbosity = verbosity; - this.bail = bail; - this.context = context; - this.prefix = prefix; - this.hasContext = true; - this.hasContext = isFunction(this.context.warn) && isFunction(this.context.error); - } - RollupContext.prototype.warn = function (message) { - if (this.verbosity < VerbosityLevel.Warning) - return; - if (this.hasContext) - this.context.warn("" + this.prefix + message); - else - console.log("" + this.prefix + message); - }; - RollupContext.prototype.error = function (message) { - if (this.verbosity < VerbosityLevel.Error) - return; - if (this.hasContext) { - if (this.bail) - this.context.error("" + this.prefix + message); - else - this.context.warn("" + this.prefix + message); - } - else - console.log("" + this.prefix + message); - }; - RollupContext.prototype.info = function (message) { - if (this.verbosity < VerbosityLevel.Info) - return; - console.log("" + this.prefix + message); - }; - RollupContext.prototype.debug = function (message) { - if (this.verbosity < VerbosityLevel.Debug) - return; - console.log("" + this.prefix + message); - }; - return RollupContext; +var RollupContext = (function () { + function RollupContext(verbosity, bail, context, prefix) { + if (prefix === void 0) { prefix = ""; } + this.verbosity = verbosity; + this.bail = bail; + this.context = context; + this.prefix = prefix; + this.hasContext = true; + this.hasContext = isFunction(this.context.warn) && isFunction(this.context.error); + } + RollupContext.prototype.warn = function (message) { + if (this.verbosity < VerbosityLevel.Warning) + return; + if (this.hasContext) + this.context.warn("" + this.prefix + message); + else + console.log("" + this.prefix + message); + }; + RollupContext.prototype.error = function (message) { + if (this.verbosity < VerbosityLevel.Error) + return; + if (this.hasContext) { + if (this.bail) + this.context.error("" + this.prefix + message); + else + this.context.warn("" + this.prefix + message); + } + else + console.log("" + this.prefix + message); + }; + RollupContext.prototype.info = function (message) { + if (this.verbosity < VerbosityLevel.Info) + return; + console.log("" + this.prefix + message); + }; + RollupContext.prototype.debug = function (message) { + if (this.verbosity < VerbosityLevel.Debug) + return; + console.log("" + this.prefix + message); + }; + return RollupContext; }()); -var LanguageServiceHost = (function () { - function LanguageServiceHost(parsedConfig) { - this.parsedConfig = parsedConfig; - this.cwd = process.cwd(); - this.snapshots = {}; - this.versions = {}; - } - LanguageServiceHost.prototype.reset = function () { - this.snapshots = {}; - this.versions = {}; - }; - LanguageServiceHost.prototype.setSnapshot = function (fileName, data) { - fileName = this.normalize(fileName); - var snapshot = ScriptSnapshot.fromString(data); - this.snapshots[fileName] = snapshot; - this.versions[fileName] = (this.versions[fileName] || 0) + 1; - return snapshot; - }; - LanguageServiceHost.prototype.getScriptSnapshot = function (fileName) { - fileName = this.normalize(fileName); - if (has(this.snapshots, fileName)) - return this.snapshots[fileName]; - if (existsSync(fileName)) { - this.snapshots[fileName] = ScriptSnapshot.fromString(sys.readFile(fileName)); - this.versions[fileName] = (this.versions[fileName] || 0) + 1; - return this.snapshots[fileName]; - } - return undefined; - }; - LanguageServiceHost.prototype.getCurrentDirectory = function () { - return this.cwd; - }; - LanguageServiceHost.prototype.getScriptVersion = function (fileName) { - fileName = this.normalize(fileName); - return (this.versions[fileName] || 0).toString(); - }; - LanguageServiceHost.prototype.getScriptFileNames = function () { - return this.parsedConfig.fileNames; - }; - LanguageServiceHost.prototype.getCompilationSettings = function () { - return this.parsedConfig.options; - }; - LanguageServiceHost.prototype.getDefaultLibFileName = function (opts) { - return getDefaultLibFilePath(opts); - }; - LanguageServiceHost.prototype.normalize = function (fileName) { - return fileName.split("\\").join("/"); - }; - return LanguageServiceHost; +var LanguageServiceHost = (function () { + function LanguageServiceHost(parsedConfig) { + this.parsedConfig = parsedConfig; + this.cwd = process.cwd(); + this.snapshots = {}; + this.versions = {}; + } + LanguageServiceHost.prototype.reset = function () { + this.snapshots = {}; + this.versions = {}; + }; + LanguageServiceHost.prototype.setSnapshot = function (fileName, data) { + fileName = this.normalize(fileName); + var snapshot = ScriptSnapshot.fromString(data); + this.snapshots[fileName] = snapshot; + this.versions[fileName] = (this.versions[fileName] || 0) + 1; + return snapshot; + }; + LanguageServiceHost.prototype.getScriptSnapshot = function (fileName) { + fileName = this.normalize(fileName); + if (has(this.snapshots, fileName)) + return this.snapshots[fileName]; + if (existsSync(fileName)) { + this.snapshots[fileName] = ScriptSnapshot.fromString(sys.readFile(fileName)); + this.versions[fileName] = (this.versions[fileName] || 0) + 1; + return this.snapshots[fileName]; + } + return undefined; + }; + LanguageServiceHost.prototype.getCurrentDirectory = function () { + return this.cwd; + }; + LanguageServiceHost.prototype.getScriptVersion = function (fileName) { + fileName = this.normalize(fileName); + return (this.versions[fileName] || 0).toString(); + }; + LanguageServiceHost.prototype.getScriptFileNames = function () { + return this.parsedConfig.fileNames; + }; + LanguageServiceHost.prototype.getCompilationSettings = function () { + return this.parsedConfig.options; + }; + LanguageServiceHost.prototype.getDefaultLibFileName = function (opts) { + return getDefaultLibFilePath(opts); + }; + LanguageServiceHost.prototype.normalize = function (fileName) { + return fileName.split("\\").join("/"); + }; + return LanguageServiceHost; }()); -/** - * Saves data in new cache folder or reads it from old one. - * Avoids perpetually growing cache and situations when things need to consider changed and then reverted data to be changed. - */ -var RollingCache = (function () { - /** - * @param cacheRoot: root folder for the cache - * @param checkNewCache: whether to also look in new cache when reading from cache - */ - function RollingCache(cacheRoot, checkNewCache) { - this.cacheRoot = cacheRoot; - this.checkNewCache = checkNewCache; - this.rolled = false; - this.oldCacheRoot = this.cacheRoot + "/cache"; - this.newCacheRoot = this.cacheRoot + "/cache_"; - emptyDirSync(this.newCacheRoot); - } - /** - * @returns true if name exist in old cache (or either old of new cache if checkNewCache is true) - */ - RollingCache.prototype.exists = function (name) { - if (this.rolled) - return false; - if (this.checkNewCache && existsSync(this.newCacheRoot + "/" + name)) - return true; - return existsSync(this.oldCacheRoot + "/" + name); - }; - RollingCache.prototype.path = function (name) { - return this.oldCacheRoot + "/" + name; - }; - /** - * @returns true if old cache contains all names and nothing more - */ - RollingCache.prototype.match = function (names) { - if (this.rolled) - return false; - if (!existsSync(this.oldCacheRoot)) - return names.length === 0; // empty folder matches - return isEqual(readdirSync(this.oldCacheRoot).sort(), names.sort()); - }; - /** - * @returns data for name, must exist in old cache (or either old of new cache if checkNewCache is true) - */ - RollingCache.prototype.read = function (name) { - if (this.checkNewCache && existsSync(this.newCacheRoot + "/" + name)) - return readJsonSync(this.newCacheRoot + "/" + name, "utf8"); - return readJsonSync(this.oldCacheRoot + "/" + name, "utf8"); - }; - RollingCache.prototype.write = function (name, data) { - if (this.rolled) - return; - if (data === undefined) - return; - writeJsonSync(this.newCacheRoot + "/" + name, data); - }; - RollingCache.prototype.touch = function (name) { - if (this.rolled) - return; - ensureFileSync(this.newCacheRoot + "/" + name); - }; - /** - * clears old cache and moves new in its place - */ - RollingCache.prototype.roll = function () { - if (this.rolled) - return; - this.rolled = true; - removeSync(this.oldCacheRoot); - renameSync(this.newCacheRoot, this.oldCacheRoot); - }; - return RollingCache; +/** + * Saves data in new cache folder or reads it from old one. + * Avoids perpetually growing cache and situations when things need to consider changed and then reverted data to be changed. + */ +var RollingCache = (function () { + /** + * @param cacheRoot: root folder for the cache + * @param checkNewCache: whether to also look in new cache when reading from cache + */ + function RollingCache(cacheRoot, checkNewCache) { + this.cacheRoot = cacheRoot; + this.checkNewCache = checkNewCache; + this.rolled = false; + this.oldCacheRoot = this.cacheRoot + "/cache"; + this.newCacheRoot = this.cacheRoot + "/cache_"; + emptyDirSync(this.newCacheRoot); + } + /** + * @returns true if name exist in old cache (or either old of new cache if checkNewCache is true) + */ + RollingCache.prototype.exists = function (name) { + if (this.rolled) + return false; + if (this.checkNewCache && existsSync(this.newCacheRoot + "/" + name)) + return true; + return existsSync(this.oldCacheRoot + "/" + name); + }; + RollingCache.prototype.path = function (name) { + return this.oldCacheRoot + "/" + name; + }; + /** + * @returns true if old cache contains all names and nothing more + */ + RollingCache.prototype.match = function (names) { + if (this.rolled) + return false; + if (!existsSync(this.oldCacheRoot)) + return names.length === 0; // empty folder matches + return isEqual(readdirSync(this.oldCacheRoot).sort(), names.sort()); + }; + /** + * @returns data for name, must exist in old cache (or either old of new cache if checkNewCache is true) + */ + RollingCache.prototype.read = function (name) { + if (this.checkNewCache && existsSync(this.newCacheRoot + "/" + name)) + return readJsonSync(this.newCacheRoot + "/" + name, "utf8"); + return readJsonSync(this.oldCacheRoot + "/" + name, "utf8"); + }; + RollingCache.prototype.write = function (name, data) { + if (this.rolled) + return; + if (data === undefined) + return; + writeJsonSync(this.newCacheRoot + "/" + name, data); + }; + RollingCache.prototype.touch = function (name) { + if (this.rolled) + return; + ensureFileSync(this.newCacheRoot + "/" + name); + }; + /** + * clears old cache and moves new in its place + */ + RollingCache.prototype.roll = function () { + if (this.rolled) + return; + this.rolled = true; + removeSync(this.oldCacheRoot); + renameSync(this.newCacheRoot, this.oldCacheRoot); + }; + return RollingCache; }()); -function convertDiagnostic(type, data) { - return map(data, function (diagnostic) { - var entry = { - flatMessage: flattenDiagnosticMessageText(diagnostic.messageText, "\n"), - category: diagnostic.category, - code: diagnostic.code, - type: type, - }; - if (diagnostic.file) { - var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; - entry.fileLine = diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + ")"; - } - return entry; - }); -} -var TsCache = (function () { - function TsCache(host, cache, options, rollupConfig, rootFilenames, context) { - var _this = this; - this.host = host; - this.options = options; - this.rollupConfig = rollupConfig; - this.context = context; - this.cacheVersion = "5"; - this.ambientTypesDirty = false; - this.cacheDir = cache + "/" + sha1({ - version: this.cacheVersion, - rootFilenames: rootFilenames, - options: this.options, - rollupConfig: this.rollupConfig, - tsVersion: version, - }); - this.dependencyTree = new Graph({ directed: true }); - this.dependencyTree.setDefaultNodeLabel(function (_node) { return ({ dirty: false }); }); - var automaticTypes = map(getAutomaticTypeDirectiveNames(options, sys), function (entry) { return resolveTypeReferenceDirective(entry, undefined, options, sys); }) - .filter(function (entry) { return entry.resolvedTypeReferenceDirective && entry.resolvedTypeReferenceDirective.resolvedFileName; }) - .map(function (entry) { return entry.resolvedTypeReferenceDirective.resolvedFileName; }); - this.ambientTypes = filter(rootFilenames, function (file) { return endsWith(file, ".d.ts"); }) - .concat(automaticTypes) - .map(function (id) { return ({ id: id, snapshot: _this.host.getScriptSnapshot(id) }); }); - this.init(); - this.checkAmbientTypes(); - } - TsCache.prototype.clean = function () { - this.context.info(blue("cleaning cache: " + this.cacheDir)); - emptyDirSync(this.cacheDir); - this.init(); - }; - TsCache.prototype.setDependency = function (importee, importer) { - // importee -> importer - this.context.debug(blue("dependency") + " '" + importee + "'"); - this.context.debug(" imported by '" + importer + "'"); - this.dependencyTree.setEdge(importer, importee); - }; - TsCache.prototype.walkTree = function (cb) { - var acyclic = alg.isAcyclic(this.dependencyTree); - if (acyclic) { - each(alg.topsort(this.dependencyTree), function (id) { return cb(id); }); - return; - } - this.context.info(yellow("import tree has cycles")); - each(this.dependencyTree.nodes(), function (id) { return cb(id); }); - }; - TsCache.prototype.done = function () { - this.context.info(blue("rolling caches")); - this.codeCache.roll(); - this.semanticDiagnosticsCache.roll(); - this.syntacticDiagnosticsCache.roll(); - this.typesCache.roll(); - }; - TsCache.prototype.getCompiled = function (id, snapshot, transform) { - var name = this.makeName(id, snapshot); - this.context.info(blue("transpiling") + " '" + id + "'"); - this.context.debug(" cache: '" + this.codeCache.path(name) + "'"); - if (!this.codeCache.exists(name) || this.isDirty(id, snapshot, false)) { - this.context.debug(yellow(" cache miss")); - var data_1 = transform(); - this.codeCache.write(name, data_1); - this.markAsDirty(id, snapshot); - return data_1; - } - this.context.debug(green(" cache hit")); - var data = this.codeCache.read(name); - this.codeCache.write(name, data); - return data; - }; - TsCache.prototype.getSyntacticDiagnostics = function (id, snapshot, check) { - return this.getDiagnostics("syntax", this.syntacticDiagnosticsCache, id, snapshot, check); - }; - TsCache.prototype.getSemanticDiagnostics = function (id, snapshot, check) { - return this.getDiagnostics("semantic", this.semanticDiagnosticsCache, id, snapshot, check); - }; - TsCache.prototype.checkAmbientTypes = function () { - var _this = this; - this.context.debug(blue("Ambient types:")); - var typeNames = filter(this.ambientTypes, function (snapshot) { return snapshot.snapshot !== undefined; }) - .map(function (snapshot) { - _this.context.debug(" " + snapshot.id); - return _this.makeName(snapshot.id, snapshot.snapshot); - }); - // types dirty if any d.ts changed, added or removed - this.ambientTypesDirty = !this.typesCache.match(typeNames); - if (this.ambientTypesDirty) - this.context.info(yellow("ambient types changed, redoing all semantic diagnostics")); - each(typeNames, function (name) { return _this.typesCache.touch(name); }); - }; - TsCache.prototype.getDiagnostics = function (type, cache, id, snapshot, check) { - var name = this.makeName(id, snapshot); - this.context.debug(" cache: '" + cache.path(name) + "'"); - if (!cache.exists(name) || this.isDirty(id, snapshot, true)) { - this.context.debug(yellow(" cache miss")); - var data_2 = convertDiagnostic(type, check()); - cache.write(name, data_2); - this.markAsDirty(id, snapshot); - return data_2; - } - this.context.debug(green(" cache hit")); - var data = cache.read(name); - cache.write(name, data); - return data; - }; - TsCache.prototype.init = function () { - this.codeCache = new RollingCache(this.cacheDir + "/code", true); - this.typesCache = new RollingCache(this.cacheDir + "/types", true); - this.syntacticDiagnosticsCache = new RollingCache(this.cacheDir + "/syntacticDiagnostics", true); - this.semanticDiagnosticsCache = new RollingCache(this.cacheDir + "/semanticDiagnostics", true); - }; - TsCache.prototype.markAsDirty = function (id, _snapshot) { - this.dependencyTree.setNode(id, { dirty: true }); - }; - // returns true if node or any of its imports or any of global types changed - TsCache.prototype.isDirty = function (id, _snapshot, checkImports) { - var _this = this; - var label = this.dependencyTree.node(id); - if (!label) - return false; - if (!checkImports || label.dirty) - return label.dirty; - if (this.ambientTypesDirty) - return true; - var dependencies = alg.dijkstra(this.dependencyTree, id); - return some(dependencies, function (dependency, node) { - if (!node || dependency.distance === Infinity) - return false; - var l = _this.dependencyTree.node(node); - var dirty = l === undefined ? true : l.dirty; - if (dirty) - _this.context.debug(" import changed: " + node); - return dirty; - }); - }; - TsCache.prototype.makeName = function (id, snapshot) { - var data = snapshot.getText(0, snapshot.getLength()); - return sha1({ data: data, id: id }); - }; - return TsCache; +function convertDiagnostic(type, data) { + return map(data, function (diagnostic) { + var entry = { + flatMessage: flattenDiagnosticMessageText(diagnostic.messageText, "\n"), + category: diagnostic.category, + code: diagnostic.code, + type: type, + }; + if (diagnostic.file) { + var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; + entry.fileLine = diagnostic.file.fileName + " (" + (line + 1) + "," + (character + 1) + ")"; + } + return entry; + }); +} +var TsCache = (function () { + function TsCache(host, cache, options, rollupConfig, rootFilenames, context) { + var _this = this; + this.host = host; + this.options = options; + this.rollupConfig = rollupConfig; + this.context = context; + this.cacheVersion = "5"; + this.ambientTypesDirty = false; + this.cacheDir = cache + "/" + sha1({ + version: this.cacheVersion, + rootFilenames: rootFilenames, + options: this.options, + rollupConfig: this.rollupConfig, + tsVersion: version, + }); + this.dependencyTree = new Graph({ directed: true }); + this.dependencyTree.setDefaultNodeLabel(function (_node) { return ({ dirty: false }); }); + var automaticTypes = map(getAutomaticTypeDirectiveNames(options, sys), function (entry) { return resolveTypeReferenceDirective(entry, undefined, options, sys); }) + .filter(function (entry) { return entry.resolvedTypeReferenceDirective && entry.resolvedTypeReferenceDirective.resolvedFileName; }) + .map(function (entry) { return entry.resolvedTypeReferenceDirective.resolvedFileName; }); + this.ambientTypes = filter(rootFilenames, function (file) { return endsWith(file, ".d.ts"); }) + .concat(automaticTypes) + .map(function (id) { return ({ id: id, snapshot: _this.host.getScriptSnapshot(id) }); }); + this.init(); + this.checkAmbientTypes(); + } + TsCache.prototype.clean = function () { + this.context.info(blue("cleaning cache: " + this.cacheDir)); + emptyDirSync(this.cacheDir); + this.init(); + }; + TsCache.prototype.setDependency = function (importee, importer) { + // importee -> importer + this.context.debug(blue("dependency") + " '" + importee + "'"); + this.context.debug(" imported by '" + importer + "'"); + this.dependencyTree.setEdge(importer, importee); + }; + TsCache.prototype.walkTree = function (cb) { + var acyclic = alg.isAcyclic(this.dependencyTree); + if (acyclic) { + each(alg.topsort(this.dependencyTree), function (id) { return cb(id); }); + return; + } + this.context.info(yellow("import tree has cycles")); + each(this.dependencyTree.nodes(), function (id) { return cb(id); }); + }; + TsCache.prototype.done = function () { + this.context.info(blue("rolling caches")); + this.codeCache.roll(); + this.semanticDiagnosticsCache.roll(); + this.syntacticDiagnosticsCache.roll(); + this.typesCache.roll(); + }; + TsCache.prototype.getCompiled = function (id, snapshot, transform) { + var name = this.makeName(id, snapshot); + this.context.info(blue("transpiling") + " '" + id + "'"); + this.context.debug(" cache: '" + this.codeCache.path(name) + "'"); + if (!this.codeCache.exists(name) || this.isDirty(id, snapshot, false)) { + this.context.debug(yellow(" cache miss")); + var data_1 = transform(); + this.codeCache.write(name, data_1); + this.markAsDirty(id, snapshot); + return data_1; + } + this.context.debug(green(" cache hit")); + var data = this.codeCache.read(name); + this.codeCache.write(name, data); + return data; + }; + TsCache.prototype.getSyntacticDiagnostics = function (id, snapshot, check) { + return this.getDiagnostics("syntax", this.syntacticDiagnosticsCache, id, snapshot, check); + }; + TsCache.prototype.getSemanticDiagnostics = function (id, snapshot, check) { + return this.getDiagnostics("semantic", this.semanticDiagnosticsCache, id, snapshot, check); + }; + TsCache.prototype.checkAmbientTypes = function () { + var _this = this; + this.context.debug(blue("Ambient types:")); + var typeNames = filter(this.ambientTypes, function (snapshot) { return snapshot.snapshot !== undefined; }) + .map(function (snapshot) { + _this.context.debug(" " + snapshot.id); + return _this.makeName(snapshot.id, snapshot.snapshot); + }); + // types dirty if any d.ts changed, added or removed + this.ambientTypesDirty = !this.typesCache.match(typeNames); + if (this.ambientTypesDirty) + this.context.info(yellow("ambient types changed, redoing all semantic diagnostics")); + each(typeNames, function (name) { return _this.typesCache.touch(name); }); + }; + TsCache.prototype.getDiagnostics = function (type, cache, id, snapshot, check) { + var name = this.makeName(id, snapshot); + this.context.debug(" cache: '" + cache.path(name) + "'"); + if (!cache.exists(name) || this.isDirty(id, snapshot, true)) { + this.context.debug(yellow(" cache miss")); + var data_2 = convertDiagnostic(type, check()); + cache.write(name, data_2); + this.markAsDirty(id, snapshot); + return data_2; + } + this.context.debug(green(" cache hit")); + var data = cache.read(name); + cache.write(name, data); + return data; + }; + TsCache.prototype.init = function () { + this.codeCache = new RollingCache(this.cacheDir + "/code", true); + this.typesCache = new RollingCache(this.cacheDir + "/types", true); + this.syntacticDiagnosticsCache = new RollingCache(this.cacheDir + "/syntacticDiagnostics", true); + this.semanticDiagnosticsCache = new RollingCache(this.cacheDir + "/semanticDiagnostics", true); + }; + TsCache.prototype.markAsDirty = function (id, _snapshot) { + this.dependencyTree.setNode(id, { dirty: true }); + }; + // returns true if node or any of its imports or any of global types changed + TsCache.prototype.isDirty = function (id, _snapshot, checkImports) { + var _this = this; + var label = this.dependencyTree.node(id); + if (!label) + return false; + if (!checkImports || label.dirty) + return label.dirty; + if (this.ambientTypesDirty) + return true; + var dependencies = alg.dijkstra(this.dependencyTree, id); + return some(dependencies, function (dependency, node) { + if (!node || dependency.distance === Infinity) + return false; + var l = _this.dependencyTree.node(node); + var dirty = l === undefined ? true : l.dirty; + if (dirty) + _this.context.debug(" import changed: " + node); + return dirty; + }); + }; + TsCache.prototype.makeName = function (id, snapshot) { + var data = snapshot.getText(0, snapshot.getLength()); + return sha1({ data: data, id: id }); + }; + return TsCache; }()); -// tslint:disable-next-line:no-var-requires -var createFilter = require("rollup-pluginutils").createFilter; -function getOptionsOverrides() { - return { - module: ModuleKind.ES2015, - noEmitHelpers: true, - importHelpers: true, - noResolve: false, - }; -} -// The injected id for helpers. -var TSLIB = "tslib"; -var tslibSource; -try { - // tslint:disable-next-line:no-string-literal no-var-requires - var tslibPath = require.resolve("tslib/" + require("tslib/package.json")["module"]); - tslibSource = readFileSync(tslibPath, "utf8"); -} -catch (e) { - console.warn("Error loading `tslib` helper library."); - throw e; -} -function parseTsConfig(context) { - var fileName = findConfigFile(process.cwd(), sys.fileExists, "tsconfig.json"); - if (!fileName) - throw new Error("couldn't find 'tsconfig.json' in " + process.cwd()); - var text = sys.readFile(fileName); - var result = parseConfigFileTextToJson(fileName, text); - if (result.error) { - printDiagnostics(context, convertDiagnostic("config", [result.error])); - throw new Error("failed to parse " + fileName); - } - var configParseResult = parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(), fileName); - return configParseResult; -} -function printDiagnostics(context, diagnostics) { - each(diagnostics, function (diagnostic) { - var print; - var color; - var category; - switch (diagnostic.category) { - case DiagnosticCategory.Message: - print = context.info; - color = white; - category = ""; - break; - case DiagnosticCategory.Error: - print = context.error; - color = red; - category = "error"; - break; - case DiagnosticCategory.Warning: - default: - print = context.warn; - color = yellow; - category = "warning"; - break; - } - // const type = ""; - var type = diagnostic.type + " "; - if (diagnostic.fileLine) - print.call(context, [diagnostic.fileLine + ": " + type + category + " TS" + diagnostic.code + " " + color(diagnostic.flatMessage)]); - else - print.call(context, ["" + type + category + " TS" + diagnostic.code + " " + color(diagnostic.flatMessage)]); - }); -} -function typescript(options) { - options = __assign({}, options); - defaults(options, { - check: true, - verbosity: VerbosityLevel.Warning, - clean: false, - cacheRoot: process.cwd() + "/.rpt2_cache", - include: ["*.ts+(|x)", "**/*.ts+(|x)"], - exclude: ["*.d.ts", "**/*.d.ts"], - abortOnError: true, - rollupCommonJSResolveHack: false, - }); - var rollupConfig; - var watchMode = false; - var round = 0; - var targetCount = 0; - var context = new ConsoleContext(options.verbosity, "rpt2: "); - context.info("Typescript version: " + version); - context.debug("Options: " + JSON.stringify(options, undefined, 4)); - var filter$$1 = createFilter(options.include, options.exclude); - var parsedConfig = parseTsConfig(context); - var servicesHost = new LanguageServiceHost(parsedConfig); - var service = createLanguageService(servicesHost, createDocumentRegistry()); - var _cache; - var cache = function () { - if (!_cache) - _cache = new TsCache(servicesHost, options.cacheRoot, parsedConfig.options, rollupConfig, parsedConfig.fileNames, context); - return _cache; - }; - var noErrors = true; - // printing compiler option errors - if (options.check) - printDiagnostics(context, convertDiagnostic("options", service.getCompilerOptionsDiagnostics())); - return { - options: function (config) { - rollupConfig = config; - context.debug("rollupConfig: " + JSON.stringify(rollupConfig, undefined, 4)); - if (options.clean) - cache().clean(); - }, - resolveId: function (importee, importer) { - if (importee === TSLIB) - return "\0" + TSLIB; - if (!importer) - return null; - importer = importer.split("\\").join("/"); - // TODO: use module resolution cache - var result = nodeModuleNameResolver(importee, importer, parsedConfig.options, sys); - if (result.resolvedModule && result.resolvedModule.resolvedFileName) { - if (filter$$1(result.resolvedModule.resolvedFileName)) - cache().setDependency(result.resolvedModule.resolvedFileName, importer); - if (endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) - return null; - var resolved = options.rollupCommonJSResolveHack - ? sync(result.resolvedModule.resolvedFileName) - : result.resolvedModule.resolvedFileName; - context.debug(blue("resolving") + " '" + importee + "'"); - context.debug(" to '" + resolved + "'"); - return resolved; - } - return null; - }, - load: function (id) { - if (id === "\0" + TSLIB) - return tslibSource; - return undefined; - }, - transform: function (code, id) { - var _this = this; - if (!filter$$1(id)) - return undefined; - var contextWrapper = new RollupContext(options.verbosity, options.abortOnError, this, "rpt2: "); - var snapshot = servicesHost.setSnapshot(id, code); - // getting compiled file from cache or from ts - var result = cache().getCompiled(id, snapshot, function () { - var output = service.getEmitOutput(id); - if (output.emitSkipped) { - noErrors = false; - // always checking on fatal errors, even if options.check is set to false - var diagnostics = concat(cache().getSyntacticDiagnostics(id, snapshot, function () { - return service.getSyntacticDiagnostics(id); - }), cache().getSemanticDiagnostics(id, snapshot, function () { - return service.getSemanticDiagnostics(id); - })); - printDiagnostics(contextWrapper, diagnostics); - // since no output was generated, aborting compilation - cache().done(); - if (isFunction(_this.error)) - _this.error(red("failed to transpile '" + id + "'")); - } - var transpiled = find(output.outputFiles, function (entry) { return endsWith(entry.name, ".js"); }); - var map$$1 = find(output.outputFiles, function (entry) { return endsWith(entry.name, ".map"); }); - return { - code: transpiled ? transpiled.text : undefined, - map: map$$1 ? JSON.parse(map$$1.text) : { mappings: "" }, - }; - }); - if (options.check) { - var diagnostics = concat(cache().getSyntacticDiagnostics(id, snapshot, function () { - return service.getSyntacticDiagnostics(id); - }), cache().getSemanticDiagnostics(id, snapshot, function () { - return service.getSemanticDiagnostics(id); - })); - if (diagnostics.length > 0) - noErrors = false; - printDiagnostics(contextWrapper, diagnostics); - } - return result; - }, - ongenerate: function (bundleOptions) { - targetCount = get(bundleOptions, "targets.length", 1); - if (round >= targetCount) { - watchMode = true; - round = 0; - } - context.debug("generating target " + (round + 1) + " of " + targetCount); - if (watchMode && round === 0) { - context.debug("running in watch mode"); - cache().walkTree(function (id) { - var diagnostics = concat(convertDiagnostic("syntax", service.getSyntacticDiagnostics(id)), convertDiagnostic("semantic", service.getSemanticDiagnostics(id))); - printDiagnostics(context, diagnostics); - }); - } - if (!watchMode && !noErrors) - context.info(yellow("there were errors or warnings above.")); - cache().done(); - round++; - }, - }; +// tslint:disable-next-line:no-var-requires +var createFilter = require("rollup-pluginutils").createFilter; +function getOptionsOverrides() { + return { + module: ModuleKind.ES2015, + noEmitHelpers: true, + importHelpers: true, + noResolve: false, + }; +} +// The injected id for helpers. +var TSLIB = "tslib"; +var tslibSource; +try { + // tslint:disable-next-line:no-string-literal no-var-requires + var tslibPath = require.resolve("tslib/" + require("tslib/package.json")["module"]); + tslibSource = readFileSync(tslibPath, "utf8"); +} +catch (e) { + console.warn("Error loading `tslib` helper library."); + throw e; +} +function parseTsConfig(context) { + var fileName = findConfigFile(process.cwd(), sys.fileExists, "tsconfig.json"); + if (!fileName) + throw new Error("couldn't find 'tsconfig.json' in " + process.cwd()); + var text = sys.readFile(fileName); + var result = parseConfigFileTextToJson(fileName, text); + if (result.error) { + printDiagnostics(context, convertDiagnostic("config", [result.error])); + throw new Error("failed to parse " + fileName); + } + var configParseResult = parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(), fileName); + return configParseResult; +} +function printDiagnostics(context, diagnostics) { + each(diagnostics, function (diagnostic) { + var print; + var color; + var category; + switch (diagnostic.category) { + case DiagnosticCategory.Message: + print = context.info; + color = white; + category = ""; + break; + case DiagnosticCategory.Error: + print = context.error; + color = red; + category = "error"; + break; + case DiagnosticCategory.Warning: + default: + print = context.warn; + color = yellow; + category = "warning"; + break; + } + // const type = ""; + var type = diagnostic.type + " "; + if (diagnostic.fileLine) + print.call(context, [diagnostic.fileLine + ": " + type + category + " TS" + diagnostic.code + " " + color(diagnostic.flatMessage)]); + else + print.call(context, ["" + type + category + " TS" + diagnostic.code + " " + color(diagnostic.flatMessage)]); + }); +} +function typescript(options) { + options = __assign({}, options); + defaults(options, { + check: true, + verbosity: VerbosityLevel.Warning, + clean: false, + cacheRoot: process.cwd() + "/.rpt2_cache", + include: ["*.ts+(|x)", "**/*.ts+(|x)"], + exclude: ["*.d.ts", "**/*.d.ts"], + abortOnError: true, + rollupCommonJSResolveHack: false, + }); + var rollupConfig; + var watchMode = false; + var round = 0; + var targetCount = 0; + var context = new ConsoleContext(options.verbosity, "rpt2: "); + context.info("Typescript version: " + version); + context.debug("Options: " + JSON.stringify(options, undefined, 4)); + var filter$$1 = createFilter(options.include, options.exclude); + var parsedConfig = parseTsConfig(context); + var servicesHost = new LanguageServiceHost(parsedConfig); + var service = createLanguageService(servicesHost, createDocumentRegistry()); + var _cache; + var cache = function () { + if (!_cache) + _cache = new TsCache(servicesHost, options.cacheRoot, parsedConfig.options, rollupConfig, parsedConfig.fileNames, context); + return _cache; + }; + var noErrors = true; + var declarations = []; + // printing compiler option errors + if (options.check) + printDiagnostics(context, convertDiagnostic("options", service.getCompilerOptionsDiagnostics())); + return { + options: function (config) { + rollupConfig = config; + context.debug("rollupConfig: " + JSON.stringify(rollupConfig, undefined, 4)); + if (options.clean) + cache().clean(); + }, + resolveId: function (importee, importer) { + if (importee === TSLIB) + return "\0" + TSLIB; + if (!importer) + return null; + importer = importer.split("\\").join("/"); + // TODO: use module resolution cache + var result = nodeModuleNameResolver(importee, importer, parsedConfig.options, sys); + if (result.resolvedModule && result.resolvedModule.resolvedFileName) { + if (filter$$1(result.resolvedModule.resolvedFileName)) + cache().setDependency(result.resolvedModule.resolvedFileName, importer); + if (endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) + return null; + var resolved = options.rollupCommonJSResolveHack + ? sync(result.resolvedModule.resolvedFileName) + : result.resolvedModule.resolvedFileName; + context.debug(blue("resolving") + " '" + importee + "'"); + context.debug(" to '" + resolved + "'"); + return resolved; + } + return null; + }, + load: function (id) { + if (id === "\0" + TSLIB) + return tslibSource; + return undefined; + }, + transform: function (code, id) { + var _this = this; + if (!filter$$1(id)) + return undefined; + var contextWrapper = new RollupContext(options.verbosity, options.abortOnError, this, "rpt2: "); + var snapshot = servicesHost.setSnapshot(id, code); + // getting compiled file from cache or from ts + var result = cache().getCompiled(id, snapshot, function () { + var output = service.getEmitOutput(id); + if (output.emitSkipped) { + noErrors = false; + // always checking on fatal errors, even if options.check is set to false + var diagnostics = concat(cache().getSyntacticDiagnostics(id, snapshot, function () { + return service.getSyntacticDiagnostics(id); + }), cache().getSemanticDiagnostics(id, snapshot, function () { + return service.getSemanticDiagnostics(id); + })); + printDiagnostics(contextWrapper, diagnostics); + // since no output was generated, aborting compilation + cache().done(); + if (isFunction(_this.error)) + _this.error(red("failed to transpile '" + id + "'")); + } + var transpiled = find(output.outputFiles, function (entry) { return endsWith(entry.name, ".js"); }); + var map$$1 = find(output.outputFiles, function (entry) { return endsWith(entry.name, ".map"); }); + var dts = find(output.outputFiles, function (entry) { return endsWith(entry.name, ".d.ts"); }); + return { + code: transpiled ? transpiled.text : undefined, + map: map$$1 ? JSON.parse(map$$1.text) : { mappings: "" }, + dts: dts, + }; + }); + if (options.check) { + var diagnostics = concat(cache().getSyntacticDiagnostics(id, snapshot, function () { + return service.getSyntacticDiagnostics(id); + }), cache().getSemanticDiagnostics(id, snapshot, function () { + return service.getSemanticDiagnostics(id); + })); + if (diagnostics.length > 0) + noErrors = false; + printDiagnostics(contextWrapper, diagnostics); + } + if (result && result.dts) { + declarations.push(result.dts); + } + return result; + }, + ongenerate: function (bundleOptions) { + targetCount = get(bundleOptions, "targets.length", 1); + if (round >= targetCount) { + watchMode = true; + round = 0; + } + context.debug("generating target " + (round + 1) + " of " + targetCount); + if (watchMode && round === 0) { + context.debug("running in watch mode"); + cache().walkTree(function (id) { + var diagnostics = concat(convertDiagnostic("syntax", service.getSyntacticDiagnostics(id)), convertDiagnostic("semantic", service.getSemanticDiagnostics(id))); + printDiagnostics(context, diagnostics); + }); + } + if (!watchMode && !noErrors) + context.info(yellow("there were errors or warnings above.")); + cache().done(); + round++; + }, + onwrite: function () { + declarations.forEach(function (_a) { + var name = _a.name, text = _a.text, writeByteOrderMark = _a.writeByteOrderMark; + sys.writeFile(name, text, writeByteOrderMark); + }); + }, + }; } export default typescript; diff --git a/src/index.ts b/src/index.ts index 98cdbed..08f3e76 100644 --- a/src/index.ts +++ b/src/index.ts @@ -152,6 +152,8 @@ export default function typescript(options: IOptions) let noErrors = true; + const declarations: ts.OutputFile[] = []; + // printing compiler option errors if (options.check) printDiagnostics(context, convertDiagnostic("options", service.getCompilerOptionsDiagnostics())); @@ -249,10 +251,12 @@ export default function typescript(options: IOptions) const transpiled = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".js") ); const map = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".map") ); + const dts = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".d.ts")); return { code: transpiled ? transpiled.text : undefined, map: map ? JSON.parse(map.text) : { mappings: "" }, + dts, }; }); @@ -275,6 +279,10 @@ export default function typescript(options: IOptions) printDiagnostics(contextWrapper, diagnostics); } + if (result && result.dts) { + declarations.push(result.dts); + } + return result; }, @@ -311,5 +319,11 @@ export default function typescript(options: IOptions) round++; }, + + onwrite() { + declarations.forEach(({ name, text, writeByteOrderMark }) => { + ts.sys.writeFile(name, text, writeByteOrderMark); + }); + }, }; } diff --git a/src/tscache.ts b/src/tscache.ts index 7bbee3d..a41297f 100644 --- a/src/tscache.ts +++ b/src/tscache.ts @@ -12,6 +12,7 @@ export interface ICode { code: string | undefined; map: string | undefined; + dts?: ts.OutputFile | undefined; } interface INodeLabel