diff --git a/dist/rollup-plugin-typescript2.cjs.js b/dist/rollup-plugin-typescript2.cjs.js index 124f9419..79a1dc96 100644 --- a/dist/rollup-plugin-typescript2.cjs.js +++ b/dist/rollup-plugin-typescript2.cjs.js @@ -219,10 +219,7 @@ var RollingCache = (function () { return; if (data === undefined) return; - if (this.rolled) - fs.writeJsonSync(this.oldCacheRoot + "/" + name, data); - else - fs.writeJsonSync(this.newCacheRoot + "/" + name, data); + fs.writeJsonSync(this.newCacheRoot + "/" + name, data); }; RollingCache.prototype.touch = function (name) { if (this.rolled) @@ -258,27 +255,29 @@ function convertDiagnostic(type, data) { }); } var TsCache = (function () { - function TsCache(host, cache, options, rootFilenames, context) { + 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 = "4"; + 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 }; }); + 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) }; }); + .map(function (id) { return ({ id: id, snapshot: _this.host.getScriptSnapshot(id) }); }); this.init(); this.checkAmbientTypes(); } @@ -463,7 +462,6 @@ function printDiagnostics(context, diagnostics) { print.call(context, ["" + type + category + " TS" + diagnostic.code + " " + color(diagnostic.flatMessage)]); }); } - function typescript(options) { options = __assign({}, options); _.defaults(options, { @@ -476,6 +474,7 @@ function typescript(options) { abortOnError: true, rollupCommonJSResolveHack: false, }); + var rollupConfig; var watchMode = false; var round = 0; var targetCount = 0; @@ -486,14 +485,23 @@ function typescript(options) { var parsedConfig = parseTsConfig(context); var servicesHost = new LanguageServiceHost(parsedConfig); var service = ts.createLanguageService(servicesHost, ts.createDocumentRegistry()); - var cache = new TsCache(servicesHost, options.cacheRoot, parsedConfig.options, parsedConfig.fileNames, context); + var _cache; + var cache = function () { + if (!_cache) + _cache = new TsCache(servicesHost, options.cacheRoot, parsedConfig.options, rollupConfig, parsedConfig.fileNames, context); + return _cache; + }; var noErrors = true; - if (options.clean) - cache.clean(); // 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; @@ -504,7 +512,7 @@ function typescript(options) { 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); + cache().setDependency(result.resolvedModule.resolvedFileName, importer); if (_.endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) return null; var resolved = options.rollupCommonJSResolveHack @@ -528,19 +536,19 @@ function typescript(options) { 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 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 () { + var diagnostics = _.concat(cache().getSyntacticDiagnostics(id, snapshot, function () { return service.getSyntacticDiagnostics(id); - }), cache.getSemanticDiagnostics(id, snapshot, function () { + }), cache().getSemanticDiagnostics(id, snapshot, function () { return service.getSemanticDiagnostics(id); })); printDiagnostics(contextWrapper, diagnostics); // since no output was generated, aborting compilation - cache.done(); + cache().done(); if (_.isFunction(_this.error)) _this.error(colors.red("failed to transpile '" + id + "'")); } @@ -552,9 +560,9 @@ function typescript(options) { }; }); if (options.check) { - var diagnostics = _.concat(cache.getSyntacticDiagnostics(id, snapshot, function () { + var diagnostics = _.concat(cache().getSyntacticDiagnostics(id, snapshot, function () { return service.getSyntacticDiagnostics(id); - }), cache.getSemanticDiagnostics(id, snapshot, function () { + }), cache().getSemanticDiagnostics(id, snapshot, function () { return service.getSemanticDiagnostics(id); })); if (diagnostics.length > 0) @@ -572,14 +580,14 @@ function typescript(options) { context.debug("generating target " + (round + 1) + " of " + targetCount); if (watchMode && round === 0) { context.debug("running in watch mode"); - cache.walkTree(function (id) { + 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(); + cache().done(); round++; }, }; diff --git a/dist/rollup-plugin-typescript2.es.js b/dist/rollup-plugin-typescript2.es.js index 25b75c9a..d4e2ca5e 100644 --- a/dist/rollup-plugin-typescript2.es.js +++ b/dist/rollup-plugin-typescript2.es.js @@ -225,10 +225,7 @@ var RollingCache = (function () { return; if (data === undefined) return; - if (this.rolled) - writeJsonSync(this.oldCacheRoot + "/" + name, data); - else - writeJsonSync(this.newCacheRoot + "/" + name, data); + writeJsonSync(this.newCacheRoot + "/" + name, data); }; RollingCache.prototype.touch = function (name) { if (this.rolled) @@ -264,27 +261,29 @@ function convertDiagnostic(type, data) { }); } var TsCache = (function () { - function TsCache(host, cache, options, rootFilenames, context) { + 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 = "4"; + 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 }; }); + 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) }; }); + .map(function (id) { return ({ id: id, snapshot: _this.host.getScriptSnapshot(id) }); }); this.init(); this.checkAmbientTypes(); } @@ -469,7 +468,6 @@ function printDiagnostics(context, diagnostics) { print.call(context, ["" + type + category + " TS" + diagnostic.code + " " + color(diagnostic.flatMessage)]); }); } - function typescript(options) { options = __assign({}, options); defaults(options, { @@ -482,6 +480,7 @@ function typescript(options) { abortOnError: true, rollupCommonJSResolveHack: false, }); + var rollupConfig; var watchMode = false; var round = 0; var targetCount = 0; @@ -492,14 +491,23 @@ function typescript(options) { var parsedConfig = parseTsConfig(context); var servicesHost = new LanguageServiceHost(parsedConfig); var service = createLanguageService(servicesHost, createDocumentRegistry()); - var cache = new TsCache(servicesHost, options.cacheRoot, parsedConfig.options, parsedConfig.fileNames, context); + var _cache; + var cache = function () { + if (!_cache) + _cache = new TsCache(servicesHost, options.cacheRoot, parsedConfig.options, rollupConfig, parsedConfig.fileNames, context); + return _cache; + }; var noErrors = true; - if (options.clean) - cache.clean(); // 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; @@ -510,7 +518,7 @@ function typescript(options) { 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); + cache().setDependency(result.resolvedModule.resolvedFileName, importer); if (endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) return null; var resolved = options.rollupCommonJSResolveHack @@ -534,19 +542,19 @@ function typescript(options) { 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 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 () { + var diagnostics = concat(cache().getSyntacticDiagnostics(id, snapshot, function () { return service.getSyntacticDiagnostics(id); - }), cache.getSemanticDiagnostics(id, snapshot, function () { + }), cache().getSemanticDiagnostics(id, snapshot, function () { return service.getSemanticDiagnostics(id); })); printDiagnostics(contextWrapper, diagnostics); // since no output was generated, aborting compilation - cache.done(); + cache().done(); if (isFunction(_this.error)) _this.error(red("failed to transpile '" + id + "'")); } @@ -558,9 +566,9 @@ function typescript(options) { }; }); if (options.check) { - var diagnostics = concat(cache.getSyntacticDiagnostics(id, snapshot, function () { + var diagnostics = concat(cache().getSyntacticDiagnostics(id, snapshot, function () { return service.getSyntacticDiagnostics(id); - }), cache.getSemanticDiagnostics(id, snapshot, function () { + }), cache().getSemanticDiagnostics(id, snapshot, function () { return service.getSemanticDiagnostics(id); })); if (diagnostics.length > 0) @@ -578,14 +586,14 @@ function typescript(options) { context.debug("generating target " + (round + 1) + " of " + targetCount); if (watchMode && round === 0) { context.debug("running in watch mode"); - cache.walkTree(function (id) { + 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(); + cache().done(); round++; }, }; diff --git a/src/index.ts b/src/index.ts index 578e7da5..98cdbed2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -92,7 +92,7 @@ function printDiagnostics(context: IContext, diagnostics: IDiagnostics[]) else print.call(context, [`${type}${category} TS${diagnostic.code} ${color(diagnostic.flatMessage)}`]); }); -}; +} interface IOptions { @@ -106,7 +106,7 @@ interface IOptions rollupCommonJSResolveHack: boolean; } -export default function typescript (options: IOptions) +export default function typescript(options: IOptions) { options = { ... options }; @@ -122,6 +122,8 @@ export default function typescript (options: IOptions) rollupCommonJSResolveHack: false, }); + let rollupConfig: any; + let watchMode = false; let round = 0; let targetCount = 0; @@ -135,16 +137,20 @@ export default function typescript (options: IOptions) const parsedConfig = parseTsConfig(context); - let servicesHost = new LanguageServiceHost(parsedConfig); + const servicesHost = new LanguageServiceHost(parsedConfig); - let service = ts.createLanguageService(servicesHost, ts.createDocumentRegistry()); + const service = ts.createLanguageService(servicesHost, ts.createDocumentRegistry()); - const cache = new TsCache(servicesHost, options.cacheRoot, parsedConfig.options, parsedConfig.fileNames, context); + let _cache: TsCache; - let noErrors = true; + const cache = (): TsCache => + { + if (!_cache) + _cache = new TsCache(servicesHost, options.cacheRoot, parsedConfig.options, rollupConfig, parsedConfig.fileNames, context); + return _cache; + }; - if (options.clean) - cache.clean(); + let noErrors = true; // printing compiler option errors if (options.check) @@ -152,6 +158,16 @@ export default function typescript (options: IOptions) return { + options(config: any) + { + rollupConfig = config; + + context.debug(`rollupConfig: ${JSON.stringify(rollupConfig, undefined, 4)}`); + + if (options.clean) + cache().clean(); + }, + resolveId(importee: string, importer: string) { if (importee === TSLIB) @@ -168,7 +184,7 @@ export default function typescript (options: IOptions) if (result.resolvedModule && result.resolvedModule.resolvedFileName) { if (filter(result.resolvedModule.resolvedFileName)) - cache.setDependency(result.resolvedModule.resolvedFileName, importer); + cache().setDependency(result.resolvedModule.resolvedFileName, importer); if (_.endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) return null; @@ -204,7 +220,7 @@ export default function typescript (options: IOptions) const snapshot = servicesHost.setSnapshot(id, code); // getting compiled file from cache or from ts - const result = cache.getCompiled(id, snapshot, () => + const result = cache().getCompiled(id, snapshot, () => { const output = service.getEmitOutput(id); @@ -214,11 +230,11 @@ export default function typescript (options: IOptions) // always checking on fatal errors, even if options.check is set to false const diagnostics = _.concat( - cache.getSyntacticDiagnostics(id, snapshot, () => + cache().getSyntacticDiagnostics(id, snapshot, () => { return service.getSyntacticDiagnostics(id); }), - cache.getSemanticDiagnostics(id, snapshot, () => + cache().getSemanticDiagnostics(id, snapshot, () => { return service.getSemanticDiagnostics(id); }), @@ -226,7 +242,7 @@ export default function typescript (options: IOptions) printDiagnostics(contextWrapper, diagnostics); // since no output was generated, aborting compilation - cache.done(); + cache().done(); if (_.isFunction(this.error)) this.error(colors.red(`failed to transpile '${id}'`)); } @@ -243,11 +259,11 @@ export default function typescript (options: IOptions) if (options.check) { const diagnostics = _.concat( - cache.getSyntacticDiagnostics(id, snapshot, () => + cache().getSyntacticDiagnostics(id, snapshot, () => { return service.getSyntacticDiagnostics(id); }), - cache.getSemanticDiagnostics(id, snapshot, () => + cache().getSemanticDiagnostics(id, snapshot, () => { return service.getSemanticDiagnostics(id); }), @@ -277,7 +293,7 @@ export default function typescript (options: IOptions) { context.debug("running in watch mode"); - cache.walkTree((id) => + cache().walkTree((id) => { const diagnostics = _.concat( convertDiagnostic("syntax", service.getSyntacticDiagnostics(id)), @@ -291,7 +307,7 @@ export default function typescript (options: IOptions) if (!watchMode && !noErrors) context.info(colors.yellow("there were errors or warnings above.")); - cache.done(); + cache().done(); round++; }, diff --git a/src/tscache.ts b/src/tscache.ts index 5c7b9009..7bbee3d5 100644 --- a/src/tscache.ts +++ b/src/tscache.ts @@ -58,7 +58,7 @@ export function convertDiagnostic(type: string, data: ts.Diagnostic[]): IDiagnos export class TsCache { - private cacheVersion = "4"; + private cacheVersion = "5"; private dependencyTree: graph.Graph; private ambientTypes: ITypeSnapshot[]; private ambientTypesDirty = false; @@ -68,17 +68,18 @@ export class TsCache private semanticDiagnosticsCache: ICache; private syntacticDiagnosticsCache: ICache; - constructor(private host: ts.LanguageServiceHost, cache: string, private options: ts.CompilerOptions, rootFilenames: string[], private context: IContext) + constructor(private host: ts.LanguageServiceHost, cache: string, private options: ts.CompilerOptions, private rollupConfig: any, rootFilenames: string[], private context: IContext) { this.cacheDir = `${cache}/${hash.sha1({ version: this.cacheVersion, rootFilenames, options: this.options, + rollupConfig: this.rollupConfig, tsVersion : ts.version, })}`; this.dependencyTree = new graph.Graph({ directed: true }); - this.dependencyTree.setDefaultNodeLabel((_node: string) => { return { dirty: false }; }); + this.dependencyTree.setDefaultNodeLabel((_node: string) => ({ dirty: false }) ); const automaticTypes = _ .map(ts.getAutomaticTypeDirectiveNames(options, ts.sys), (entry) => ts.resolveTypeReferenceDirective(entry, undefined, options, ts.sys)) @@ -88,7 +89,7 @@ export class TsCache this.ambientTypes = _ .filter(rootFilenames, (file) => _.endsWith(file, ".d.ts")) .concat(automaticTypes) - .map((id) => { return { id, snapshot: this.host.getScriptSnapshot(id) }; }); + .map((id) => ({ id, snapshot: this.host.getScriptSnapshot(id) })); this.init();