From c59b8cece756620c485d35d104e84314cabaff1e Mon Sep 17 00:00:00 2001 From: ezolenko Date: Mon, 6 Mar 2017 21:22:37 -0700 Subject: [PATCH] - tracking changes to automatic ambient types --- dist/rollup-plugin-typescript2.cjs.js | 35 ++++++--------------- dist/rollup-plugin-typescript2.es.js | 39 ++++++++---------------- package.json | 2 +- src/cache.ts | 6 ++++ src/index.ts | 44 ++++++--------------------- 5 files changed, 38 insertions(+), 88 deletions(-) diff --git a/dist/rollup-plugin-typescript2.cjs.js b/dist/rollup-plugin-typescript2.cjs.js index 6ac3a77..21d82c4 100644 --- a/dist/rollup-plugin-typescript2.cjs.js +++ b/dist/rollup-plugin-typescript2.cjs.js @@ -241,7 +241,11 @@ var Cache = (function () { }); 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(); } @@ -371,23 +375,6 @@ function getOptionsOverrides() { noResolve: false, }; } -// Gratefully lifted from 'look-up', due to problems using it directly: -// https://github.com/jonschlinkert/look-up/blob/master/index.js -// MIT Licenced -function findFile(cwd, filename) { - var fp = cwd ? (cwd + "/" + filename) : filename; - if (fs.existsSync(fp)) - return fp; - var segs = cwd.split(path.sep); - var len = segs.length; - while (len--) { - cwd = segs.slice(0, len).join("/"); - fp = cwd + "/" + filename; - if (fs.existsSync(fp)) - return fp; - } - return null; -} // The injected id for helpers. var TSLIB = "tslib"; var tslibSource; @@ -401,7 +388,7 @@ catch (e) { throw e; } function parseTsConfig(context) { - var fileName = findFile(process.cwd(), "tsconfig.json"); + 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); @@ -445,7 +432,7 @@ function typescript(options) { check: true, verbosity: VerbosityLevel.Info, clean: false, - cacheRoot: process.cwd() + "/.rts2_cache", + cacheRoot: process.cwd() + "/.rpt2_cache", include: ["*.ts+(|x)", "**/*.ts+(|x)"], exclude: ["*.d.ts", "**/*.d.ts"], abortOnError: true, @@ -458,6 +445,9 @@ function typescript(options) { var cache = new Cache(servicesHost, options.cacheRoot, parsedConfig.options, parsedConfig.fileNames, context); if (options.clean) cache.clean(); + // printing compiler option errors + if (options.check) + printDiagnostics(context, convertDiagnostic(services.getCompilerOptionsDiagnostics())); return { resolveId: function (importee, importer) { if (importee === TSLIB) @@ -471,6 +461,7 @@ function typescript(options) { cache.setDependency(result.resolvedModule.resolvedFileName, importer); if (_.endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) return null; + context.debug("resolving " + importee + " to " + result.resolvedModule.resolvedFileName); return result.resolvedModule.resolvedFileName; } return null; @@ -515,12 +506,6 @@ function typescript(options) { } return result; }, - intro: function () { - context.debug("intro"); - // printing compiler option errors - if (options.check) - printDiagnostics(context, convertDiagnostic(services.getCompilerOptionsDiagnostics())); - }, outro: function () { context.debug("outro"); cache.compileDone(); diff --git a/dist/rollup-plugin-typescript2.es.js b/dist/rollup-plugin-typescript2.es.js index af23f94..ba9d262 100644 --- a/dist/rollup-plugin-typescript2.es.js +++ b/dist/rollup-plugin-typescript2.es.js @@ -1,7 +1,7 @@ /* eslint-disable */ import { emptyDirSync, ensureFile, ensureFileSync, existsSync, move, readFileSync, readJsonSync, readdirSync, remove, writeJson, writeJsonSync } from 'fs-extra'; import * as fs from 'fs-extra'; -import { DiagnosticCategory, ModuleKind, ScriptSnapshot, createDocumentRegistry, createLanguageService, flattenDiagnosticMessageText, getDefaultLibFilePath, nodeModuleNameResolver, parseConfigFileTextToJson, parseJsonConfigFileContent, sys, version } from 'typescript'; +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, isEqual, map, some } from 'lodash'; import * as _ from 'lodash'; @@ -9,7 +9,7 @@ import { Graph, alg } from 'graphlib'; import * as graph from 'graphlib'; import { sha1 } from 'object-hash'; import * as hash from 'object-hash'; -import { dirname, sep } from 'path'; +import { dirname } from 'path'; import * as path from 'path'; import { red, white, yellow } from 'colors/safe'; import * as colors from 'colors/safe'; @@ -246,7 +246,11 @@ var Cache = (function () { }); 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(); } @@ -376,23 +380,6 @@ function getOptionsOverrides() { noResolve: false, }; } -// Gratefully lifted from 'look-up', due to problems using it directly: -// https://github.com/jonschlinkert/look-up/blob/master/index.js -// MIT Licenced -function findFile(cwd, filename) { - var fp = cwd ? (cwd + "/" + filename) : filename; - if (existsSync(fp)) - return fp; - var segs = cwd.split(sep); - var len = segs.length; - while (len--) { - cwd = segs.slice(0, len).join("/"); - fp = cwd + "/" + filename; - if (existsSync(fp)) - return fp; - } - return null; -} // The injected id for helpers. var TSLIB = "tslib"; var tslibSource; @@ -406,7 +393,7 @@ catch (e) { throw e; } function parseTsConfig(context) { - var fileName = findFile(process.cwd(), "tsconfig.json"); + 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); @@ -450,7 +437,7 @@ function typescript(options) { check: true, verbosity: VerbosityLevel.Info, clean: false, - cacheRoot: process.cwd() + "/.rts2_cache", + cacheRoot: process.cwd() + "/.rpt2_cache", include: ["*.ts+(|x)", "**/*.ts+(|x)"], exclude: ["*.d.ts", "**/*.d.ts"], abortOnError: true, @@ -463,6 +450,9 @@ function typescript(options) { var cache = new Cache(servicesHost, options.cacheRoot, parsedConfig.options, parsedConfig.fileNames, context); if (options.clean) cache.clean(); + // printing compiler option errors + if (options.check) + printDiagnostics(context, convertDiagnostic(services.getCompilerOptionsDiagnostics())); return { resolveId: function (importee, importer) { if (importee === TSLIB) @@ -476,6 +466,7 @@ function typescript(options) { cache.setDependency(result.resolvedModule.resolvedFileName, importer); if (endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) return null; + context.debug("resolving " + importee + " to " + result.resolvedModule.resolvedFileName); return result.resolvedModule.resolvedFileName; } return null; @@ -520,12 +511,6 @@ function typescript(options) { } return result; }, - intro: function () { - context.debug("intro"); - // printing compiler option errors - if (options.check) - printDiagnostics(context, convertDiagnostic(services.getCompilerOptionsDiagnostics())); - }, outro: function () { context.debug("outro"); cache.compileDone(); diff --git a/package.json b/package.json index b6f594e..719c483 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-typescript2", - "version": "0.2.1", + "version": "0.2.2", "description": "Seamless integration between Rollup and TypeScript. Now with errors.", "main": "dist/rollup-plugin-typescript2.cjs.js", "module": "dist/rollup-plugin-typescript2.es.js", diff --git a/src/cache.ts b/src/cache.ts index 9a2a805..cbe8116 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -74,8 +74,14 @@ export class Cache this.dependencyTree = new graph.Graph({ directed: true }); this.dependencyTree.setDefaultNodeLabel((_node: string) => { return { dirty: false }; }); + const automaticTypes = _ + .map(ts.getAutomaticTypeDirectiveNames(options, ts.sys), (entry) => ts.resolveTypeReferenceDirective(entry, undefined, options, ts.sys)) + .filter((entry) => entry.resolvedTypeReferenceDirective && entry.resolvedTypeReferenceDirective.resolvedFileName) + .map((entry) => entry.resolvedTypeReferenceDirective!.resolvedFileName!); + this.ambientTypes = _ .filter(rootFilenames, (file) => _.endsWith(file, ".d.ts")) + .concat(automaticTypes) .map((id) => { return { id, snapshot: this.host.getScriptSnapshot(id) }; }); this.init(); diff --git a/src/index.ts b/src/index.ts index 91e69a1..4e87ee0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,30 +22,6 @@ function getOptionsOverrides(): ts.CompilerOptions }; } -// Gratefully lifted from 'look-up', due to problems using it directly: -// https://github.com/jonschlinkert/look-up/blob/master/index.js -// MIT Licenced -function findFile(cwd: string, filename: string) -{ - let fp = cwd ? (cwd + "/" + filename) : filename; - - if (fs.existsSync(fp)) - return fp; - - const segs = cwd.split(path.sep); - let len = segs.length; - - while (len--) - { - cwd = segs.slice(0, len).join("/"); - fp = cwd + "/" + filename; - if (fs.existsSync(fp)) - return fp; - } - - return null; -} - // The injected id for helpers. const TSLIB = "tslib"; let tslibSource: string; @@ -62,7 +38,8 @@ try function parseTsConfig(context: IContext) { - const fileName = findFile(process.cwd(), "tsconfig.json"); + const fileName = ts.findConfigFile(process.cwd(), ts.sys.fileExists, "tsconfig.json"); + if (!fileName) throw new Error(`couldn't find 'tsconfig.json' in ${process.cwd()}`); @@ -130,7 +107,7 @@ export default function typescript (options: IOptions) check: true, verbosity: VerbosityLevel.Info, clean: false, - cacheRoot: `${process.cwd()}/.rts2_cache`, + cacheRoot: `${process.cwd()}/.rpt2_cache`, include: [ "*.ts+(|x)", "**/*.ts+(|x)" ], exclude: [ "*.d.ts", "**/*.d.ts" ], abortOnError: true, @@ -151,6 +128,10 @@ export default function typescript (options: IOptions) if (options.clean) cache.clean(); + // printing compiler option errors + if (options.check) + printDiagnostics(context, convertDiagnostic(services.getCompilerOptionsDiagnostics())); + return { resolveId(importee: string, importer: string) @@ -173,6 +154,8 @@ export default function typescript (options: IOptions) if (_.endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) return null; + context.debug(`resolving ${importee} to ${result.resolvedModule.resolvedFileName}`); + return result.resolvedModule.resolvedFileName; } @@ -238,15 +221,6 @@ export default function typescript (options: IOptions) return result; }, - intro(): void - { - context.debug("intro"); - - // printing compiler option errors - if (options.check) - printDiagnostics(context, convertDiagnostic(services.getCompilerOptionsDiagnostics())); - }, - outro(): void { context.debug("outro");