Skip to content

Commit

Permalink
- partial fix for watch mode (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezolenko committed Mar 13, 2017
1 parent 1dbe1cc commit ff7cb72
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/node_modules
/npm-debug.log
/typings
/.rts2_cache
/.rpt2_cache
114 changes: 65 additions & 49 deletions dist/rollup-plugin-typescript2.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""; }
Expand Down Expand Up @@ -106,6 +108,8 @@ var RollupContext = (function () {
return RollupContext;
}());

//# sourceMappingURL=rollupcontext.js.map

var LanguageServiceHost = (function () {
function LanguageServiceHost(parsedConfig) {
this.parsedConfig = parsedConfig;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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 = {
Expand All @@ -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,
Expand All @@ -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) + "'");
Expand All @@ -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; })
Expand All @@ -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)) {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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;
Loading

0 comments on commit ff7cb72

Please sign in to comment.