From ff7cb729290887d2897989a06681122add368c50 Mon Sep 17 00:00:00 2001 From: ezolenko Date: Sun, 12 Mar 2017 18:37:42 -0600 Subject: [PATCH] - partial fix for watch mode (#6) --- .gitignore | 2 +- dist/rollup-plugin-typescript2.cjs.js | 114 ++++++++++++++----------- dist/rollup-plugin-typescript2.es.js | 116 +++++++++++++++----------- package.json | 11 +-- src/host.ts | 4 +- src/icache.ts | 16 ++++ src/index.ts | 39 +++++---- src/rollingcache.ts | 27 +++--- src/{cache.ts => tscache.ts} | 22 ++--- 9 files changed, 208 insertions(+), 143 deletions(-) create mode 100644 src/icache.ts rename src/{cache.ts => tscache.ts} (92%) diff --git a/.gitignore b/.gitignore index 3c97547b..0b325ffe 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /node_modules /npm-debug.log /typings -/.rts2_cache \ No newline at end of file +/.rpt2_cache \ No newline at end of file diff --git a/dist/rollup-plugin-typescript2.cjs.js b/dist/rollup-plugin-typescript2.cjs.js index a274c4e7..ae28392e 100644 --- a/dist/rollup-plugin-typescript2.cjs.js +++ b/dist/rollup-plugin-typescript2.cjs.js @@ -72,6 +72,8 @@ var ConsoleContext = (function () { return ConsoleContext; }()); +//# sourceMappingURL=context.js.map + var RollupContext = (function () { function RollupContext(verbosity, bail, context, prefix) { if (prefix === void 0) { prefix = ""; } @@ -106,6 +108,8 @@ var RollupContext = (function () { return RollupContext; }()); +//# sourceMappingURL=rollupcontext.js.map + var LanguageServiceHost = (function () { function LanguageServiceHost(parsedConfig) { this.parsedConfig = parsedConfig; @@ -131,8 +135,8 @@ var LanguageServiceHost = (function () { LanguageServiceHost.prototype.getCurrentDirectory = function () { return this.cwd; }; - LanguageServiceHost.prototype.getScriptVersion = function (_fileName) { - return (this.versions[_fileName] || 0).toString(); + LanguageServiceHost.prototype.getScriptVersion = function (fileName) { + return (this.versions[fileName] || 0).toString(); }; LanguageServiceHost.prototype.getScriptFileNames = function () { return this.parsedConfig.fileNames; @@ -146,10 +150,8 @@ var LanguageServiceHost = (function () { 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. - */ +//# sourceMappingURL=host.js.map + var RollingCache = (function () { /** * @param cacheRoot: root folder for the cache @@ -158,6 +160,7 @@ var RollingCache = (function () { 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); @@ -192,29 +195,32 @@ var RollingCache = (function () { RollingCache.prototype.write = function (name, data) { if (data === undefined) return; - if (this.checkNewCache) - fs.writeJsonSync(this.newCacheRoot + "/" + name, data); + if (this.rolled) + fs.writeJsonSync(this.oldCacheRoot + "/" + name, data); else - fs.writeJson(this.newCacheRoot + "/" + name, data, { encoding: "utf8" }, function () { }); + fs.writeJsonSync(this.newCacheRoot + "/" + name, data); }; RollingCache.prototype.touch = function (name) { - if (this.checkNewCache) - fs.ensureFileSync(this.newCacheRoot + "/" + name); + if (this.rolled) + fs.ensureFileSync(this.oldCacheRoot + "/" + name); else - fs.ensureFile(this.newCacheRoot + "/" + name, function () { }); + fs.ensureFileSync(this.newCacheRoot + "/" + name); }; /** * clears old cache and moves new in its place */ RollingCache.prototype.roll = function () { - var _this = this; - fs.remove(this.oldCacheRoot, function () { - fs.move(_this.newCacheRoot, _this.oldCacheRoot, function () { }); - }); + if (this.rolled) + return; + this.rolled = true; + fs.removeSync(this.oldCacheRoot); + fs.move(this.newCacheRoot, this.oldCacheRoot, function () { }); }; return RollingCache; }()); +//# sourceMappingURL=rollingcache.js.map + function convertDiagnostic(data) { return _.map(data, function (diagnostic) { var entry = { @@ -228,13 +234,13 @@ function convertDiagnostic(data) { return entry; }); } -var Cache = (function () { - function Cache(host, cache, options, rootFilenames, context) { +var TsCache = (function () { + function TsCache(host, cache, options, rootFilenames, context) { var _this = this; this.host = host; this.options = options; this.context = context; - this.cacheVersion = "2"; + this.cacheVersion = "3"; this.ambientTypesDirty = false; this.cacheDir = cache + "/" + hash.sha1({ version: this.cacheVersion, @@ -253,25 +259,25 @@ var Cache = (function () { this.init(); this.checkAmbientTypes(); } - Cache.prototype.clean = function () { + TsCache.prototype.clean = function () { this.context.info(colors.blue("cleaning cache: " + this.cacheDir)); fs.emptyDirSync(this.cacheDir); this.init(); }; - Cache.prototype.setDependency = function (importee, importer) { + 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); }; - Cache.prototype.done = function () { + TsCache.prototype.done = function () { this.context.info(colors.blue("rolling caches")); this.codeCache.roll(); this.semanticDiagnosticsCache.roll(); this.syntacticDiagnosticsCache.roll(); this.typesCache.roll(); }; - Cache.prototype.getCompiled = function (id, snapshot, transform) { + 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) + "'"); @@ -287,13 +293,13 @@ var Cache = (function () { this.codeCache.write(name, data); return data; }; - Cache.prototype.getSyntacticDiagnostics = function (id, snapshot, check) { + TsCache.prototype.getSyntacticDiagnostics = function (id, snapshot, check) { return this.getDiagnostics(this.syntacticDiagnosticsCache, id, snapshot, check); }; - Cache.prototype.getSemanticDiagnostics = function (id, snapshot, check) { + TsCache.prototype.getSemanticDiagnostics = function (id, snapshot, check) { return this.getDiagnostics(this.semanticDiagnosticsCache, id, snapshot, check); }; - Cache.prototype.checkAmbientTypes = function () { + 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; }) @@ -307,7 +313,7 @@ var Cache = (function () { this.context.info(colors.yellow("ambient types changed, redoing all semantic diagnostics")); _.each(typeNames, function (name) { return _this.typesCache.touch(name); }); }; - Cache.prototype.getDiagnostics = function (cache, id, snapshot, check) { + TsCache.prototype.getDiagnostics = function (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)) { @@ -322,17 +328,17 @@ var Cache = (function () { cache.write(name, data); return data; }; - Cache.prototype.init = function () { + TsCache.prototype.init = function () { this.codeCache = new RollingCache(this.cacheDir + "/code", true); - this.typesCache = new RollingCache(this.cacheDir + "/types", false); - this.syntacticDiagnosticsCache = new RollingCache(this.cacheDir + "/syntacticDiagnostics", false); - this.semanticDiagnosticsCache = new RollingCache(this.cacheDir + "/semanticDiagnostics", false); + this.typesCache = new RollingCache(this.cacheDir + "/types", true); + this.syntacticDiagnosticsCache = new RollingCache(this.cacheDir + "/syntacticDiagnostics", true); + this.semanticDiagnosticsCache = new RollingCache(this.cacheDir + "/semanticDiagnostics", true); }; - Cache.prototype.markAsDirty = function (id, _snapshot) { + 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 - Cache.prototype.isDirty = function (id, _snapshot, checkImports) { + TsCache.prototype.isDirty = function (id, _snapshot, checkImports) { var _this = this; var label = this.dependencyTree.node(id); if (!label) @@ -352,14 +358,15 @@ var Cache = (function () { return dirty; }); }; - Cache.prototype.makeName = function (id, snapshot) { + TsCache.prototype.makeName = function (id, snapshot) { var data = snapshot.getText(0, snapshot.getLength()); return hash.sha1({ data: data, id: id }); }; - return Cache; + return TsCache; }()); -// tslint:disable-next-line:no-var-requires +//# sourceMappingURL=tscache.js.map + var createFilter = require("rollup-pluginutils").createFilter; function getOptionsOverrides() { return { @@ -432,20 +439,22 @@ function typescript(options) { abortOnError: true, rollupCommonJSResolveHack: false, }); + var watchMode = false; + var round = 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 services = ts.createLanguageService(servicesHost, ts.createDocumentRegistry()); - var cache = new Cache(servicesHost, options.cacheRoot, parsedConfig.options, parsedConfig.fileNames, context); - var cleanTranspile = true; + var service = ts.createLanguageService(servicesHost, ts.createDocumentRegistry()); + var cache = new TsCache(servicesHost, options.cacheRoot, parsedConfig.options, parsedConfig.fileNames, context); + var noErrors = true; if (options.clean) cache.clean(); // printing compiler option errors if (options.check) - printDiagnostics(context, convertDiagnostic(services.getCompilerOptionsDiagnostics())); + printDiagnostics(context, convertDiagnostic(service.getCompilerOptionsDiagnostics())); return { resolveId: function (importee, importer) { if (importee === TSLIB) @@ -481,14 +490,14 @@ function typescript(options) { var snapshot = servicesHost.setSnapshot(id, code); // getting compiled file from cache or from ts var result = cache.getCompiled(id, snapshot, function () { - var output = services.getEmitOutput(id); + var output = service.getEmitOutput(id); if (output.emitSkipped) { - cleanTranspile = false; + noErrors = false; // always checking on fatal errors, even if options.check is set to false var diagnostics = cache.getSyntacticDiagnostics(id, snapshot, function () { - return services.getSyntacticDiagnostics(id); + return service.getSyntacticDiagnostics(id); }).concat(cache.getSemanticDiagnostics(id, snapshot, function () { - return services.getSemanticDiagnostics(id); + return service.getSemanticDiagnostics(id); })); printDiagnostics(contextWrapper, diagnostics); // since no output was generated, aborting compilation @@ -503,22 +512,29 @@ function typescript(options) { }); if (options.check) { var diagnostics = cache.getSyntacticDiagnostics(id, snapshot, function () { - return services.getSyntacticDiagnostics(id); + return service.getSyntacticDiagnostics(id); }).concat(cache.getSemanticDiagnostics(id, snapshot, function () { - return services.getSemanticDiagnostics(id); + return service.getSemanticDiagnostics(id); })); if (diagnostics.length !== 0) - cleanTranspile = false; + noErrors = false; printDiagnostics(contextWrapper, diagnostics); } return result; }, ongenerate: function () { - cache.done(); - if (!cleanTranspile) + if (watchMode) + context.debug("running in watch mode"); + else + cache.done(); + if (!noErrors) context.info(colors.yellow("there were errors or warnings above.")); + noErrors = true; + watchMode = true; + round++; }, }; } +//# sourceMappingURL=index.js.map module.exports = typescript; diff --git a/dist/rollup-plugin-typescript2.es.js b/dist/rollup-plugin-typescript2.es.js index 6c0be4f5..95f14afd 100644 --- a/dist/rollup-plugin-typescript2.es.js +++ b/dist/rollup-plugin-typescript2.es.js @@ -1,5 +1,5 @@ /* eslint-disable */ -import { emptyDirSync, ensureFile, ensureFileSync, existsSync, move, readFileSync, readJsonSync, readdirSync, remove, writeJson, writeJsonSync } from 'fs-extra'; +import { emptyDirSync, ensureFileSync, existsSync, move, readFileSync, readJsonSync, readdirSync, removeSync, writeJsonSync } from 'fs-extra'; import * as fs from 'fs-extra'; import { DiagnosticCategory, ModuleKind, ScriptSnapshot, createDocumentRegistry, createLanguageService, findConfigFile, flattenDiagnosticMessageText, getAutomaticTypeDirectiveNames, getDefaultLibFilePath, nodeModuleNameResolver, parseConfigFileTextToJson, parseJsonConfigFileContent, resolveTypeReferenceDirective, sys, version } from 'typescript'; import * as ts from 'typescript'; @@ -78,6 +78,8 @@ var ConsoleContext = (function () { return ConsoleContext; }()); +//# sourceMappingURL=context.js.map + var RollupContext = (function () { function RollupContext(verbosity, bail, context, prefix) { if (prefix === void 0) { prefix = ""; } @@ -112,6 +114,8 @@ var RollupContext = (function () { return RollupContext; }()); +//# sourceMappingURL=rollupcontext.js.map + var LanguageServiceHost = (function () { function LanguageServiceHost(parsedConfig) { this.parsedConfig = parsedConfig; @@ -137,8 +141,8 @@ var LanguageServiceHost = (function () { LanguageServiceHost.prototype.getCurrentDirectory = function () { return this.cwd; }; - LanguageServiceHost.prototype.getScriptVersion = function (_fileName) { - return (this.versions[_fileName] || 0).toString(); + LanguageServiceHost.prototype.getScriptVersion = function (fileName) { + return (this.versions[fileName] || 0).toString(); }; LanguageServiceHost.prototype.getScriptFileNames = function () { return this.parsedConfig.fileNames; @@ -152,10 +156,8 @@ var LanguageServiceHost = (function () { 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. - */ +//# sourceMappingURL=host.js.map + var RollingCache = (function () { /** * @param cacheRoot: root folder for the cache @@ -164,6 +166,7 @@ var RollingCache = (function () { 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); @@ -198,29 +201,32 @@ var RollingCache = (function () { RollingCache.prototype.write = function (name, data) { if (data === undefined) return; - if (this.checkNewCache) - writeJsonSync(this.newCacheRoot + "/" + name, data); + if (this.rolled) + writeJsonSync(this.oldCacheRoot + "/" + name, data); else - writeJson(this.newCacheRoot + "/" + name, data, { encoding: "utf8" }, function () { }); + writeJsonSync(this.newCacheRoot + "/" + name, data); }; RollingCache.prototype.touch = function (name) { - if (this.checkNewCache) - ensureFileSync(this.newCacheRoot + "/" + name); + if (this.rolled) + ensureFileSync(this.oldCacheRoot + "/" + name); else - ensureFile(this.newCacheRoot + "/" + name, function () { }); + ensureFileSync(this.newCacheRoot + "/" + name); }; /** * clears old cache and moves new in its place */ RollingCache.prototype.roll = function () { - var _this = this; - remove(this.oldCacheRoot, function () { - move(_this.newCacheRoot, _this.oldCacheRoot, function () { }); - }); + if (this.rolled) + return; + this.rolled = true; + removeSync(this.oldCacheRoot); + move(this.newCacheRoot, this.oldCacheRoot, function () { }); }; return RollingCache; }()); +//# sourceMappingURL=rollingcache.js.map + function convertDiagnostic(data) { return map(data, function (diagnostic) { var entry = { @@ -234,13 +240,13 @@ function convertDiagnostic(data) { return entry; }); } -var Cache = (function () { - function Cache(host, cache, options, rootFilenames, context) { +var TsCache = (function () { + function TsCache(host, cache, options, rootFilenames, context) { var _this = this; this.host = host; this.options = options; this.context = context; - this.cacheVersion = "2"; + this.cacheVersion = "3"; this.ambientTypesDirty = false; this.cacheDir = cache + "/" + sha1({ version: this.cacheVersion, @@ -259,25 +265,25 @@ var Cache = (function () { this.init(); this.checkAmbientTypes(); } - Cache.prototype.clean = function () { + TsCache.prototype.clean = function () { this.context.info(blue("cleaning cache: " + this.cacheDir)); emptyDirSync(this.cacheDir); this.init(); }; - Cache.prototype.setDependency = function (importee, importer) { + 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); }; - Cache.prototype.done = function () { + TsCache.prototype.done = function () { this.context.info(blue("rolling caches")); this.codeCache.roll(); this.semanticDiagnosticsCache.roll(); this.syntacticDiagnosticsCache.roll(); this.typesCache.roll(); }; - Cache.prototype.getCompiled = function (id, snapshot, transform) { + 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) + "'"); @@ -293,13 +299,13 @@ var Cache = (function () { this.codeCache.write(name, data); return data; }; - Cache.prototype.getSyntacticDiagnostics = function (id, snapshot, check) { + TsCache.prototype.getSyntacticDiagnostics = function (id, snapshot, check) { return this.getDiagnostics(this.syntacticDiagnosticsCache, id, snapshot, check); }; - Cache.prototype.getSemanticDiagnostics = function (id, snapshot, check) { + TsCache.prototype.getSemanticDiagnostics = function (id, snapshot, check) { return this.getDiagnostics(this.semanticDiagnosticsCache, id, snapshot, check); }; - Cache.prototype.checkAmbientTypes = function () { + TsCache.prototype.checkAmbientTypes = function () { var _this = this; this.context.debug(blue("Ambient types:")); var typeNames = filter(this.ambientTypes, function (snapshot) { return snapshot.snapshot !== undefined; }) @@ -313,7 +319,7 @@ var Cache = (function () { this.context.info(yellow("ambient types changed, redoing all semantic diagnostics")); each(typeNames, function (name) { return _this.typesCache.touch(name); }); }; - Cache.prototype.getDiagnostics = function (cache, id, snapshot, check) { + TsCache.prototype.getDiagnostics = function (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)) { @@ -328,17 +334,17 @@ var Cache = (function () { cache.write(name, data); return data; }; - Cache.prototype.init = function () { + TsCache.prototype.init = function () { this.codeCache = new RollingCache(this.cacheDir + "/code", true); - this.typesCache = new RollingCache(this.cacheDir + "/types", false); - this.syntacticDiagnosticsCache = new RollingCache(this.cacheDir + "/syntacticDiagnostics", false); - this.semanticDiagnosticsCache = new RollingCache(this.cacheDir + "/semanticDiagnostics", false); + this.typesCache = new RollingCache(this.cacheDir + "/types", true); + this.syntacticDiagnosticsCache = new RollingCache(this.cacheDir + "/syntacticDiagnostics", true); + this.semanticDiagnosticsCache = new RollingCache(this.cacheDir + "/semanticDiagnostics", true); }; - Cache.prototype.markAsDirty = function (id, _snapshot) { + 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 - Cache.prototype.isDirty = function (id, _snapshot, checkImports) { + TsCache.prototype.isDirty = function (id, _snapshot, checkImports) { var _this = this; var label = this.dependencyTree.node(id); if (!label) @@ -358,14 +364,15 @@ var Cache = (function () { return dirty; }); }; - Cache.prototype.makeName = function (id, snapshot) { + TsCache.prototype.makeName = function (id, snapshot) { var data = snapshot.getText(0, snapshot.getLength()); return sha1({ data: data, id: id }); }; - return Cache; + return TsCache; }()); -// tslint:disable-next-line:no-var-requires +//# sourceMappingURL=tscache.js.map + var createFilter = require("rollup-pluginutils").createFilter; function getOptionsOverrides() { return { @@ -438,20 +445,22 @@ function typescript(options) { abortOnError: true, rollupCommonJSResolveHack: false, }); + var watchMode = false; + var round = 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 services = createLanguageService(servicesHost, createDocumentRegistry()); - var cache = new Cache(servicesHost, options.cacheRoot, parsedConfig.options, parsedConfig.fileNames, context); - var cleanTranspile = true; + var service = createLanguageService(servicesHost, createDocumentRegistry()); + var cache = new TsCache(servicesHost, options.cacheRoot, parsedConfig.options, parsedConfig.fileNames, context); + var noErrors = true; if (options.clean) cache.clean(); // printing compiler option errors if (options.check) - printDiagnostics(context, convertDiagnostic(services.getCompilerOptionsDiagnostics())); + printDiagnostics(context, convertDiagnostic(service.getCompilerOptionsDiagnostics())); return { resolveId: function (importee, importer) { if (importee === TSLIB) @@ -487,14 +496,14 @@ function typescript(options) { var snapshot = servicesHost.setSnapshot(id, code); // getting compiled file from cache or from ts var result = cache.getCompiled(id, snapshot, function () { - var output = services.getEmitOutput(id); + var output = service.getEmitOutput(id); if (output.emitSkipped) { - cleanTranspile = false; + noErrors = false; // always checking on fatal errors, even if options.check is set to false var diagnostics = cache.getSyntacticDiagnostics(id, snapshot, function () { - return services.getSyntacticDiagnostics(id); + return service.getSyntacticDiagnostics(id); }).concat(cache.getSemanticDiagnostics(id, snapshot, function () { - return services.getSemanticDiagnostics(id); + return service.getSemanticDiagnostics(id); })); printDiagnostics(contextWrapper, diagnostics); // since no output was generated, aborting compilation @@ -509,22 +518,29 @@ function typescript(options) { }); if (options.check) { var diagnostics = cache.getSyntacticDiagnostics(id, snapshot, function () { - return services.getSyntacticDiagnostics(id); + return service.getSyntacticDiagnostics(id); }).concat(cache.getSemanticDiagnostics(id, snapshot, function () { - return services.getSemanticDiagnostics(id); + return service.getSemanticDiagnostics(id); })); if (diagnostics.length !== 0) - cleanTranspile = false; + noErrors = false; printDiagnostics(contextWrapper, diagnostics); } return result; }, ongenerate: function () { - cache.done(); - if (!cleanTranspile) + if (watchMode) + context.debug("running in watch mode"); + else + cache.done(); + if (!noErrors) context.info(yellow("there were errors or warnings above.")); + noErrors = true; + watchMode = true; + round++; }, }; } +//# sourceMappingURL=index.js.map export default typescript; diff --git a/package.json b/package.json index 3f8a9782..84d254c8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-typescript2", - "version": "0.3.0", + "version": "0.4.0", "description": "Seamless integration between Rollup and TypeScript. Now with errors.", "main": "dist/rollup-plugin-typescript2.cjs.js", "module": "dist/rollup-plugin-typescript2.es.js", @@ -43,13 +43,14 @@ "@types/node": "^6.0.53", "@types/object-hash": "^0.5.28", "@types/resolve": "0.0.4", + "resolve": "^1.1.7", "rimraf": "^2.5.4", "rollup": "^0.41.4", - "rollup-plugin-typescript2": "^0.2.2", + "rollup-plugin-typescript2": "^0.3.0", + "rollup-watch": "^3.2.2", + "tslib": "^1.6.0", "tslint": "^4.5.1", - "typescript": "^2.2.1", - "resolve": "^1.1.7", - "tslib": "^1.6.0" + "typescript": "^2.2.1" }, "repository": { "type": "git", diff --git a/src/host.ts b/src/host.ts index a9f873df..cbc252d8 100644 --- a/src/host.ts +++ b/src/host.ts @@ -39,9 +39,9 @@ export class LanguageServiceHost implements ts.LanguageServiceHost return this.cwd; } - public getScriptVersion(_fileName: string) + public getScriptVersion(fileName: string) { - return (this.versions[_fileName] || 0).toString(); + return (this.versions[fileName] || 0).toString(); } public getScriptFileNames() diff --git a/src/icache.ts b/src/icache.ts new file mode 100644 index 00000000..9885a3a3 --- /dev/null +++ b/src/icache.ts @@ -0,0 +1,16 @@ +export interface ICache +{ + exists(name: string): boolean; + + path(name: string): string; + + match(names: string[]): boolean; + + read(name: string): DataType; + + write(name: string, data: DataType): void; + + touch(name: string): void; + + roll(): void; +}; diff --git a/src/index.ts b/src/index.ts index b575dd08..50756b73 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import { RollupContext } from "./rollupcontext"; import { IContext, ConsoleContext, IRollupContext, VerbosityLevel } from "./context"; import { LanguageServiceHost } from "./host"; -import { Cache, convertDiagnostic, ICode, IDiagnostics } from "./cache"; +import { TsCache, convertDiagnostic, ICode, IDiagnostics } from "./tscache"; import * as ts from "typescript"; import * as fs from "fs-extra"; import * as path from "path"; @@ -115,6 +115,9 @@ export default function typescript (options: IOptions) rollupCommonJSResolveHack: false, }); + let watchMode = false; + let round = 0; + const context = new ConsoleContext(options.verbosity, "rpt2: "); context.info(`Typescript version: ${ts.version}`); @@ -126,18 +129,18 @@ export default function typescript (options: IOptions) const servicesHost = new LanguageServiceHost(parsedConfig); - const services = ts.createLanguageService(servicesHost, ts.createDocumentRegistry()); + const service = ts.createLanguageService(servicesHost, ts.createDocumentRegistry()); - const cache = new Cache(servicesHost, options.cacheRoot, parsedConfig.options, parsedConfig.fileNames, context); + const cache = new TsCache(servicesHost, options.cacheRoot, parsedConfig.options, parsedConfig.fileNames, context); - let cleanTranspile = true; + let noErrors = true; if (options.clean) cache.clean(); // printing compiler option errors if (options.check) - printDiagnostics(context, convertDiagnostic(services.getCompilerOptionsDiagnostics())); + printDiagnostics(context, convertDiagnostic(service.getCompilerOptionsDiagnostics())); return { @@ -194,19 +197,19 @@ export default function typescript (options: IOptions) // getting compiled file from cache or from ts const result = cache.getCompiled(id, snapshot, () => { - const output = services.getEmitOutput(id); + const output = service.getEmitOutput(id); if (output.emitSkipped) { - cleanTranspile = false; + noErrors = false; // always checking on fatal errors, even if options.check is set to false const diagnostics = cache.getSyntacticDiagnostics(id, snapshot, () => { - return services.getSyntacticDiagnostics(id); + return service.getSyntacticDiagnostics(id); }).concat(cache.getSemanticDiagnostics(id, snapshot, () => { - return services.getSemanticDiagnostics(id); + return service.getSemanticDiagnostics(id); })); printDiagnostics(contextWrapper, diagnostics); @@ -227,14 +230,14 @@ export default function typescript (options: IOptions) { const diagnostics = cache.getSyntacticDiagnostics(id, snapshot, () => { - return services.getSyntacticDiagnostics(id); + return service.getSyntacticDiagnostics(id); }).concat(cache.getSemanticDiagnostics(id, snapshot, () => { - return services.getSemanticDiagnostics(id); + return service.getSemanticDiagnostics(id); })); if (diagnostics.length !== 0) - cleanTranspile = false; + noErrors = false; printDiagnostics(contextWrapper, diagnostics); } @@ -244,9 +247,17 @@ export default function typescript (options: IOptions) ongenerate(): void { - cache.done(); - if (!cleanTranspile) + if (watchMode) + context.debug("running in watch mode"); + else + cache.done(); + + if (!noErrors) context.info(colors.yellow("there were errors or warnings above.")); + + noErrors = true; + watchMode = true; + round++; }, }; } diff --git a/src/rollingcache.ts b/src/rollingcache.ts index 43214b94..a1225043 100644 --- a/src/rollingcache.ts +++ b/src/rollingcache.ts @@ -1,3 +1,4 @@ +import { ICache } from "./icache"; import * as fs from "fs-extra"; import * as _ from "lodash"; @@ -5,11 +6,13 @@ import * as _ from "lodash"; * 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. */ -export class RollingCache +export class RollingCache implements ICache { private oldCacheRoot: string; private newCacheRoot: string; + private rolled: boolean = false; + /** * @param cacheRoot: root folder for the cache * @param checkNewCache: whether to also look in new cache when reading from cache @@ -65,18 +68,18 @@ export class RollingCache if (data === undefined) return; - if (this.checkNewCache) + if (this.rolled) + fs.writeJsonSync(`${this.oldCacheRoot}/${name}`, data); + else fs.writeJsonSync(`${this.newCacheRoot}/${name}`, data); - else // won't be reading it this run - fs.writeJson(`${this.newCacheRoot}/${name}`, data, { encoding: "utf8" }, () => { ; }); } public touch(name: string) { - if (this.checkNewCache) + if (this.rolled) + fs.ensureFileSync(`${this.oldCacheRoot}/${name}`); + else fs.ensureFileSync(`${this.newCacheRoot}/${name}`); - else // won't be reading it this run - fs.ensureFile(`${this.newCacheRoot}/${name}`, () => { ; }); } /** @@ -84,9 +87,11 @@ export class RollingCache */ public roll() { - fs.remove(this.oldCacheRoot, () => - { - fs.move(this.newCacheRoot, this.oldCacheRoot, () => { ; }); - }); + if (this.rolled) + return; + + this.rolled = true; + fs.removeSync(this.oldCacheRoot); + fs.move(this.newCacheRoot, this.oldCacheRoot, () => { ; }); } } diff --git a/src/cache.ts b/src/tscache.ts similarity index 92% rename from src/cache.ts rename to src/tscache.ts index e850045d..275c73c8 100644 --- a/src/cache.ts +++ b/src/tscache.ts @@ -6,6 +6,7 @@ import * as _ from "lodash"; import { RollingCache } from "./rollingcache"; import * as fs from "fs-extra"; import * as colors from "colors/safe"; +import { ICache } from "./icache"; export interface ICode { @@ -51,17 +52,17 @@ export function convertDiagnostic(data: ts.Diagnostic[]): IDiagnostics[] }); } -export class Cache +export class TsCache { - private cacheVersion = "2"; + private cacheVersion = "3"; private dependencyTree: graph.Graph; private ambientTypes: ITypeSnapshot[]; private ambientTypesDirty = false; private cacheDir: string; - private codeCache: RollingCache; - private typesCache: RollingCache; - private semanticDiagnosticsCache: RollingCache; - private syntacticDiagnosticsCache: RollingCache; + private codeCache: ICache; + private typesCache: ICache; + private semanticDiagnosticsCache: ICache; + private syntacticDiagnosticsCache: ICache; constructor(private host: ts.LanguageServiceHost, cache: string, private options: ts.CompilerOptions, rootFilenames: string[], private context: IContext) { @@ -159,7 +160,6 @@ export class Cache 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); @@ -169,7 +169,7 @@ export class Cache _.each(typeNames, (name) => this.typesCache.touch(name)); } - private getDiagnostics(cache: RollingCache, id: string, snapshot: ts.IScriptSnapshot, check: () => ts.Diagnostic[]): IDiagnostics[] + private getDiagnostics(cache: ICache, id: string, snapshot: ts.IScriptSnapshot, check: () => ts.Diagnostic[]): IDiagnostics[] { const name = this.makeName(id, snapshot); @@ -195,9 +195,9 @@ export class Cache private init() { this.codeCache = new RollingCache(`${this.cacheDir}/code`, true); - this.typesCache = new RollingCache(`${this.cacheDir}/types`, false); - this.syntacticDiagnosticsCache = new RollingCache(`${this.cacheDir}/syntacticDiagnostics`, false); - this.semanticDiagnosticsCache = new RollingCache(`${this.cacheDir}/semanticDiagnostics`, false); + this.typesCache = new RollingCache(`${this.cacheDir}/types`, true); + this.syntacticDiagnosticsCache = new RollingCache(`${this.cacheDir}/syntacticDiagnostics`, true); + this.semanticDiagnosticsCache = new RollingCache(`${this.cacheDir}/semanticDiagnostics`, true); } private markAsDirty(id: string, _snapshot: ts.IScriptSnapshot): void