From 52297b9bd3e4335083a6fac4fc2cd8cc57e7c8bf Mon Sep 17 00:00:00 2001 From: ezolenko Date: Fri, 24 Mar 2017 23:21:16 -0600 Subject: [PATCH] - trying not to fail too badly on old rollup --- dist/rollup-plugin-typescript2.cjs.js | 74 ++++++++++++++++---------- dist/rollup-plugin-typescript2.es.js | 76 +++++++++++++++++---------- src/rollupcontext.ts | 21 ++++++-- 3 files changed, 110 insertions(+), 61 deletions(-) diff --git a/dist/rollup-plugin-typescript2.cjs.js b/dist/rollup-plugin-typescript2.cjs.js index 0dc6ddb..f2ce939 100644 --- a/dist/rollup-plugin-typescript2.cjs.js +++ b/dist/rollup-plugin-typescript2.cjs.js @@ -1,9 +1,9 @@ /* eslint-disable */ 'use strict'; +var _ = require('lodash'); var fs = require('fs-extra'); var ts = require('typescript'); -var _ = require('lodash'); var graph = require('graphlib'); var hash = require('object-hash'); var colors = require('colors/safe'); @@ -79,19 +79,28 @@ var RollupContext = (function () { 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; - this.context.warn("" + this.prefix + message); + 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.bail) - this.context.error("" + this.prefix + message); + if (this.hasContext) { + if (this.bail) + this.context.error("" + this.prefix + message); + else + this.context.warn("" + this.prefix + message); + } else - this.context.warn("" + this.prefix + message); + console.log("" + this.prefix + message); }; RollupContext.prototype.info = function (message) { if (this.verbosity < VerbosityLevel.Info) @@ -128,6 +137,7 @@ var LanguageServiceHost = (function () { 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; @@ -226,11 +236,13 @@ var RollingCache = (function () { return RollingCache; }()); -function convertDiagnostic(data) { +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; @@ -245,7 +257,7 @@ var TsCache = (function () { this.host = host; this.options = options; this.context = context; - this.cacheVersion = "3"; + this.cacheVersion = "4"; this.ambientTypesDirty = false; this.cacheDir = cache + "/" + hash.sha1({ version: this.cacheVersion, @@ -308,10 +320,10 @@ var TsCache = (function () { return data; }; TsCache.prototype.getSyntacticDiagnostics = function (id, snapshot, check) { - return this.getDiagnostics(this.syntacticDiagnosticsCache, id, snapshot, check); + return this.getDiagnostics("syntax", this.syntacticDiagnosticsCache, id, snapshot, check); }; TsCache.prototype.getSemanticDiagnostics = function (id, snapshot, check) { - return this.getDiagnostics(this.semanticDiagnosticsCache, id, snapshot, check); + return this.getDiagnostics("semantic", this.semanticDiagnosticsCache, id, snapshot, check); }; TsCache.prototype.checkAmbientTypes = function () { var _this = this; @@ -327,12 +339,12 @@ var TsCache = (function () { 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 (cache, id, snapshot, check) { + 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(check()); + var data_2 = convertDiagnostic(type, check()); cache.write(name, data_2); this.markAsDirty(id, snapshot); return data_2; @@ -408,7 +420,7 @@ function parseTsConfig(context) { var text = ts.sys.readFile(fileName); var result = ts.parseConfigFileTextToJson(fileName, text); if (result.error) { - printDiagnostics(context, convertDiagnostic([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); @@ -418,25 +430,31 @@ 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 + ": " + color(diagnostic.flatMessage)]); + print.call(context, [diagnostic.fileLine + ": " + type + category + " TS" + diagnostic.code + " " + color(diagnostic.flatMessage)]); else - print.call(context, [color(diagnostic.flatMessage)]); + print.call(context, ["" + type + category + " TS" + diagnostic.code + " " + color(diagnostic.flatMessage)]); }); } @@ -468,7 +486,7 @@ function typescript(options) { cache.clean(); // printing compiler option errors if (options.check) - printDiagnostics(context, convertDiagnostic(service.getCompilerOptionsDiagnostics())); + printDiagnostics(context, convertDiagnostic("options", service.getCompilerOptionsDiagnostics())); return { resolveId: function (importee, importer) { if (importee === TSLIB) @@ -476,6 +494,7 @@ function typescript(options) { 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)) @@ -508,9 +527,9 @@ function typescript(options) { if (output.emitSkipped) { noErrors = false; // always checking on fatal errors, even if options.check is set to false - var diagnostics = cache.getSyntacticDiagnostics(id, snapshot, function () { + var diagnostics = _.concat(cache.getSyntacticDiagnostics(id, snapshot, function () { return service.getSyntacticDiagnostics(id); - }).concat(cache.getSemanticDiagnostics(id, snapshot, function () { + }), cache.getSemanticDiagnostics(id, snapshot, function () { return service.getSemanticDiagnostics(id); })); printDiagnostics(contextWrapper, diagnostics); @@ -525,40 +544,39 @@ function typescript(options) { }; }); if (options.check) { - var diagnostics = cache.getSyntacticDiagnostics(id, snapshot, function () { + var diagnostics = _.concat(cache.getSyntacticDiagnostics(id, snapshot, function () { return service.getSyntacticDiagnostics(id); - }).concat(cache.getSemanticDiagnostics(id, snapshot, function () { + }), cache.getSemanticDiagnostics(id, snapshot, function () { return service.getSemanticDiagnostics(id); })); - if (diagnostics.length !== 0) + if (diagnostics.length > 0) noErrors = false; printDiagnostics(contextWrapper, diagnostics); } return result; }, ongenerate: function (bundleOptions) { - if (_.isArray(bundleOptions.targets)) - targetCount = bundleOptions.targets.length; + targetCount = _.get(bundleOptions, "targets.length", 1); if (round >= targetCount) { watchMode = true; round = 0; } - context.debug("generating target " + round + " of " + bundleOptions.targets.length); + context.debug("generating target " + (round + 1) + " of " + targetCount); if (watchMode && round === 0) { context.debug("running in watch mode"); // hack to fix ts lagging servicesHost.reset(); service.cleanupSemanticCache(); cache.walkTree(function (id) { - var diagnostics = convertDiagnostic(service.getSyntacticDiagnostics(id)).concat(convertDiagnostic(service.getSemanticDiagnostics(id))); + var diagnostics = _.concat(convertDiagnostic("syntax", service.getSyntacticDiagnostics(id)), convertDiagnostic("semantic", service.getSemanticDiagnostics(id))); if (diagnostics.length > 0) noErrors = false; printDiagnostics(context, diagnostics); }); - } - if (!noErrors) { - noErrors = true; - context.info(colors.yellow("there were errors or warnings above.")); + if (!noErrors) { + noErrors = true; + context.info(colors.yellow("there were errors or warnings above.")); + } } cache.done(); round++; diff --git a/dist/rollup-plugin-typescript2.es.js b/dist/rollup-plugin-typescript2.es.js index d9c7472..ca2cf62 100644 --- a/dist/rollup-plugin-typescript2.es.js +++ b/dist/rollup-plugin-typescript2.es.js @@ -1,10 +1,10 @@ /* eslint-disable */ +import { concat, defaults, each, endsWith, filter, find, get, has, isEqual, isFunction, map, some } from 'lodash'; +import * as _ from 'lodash'; 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'; -import { defaults, each, endsWith, filter, find, has, isArray, isEqual, map, some } from 'lodash'; -import * as _ from 'lodash'; import { Graph, alg } from 'graphlib'; import * as graph from 'graphlib'; import { sha1 } from 'object-hash'; @@ -85,19 +85,28 @@ var RollupContext = (function () { 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; - this.context.warn("" + this.prefix + message); + 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.bail) - this.context.error("" + this.prefix + message); + if (this.hasContext) { + if (this.bail) + this.context.error("" + this.prefix + message); + else + this.context.warn("" + this.prefix + message); + } else - this.context.warn("" + this.prefix + message); + console.log("" + this.prefix + message); }; RollupContext.prototype.info = function (message) { if (this.verbosity < VerbosityLevel.Info) @@ -134,6 +143,7 @@ var LanguageServiceHost = (function () { 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; @@ -232,11 +242,13 @@ var RollingCache = (function () { return RollingCache; }()); -function convertDiagnostic(data) { +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; @@ -251,7 +263,7 @@ var TsCache = (function () { this.host = host; this.options = options; this.context = context; - this.cacheVersion = "3"; + this.cacheVersion = "4"; this.ambientTypesDirty = false; this.cacheDir = cache + "/" + sha1({ version: this.cacheVersion, @@ -314,10 +326,10 @@ var TsCache = (function () { return data; }; TsCache.prototype.getSyntacticDiagnostics = function (id, snapshot, check) { - return this.getDiagnostics(this.syntacticDiagnosticsCache, id, snapshot, check); + return this.getDiagnostics("syntax", this.syntacticDiagnosticsCache, id, snapshot, check); }; TsCache.prototype.getSemanticDiagnostics = function (id, snapshot, check) { - return this.getDiagnostics(this.semanticDiagnosticsCache, id, snapshot, check); + return this.getDiagnostics("semantic", this.semanticDiagnosticsCache, id, snapshot, check); }; TsCache.prototype.checkAmbientTypes = function () { var _this = this; @@ -333,12 +345,12 @@ var TsCache = (function () { this.context.info(yellow("ambient types changed, redoing all semantic diagnostics")); each(typeNames, function (name) { return _this.typesCache.touch(name); }); }; - TsCache.prototype.getDiagnostics = function (cache, id, snapshot, check) { + 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(check()); + var data_2 = convertDiagnostic(type, check()); cache.write(name, data_2); this.markAsDirty(id, snapshot); return data_2; @@ -414,7 +426,7 @@ function parseTsConfig(context) { var text = sys.readFile(fileName); var result = parseConfigFileTextToJson(fileName, text); if (result.error) { - printDiagnostics(context, convertDiagnostic([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); @@ -424,25 +436,31 @@ 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 + ": " + color(diagnostic.flatMessage)]); + print.call(context, [diagnostic.fileLine + ": " + type + category + " TS" + diagnostic.code + " " + color(diagnostic.flatMessage)]); else - print.call(context, [color(diagnostic.flatMessage)]); + print.call(context, ["" + type + category + " TS" + diagnostic.code + " " + color(diagnostic.flatMessage)]); }); } @@ -474,7 +492,7 @@ function typescript(options) { cache.clean(); // printing compiler option errors if (options.check) - printDiagnostics(context, convertDiagnostic(service.getCompilerOptionsDiagnostics())); + printDiagnostics(context, convertDiagnostic("options", service.getCompilerOptionsDiagnostics())); return { resolveId: function (importee, importer) { if (importee === TSLIB) @@ -482,6 +500,7 @@ function typescript(options) { 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)) @@ -514,9 +533,9 @@ function typescript(options) { if (output.emitSkipped) { noErrors = false; // always checking on fatal errors, even if options.check is set to false - var diagnostics = cache.getSyntacticDiagnostics(id, snapshot, function () { + var diagnostics = concat(cache.getSyntacticDiagnostics(id, snapshot, function () { return service.getSyntacticDiagnostics(id); - }).concat(cache.getSemanticDiagnostics(id, snapshot, function () { + }), cache.getSemanticDiagnostics(id, snapshot, function () { return service.getSemanticDiagnostics(id); })); printDiagnostics(contextWrapper, diagnostics); @@ -531,40 +550,39 @@ function typescript(options) { }; }); if (options.check) { - var diagnostics = cache.getSyntacticDiagnostics(id, snapshot, function () { + var diagnostics = concat(cache.getSyntacticDiagnostics(id, snapshot, function () { return service.getSyntacticDiagnostics(id); - }).concat(cache.getSemanticDiagnostics(id, snapshot, function () { + }), cache.getSemanticDiagnostics(id, snapshot, function () { return service.getSemanticDiagnostics(id); })); - if (diagnostics.length !== 0) + if (diagnostics.length > 0) noErrors = false; printDiagnostics(contextWrapper, diagnostics); } return result; }, ongenerate: function (bundleOptions) { - if (isArray(bundleOptions.targets)) - targetCount = bundleOptions.targets.length; + targetCount = get(bundleOptions, "targets.length", 1); if (round >= targetCount) { watchMode = true; round = 0; } - context.debug("generating target " + round + " of " + bundleOptions.targets.length); + context.debug("generating target " + (round + 1) + " of " + targetCount); if (watchMode && round === 0) { context.debug("running in watch mode"); // hack to fix ts lagging servicesHost.reset(); service.cleanupSemanticCache(); cache.walkTree(function (id) { - var diagnostics = convertDiagnostic(service.getSyntacticDiagnostics(id)).concat(convertDiagnostic(service.getSemanticDiagnostics(id))); + var diagnostics = concat(convertDiagnostic("syntax", service.getSyntacticDiagnostics(id)), convertDiagnostic("semantic", service.getSemanticDiagnostics(id))); if (diagnostics.length > 0) noErrors = false; printDiagnostics(context, diagnostics); }); - } - if (!noErrors) { - noErrors = true; - context.info(yellow("there were errors or warnings above.")); + if (!noErrors) { + noErrors = true; + context.info(yellow("there were errors or warnings above.")); + } } cache.done(); round++; diff --git a/src/rollupcontext.ts b/src/rollupcontext.ts index 08d3214..13bcb7f 100644 --- a/src/rollupcontext.ts +++ b/src/rollupcontext.ts @@ -1,16 +1,24 @@ import { IContext, IRollupContext, VerbosityLevel } from "./context"; +import * as _ from "lodash"; export class RollupContext implements IContext { + private hasContext: boolean = true; + constructor(private verbosity: VerbosityLevel, private bail: boolean, private context: IRollupContext, private prefix: string = "") { + this.hasContext = _.isFunction(this.context.warn) && _.isFunction(this.context.error); } public warn(message: string): void { if (this.verbosity < VerbosityLevel.Warning) return; - this.context.warn(`${this.prefix}${message}`); + + if (this.hasContext) + this.context.warn(`${this.prefix}${message}`); + else + console.log(`${this.prefix}${message}`); } public error(message: string): void @@ -18,10 +26,15 @@ export class RollupContext implements IContext if (this.verbosity < VerbosityLevel.Error) return; - if (this.bail) - this.context.error(`${this.prefix}${message}`); + if (this.hasContext) + { + if (this.bail) + this.context.error(`${this.prefix}${message}`); + else + this.context.warn(`${this.prefix}${message}`); + } else - this.context.warn(`${this.prefix}${message}`); + console.log(`${this.prefix}${message}`); } public info(message: string): void