From 448913d411aa921c407240e517c43f041d65859b Mon Sep 17 00:00:00 2001 From: ezolenko Date: Tue, 15 Aug 2017 17:56:29 -0600 Subject: [PATCH] - fix for #24. forcing declarations for project files even if they are ignored by rollup --- dist/host.d.ts | 1 - dist/normalize.d.ts | 1 + dist/rollup-plugin-typescript2.cjs.js | 112 +++++++++++++++----------- dist/rollup-plugin-typescript2.es.js | 65 +++++++++------ src/get-options-overrides.ts | 4 +- src/host.ts | 17 ++-- src/index.ts | 76 ++++++++++------- src/normalize.ts | 4 + src/print-diagnostics.ts | 4 +- src/rollingcache.ts | 4 +- src/rollupcontext.ts | 4 +- src/tscache.ts | 18 ++--- 12 files changed, 181 insertions(+), 129 deletions(-) create mode 100644 dist/normalize.d.ts create mode 100644 src/normalize.ts diff --git a/dist/host.d.ts b/dist/host.d.ts index 39405b82..f2965146 100644 --- a/dist/host.d.ts +++ b/dist/host.d.ts @@ -20,5 +20,4 @@ export declare class LanguageServiceHost implements tsTypes.LanguageServiceHost getTypeRootsVersion(): number; directoryExists(directoryName: string): boolean; getDirectories(directoryName: string): string[]; - private normalize(fileName); } diff --git a/dist/normalize.d.ts b/dist/normalize.d.ts new file mode 100644 index 00000000..224bcea9 --- /dev/null +++ b/dist/normalize.d.ts @@ -0,0 +1 @@ +export declare function normalize(fileName: string): string; diff --git a/dist/rollup-plugin-typescript2.cjs.js b/dist/rollup-plugin-typescript2.cjs.js index 0e27441d..93d6c103 100644 --- a/dist/rollup-plugin-typescript2.cjs.js +++ b/dist/rollup-plugin-typescript2.cjs.js @@ -1,7 +1,7 @@ /* eslint-disable */ 'use strict'; -var lodash = require('lodash'); +var _ = require('lodash'); var fs = require('fs'); var graphlib = require('graphlib'); var objectHash = require('object-hash'); @@ -80,7 +80,7 @@ var RollupContext = (function () { this.context = context; this.prefix = prefix; this.hasContext = true; - this.hasContext = lodash.isFunction(this.context.warn) && lodash.isFunction(this.context.error); + this.hasContext = _.isFunction(this.context.warn) && _.isFunction(this.context.error); } RollupContext.prototype.warn = function (message) { if (this.verbosity < VerbosityLevel.Warning) @@ -120,6 +120,10 @@ function setTypescriptModule(override) { tsModule = override; } +function normalize(fileName) { + return fileName.split("\\").join("/"); +} + var LanguageServiceHost = (function () { function LanguageServiceHost(parsedConfig) { this.parsedConfig = parsedConfig; @@ -132,15 +136,15 @@ var LanguageServiceHost = (function () { this.versions = {}; }; LanguageServiceHost.prototype.setSnapshot = function (fileName, data) { - fileName = this.normalize(fileName); + fileName = normalize(fileName); var snapshot = tsModule.ScriptSnapshot.fromString(data); this.snapshots[fileName] = snapshot; this.versions[fileName] = (this.versions[fileName] || 0) + 1; return snapshot; }; LanguageServiceHost.prototype.getScriptSnapshot = function (fileName) { - fileName = this.normalize(fileName); - if (lodash.has(this.snapshots, fileName)) + fileName = normalize(fileName); + if (_.has(this.snapshots, fileName)) return this.snapshots[fileName]; if (fs.existsSync(fileName)) { this.snapshots[fileName] = tsModule.ScriptSnapshot.fromString(tsModule.sys.readFile(fileName)); @@ -153,7 +157,7 @@ var LanguageServiceHost = (function () { return this.cwd; }; LanguageServiceHost.prototype.getScriptVersion = function (fileName) { - fileName = this.normalize(fileName); + fileName = normalize(fileName); return (this.versions[fileName] || 0).toString(); }; LanguageServiceHost.prototype.getScriptFileNames = function () { @@ -186,9 +190,6 @@ var LanguageServiceHost = (function () { LanguageServiceHost.prototype.getDirectories = function (directoryName) { return tsModule.sys.getDirectories(directoryName); }; - LanguageServiceHost.prototype.normalize = function (fileName) { - return fileName.split("\\").join("/"); - }; return LanguageServiceHost; }()); @@ -230,7 +231,7 @@ var RollingCache = (function () { return false; if (!fs.existsSync(this.oldCacheRoot)) return names.length === 0; // empty folder matches - return lodash.isEqual(fs.readdirSync(this.oldCacheRoot).sort(), names.sort()); + return _.isEqual(fs.readdirSync(this.oldCacheRoot).sort(), names.sort()); }; /** * @returns data for name, must exist in old cache (or either old of new cache if checkNewCache is true) @@ -266,7 +267,7 @@ var RollingCache = (function () { }()); function convertDiagnostic(type, data) { - return lodash.map(data, function (diagnostic) { + return _.map(data, function (diagnostic) { var entry = { flatMessage: tsModule.flattenDiagnosticMessageText(diagnostic.messageText, "\n"), category: diagnostic.category, @@ -298,10 +299,10 @@ var TsCache = (function () { }); this.dependencyTree = new graphlib.Graph({ directed: true }); this.dependencyTree.setDefaultNodeLabel(function (_node) { return ({ dirty: false }); }); - var automaticTypes = lodash.map(tsModule.getAutomaticTypeDirectiveNames(options, tsModule.sys), function (entry) { return tsModule.resolveTypeReferenceDirective(entry, undefined, options, tsModule.sys); }) + var automaticTypes = _.map(tsModule.getAutomaticTypeDirectiveNames(options, tsModule.sys), function (entry) { return tsModule.resolveTypeReferenceDirective(entry, undefined, options, tsModule.sys); }) .filter(function (entry) { return entry.resolvedTypeReferenceDirective && entry.resolvedTypeReferenceDirective.resolvedFileName; }) .map(function (entry) { return entry.resolvedTypeReferenceDirective.resolvedFileName; }); - this.ambientTypes = lodash.filter(rootFilenames, function (file) { return lodash.endsWith(file, ".d.ts"); }) + 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(); @@ -321,11 +322,11 @@ var TsCache = (function () { TsCache.prototype.walkTree = function (cb) { var acyclic = graphlib.alg.isAcyclic(this.dependencyTree); if (acyclic) { - lodash.each(graphlib.alg.topsort(this.dependencyTree), function (id) { return cb(id); }); + _.each(graphlib.alg.topsort(this.dependencyTree), function (id) { return cb(id); }); return; } this.context.info(colors_safe.yellow("import tree has cycles")); - lodash.each(this.dependencyTree.nodes(), function (id) { return cb(id); }); + _.each(this.dependencyTree.nodes(), function (id) { return cb(id); }); }; TsCache.prototype.done = function () { this.context.info(colors_safe.blue("rolling caches")); @@ -359,7 +360,7 @@ var TsCache = (function () { TsCache.prototype.checkAmbientTypes = function () { var _this = this; this.context.debug(colors_safe.blue("Ambient types:")); - var typeNames = lodash.filter(this.ambientTypes, function (snapshot) { return snapshot.snapshot !== undefined; }) + var typeNames = _.filter(this.ambientTypes, function (snapshot) { return snapshot.snapshot !== undefined; }) .map(function (snapshot) { _this.context.debug(" " + snapshot.id); return _this.makeName(snapshot.id, snapshot.snapshot); @@ -368,7 +369,7 @@ var TsCache = (function () { this.ambientTypesDirty = !this.typesCache.match(typeNames); if (this.ambientTypesDirty) this.context.info(colors_safe.yellow("ambient types changed, redoing all semantic diagnostics")); - lodash.each(typeNames, function (name) { return _this.typesCache.touch(name); }); + _.each(typeNames, function (name) { return _this.typesCache.touch(name); }); }; TsCache.prototype.getDiagnostics = function (type, cache, id, snapshot, check) { var name = this.makeName(id, snapshot); @@ -405,7 +406,7 @@ var TsCache = (function () { if (this.ambientTypesDirty) return true; var dependencies = graphlib.alg.dijkstra(this.dependencyTree, id); - return lodash.some(dependencies, function (dependency, node) { + return _.some(dependencies, function (dependency, node) { if (!node || dependency.distance === Infinity) return false; var l = _this.dependencyTree.node(node); @@ -423,7 +424,7 @@ var TsCache = (function () { }()); function printDiagnostics(context, diagnostics) { - lodash.each(diagnostics, function (diagnostic) { + _.each(diagnostics, function (diagnostic) { var print; var color; var category; @@ -455,7 +456,7 @@ function printDiagnostics(context, diagnostics) { function getOptionsOverrides(_a, tsConfigJson) { var useTsconfigDeclarationDir = _a.useTsconfigDeclarationDir; - var declaration = lodash.get(tsConfigJson, "compilerOptions.declaration", false); + var declaration = _.get(tsConfigJson, "compilerOptions.declaration", false); return __assign({ module: tsModule.ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd() }, (!declaration || useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() })); } @@ -507,7 +508,7 @@ function typescript(options) { return _cache; }; var pluginOptions = __assign({}, options); - lodash.defaults(pluginOptions, { + _.defaults(pluginOptions, { check: true, verbosity: VerbosityLevel.Warning, clean: false, @@ -549,7 +550,7 @@ function typescript(options) { if (result.resolvedModule && result.resolvedModule.resolvedFileName) { if (filter$$1(result.resolvedModule.resolvedFileName)) cache().setDependency(result.resolvedModule.resolvedFileName, importer); - if (lodash.endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) + if (_.endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) return null; var resolved = pluginOptions.rollupCommonJSResolveHack ? resolve.sync(result.resolvedModule.resolvedFileName) @@ -577,7 +578,7 @@ function typescript(options) { if (output.emitSkipped) { noErrors = false; // always checking on fatal errors, even if options.check is set to false - var diagnostics = lodash.concat(cache().getSyntacticDiagnostics(id, snapshot, function () { + var diagnostics = _.concat(cache().getSyntacticDiagnostics(id, snapshot, function () { return service.getSyntacticDiagnostics(id); }), cache().getSemanticDiagnostics(id, snapshot, function () { return service.getSemanticDiagnostics(id); @@ -585,12 +586,12 @@ function typescript(options) { printDiagnostics(contextWrapper, diagnostics); // since no output was generated, aborting compilation cache().done(); - if (lodash.isFunction(_this.error)) + if (_.isFunction(_this.error)) _this.error(colors_safe.red("failed to transpile '" + id + "'")); } - var transpiled = lodash.find(output.outputFiles, function (entry) { return lodash.endsWith(entry.name, ".js") || lodash.endsWith(entry.name, ".jsx"); }); - var map$$1 = lodash.find(output.outputFiles, function (entry) { return lodash.endsWith(entry.name, ".map"); }); - var dts = lodash.find(output.outputFiles, function (entry) { return lodash.endsWith(entry.name, ".d.ts"); }); + var transpiled = _.find(output.outputFiles, function (entry) { return _.endsWith(entry.name, ".js") || _.endsWith(entry.name, ".jsx"); }); + var map$$1 = _.find(output.outputFiles, function (entry) { return _.endsWith(entry.name, ".map"); }); + var dts = _.find(output.outputFiles, function (entry) { return _.endsWith(entry.name, ".d.ts"); }); return { code: transpiled ? transpiled.text : undefined, map: map$$1 ? JSON.parse(map$$1.text) : { mappings: "" }, @@ -598,7 +599,7 @@ function typescript(options) { }; }); if (pluginOptions.check) { - var diagnostics = lodash.concat(cache().getSyntacticDiagnostics(id, snapshot, function () { + var diagnostics = _.concat(cache().getSyntacticDiagnostics(id, snapshot, function () { return service.getSyntacticDiagnostics(id); }), cache().getSemanticDiagnostics(id, snapshot, function () { return service.getSemanticDiagnostics(id); @@ -608,13 +609,15 @@ function typescript(options) { printDiagnostics(contextWrapper, diagnostics); } if (result && result.dts) { - declarations[result.dts.name] = result.dts; + var key = normalize(id); + declarations[key] = result.dts; + context.debug(colors_safe.blue("generated declarations") + " for '" + key + "'"); result.dts = undefined; } return result; }, ongenerate: function (bundleOptions) { - targetCount = lodash.get(bundleOptions, "targets.length", 1); + targetCount = _.get(bundleOptions, "targets.length", 1); if (round >= targetCount) { watchMode = true; round = 0; @@ -623,7 +626,7 @@ function typescript(options) { if (watchMode && round === 0) { context.debug("running in watch mode"); cache().walkTree(function (id) { - var diagnostics = lodash.concat(convertDiagnostic("syntax", service.getSyntacticDiagnostics(id)), convertDiagnostic("semantic", service.getSemanticDiagnostics(id))); + var diagnostics = _.concat(convertDiagnostic("syntax", service.getSyntacticDiagnostics(id)), convertDiagnostic("semantic", service.getSemanticDiagnostics(id))); printDiagnostics(context, diagnostics); }); } @@ -634,23 +637,36 @@ function typescript(options) { }, onwrite: function (_a) { var dest = _a.dest; - var baseDeclarationDir = parsedConfig.options.outDir; - lodash.each(declarations, function (_a) { - var name = _a.name, text = _a.text, writeByteOrderMark = _a.writeByteOrderMark; - var writeToPath; - // If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options, - // use the path provided by Typescript's LanguageService. - if (!dest || pluginOptions.useTsconfigDeclarationDir) - writeToPath = name; - else { - // Otherwise, take the directory name from the path and make sure it is absolute. - var destDirname = path.dirname(dest); - var destDirectory = path.isAbsolute(dest) ? destDirname : path.join(process.cwd(), destDirname); - writeToPath = path.join(destDirectory, path.relative(baseDeclarationDir, name)); - } - // Write the declaration file to disk. - tsModule.sys.writeFile(writeToPath, text, writeByteOrderMark); - }); + if (parsedConfig.options.declaration) { + _.each(parsedConfig.fileNames, function (name) { + var key = normalize(name); + if (_.has(declarations, key) || !filter$$1(key)) + return; + context.debug("generating missed declarations for '" + key + "'"); + var output = service.getEmitOutput(key, true); + var dts = _.find(output.outputFiles, function (entry) { return _.endsWith(entry.name, ".d.ts"); }); + if (dts) + declarations[key] = dts; + }); + var baseDeclarationDir_1 = parsedConfig.options.outDir; + _.each(declarations, function (_a, key) { + var name = _a.name, text = _a.text, writeByteOrderMark = _a.writeByteOrderMark; + var writeToPath; + // If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options, + // use the path provided by Typescript's LanguageService. + if (!dest || pluginOptions.useTsconfigDeclarationDir) + writeToPath = name; + else { + // Otherwise, take the directory name from the path and make sure it is absolute. + var destDirname = path.dirname(dest); + var destDirectory = path.isAbsolute(dest) ? destDirname : path.join(process.cwd(), destDirname); + writeToPath = path.join(destDirectory, path.relative(baseDeclarationDir_1, name)); + } + context.debug(colors_safe.blue("writing declarations") + " for '" + key + "' to '" + writeToPath + "'"); + // Write the declaration file to disk. + tsModule.sys.writeFile(writeToPath, text, writeByteOrderMark); + }); + } }, }; } diff --git a/dist/rollup-plugin-typescript2.es.js b/dist/rollup-plugin-typescript2.es.js index 625c83f7..c989fd24 100644 --- a/dist/rollup-plugin-typescript2.es.js +++ b/dist/rollup-plugin-typescript2.es.js @@ -1,5 +1,6 @@ /* eslint-disable */ import { concat, defaults, each, endsWith, filter, find, get, has, isEqual, isFunction, map, some } from 'lodash'; +import * as _ from 'lodash'; import { existsSync, readFileSync, readdirSync, renameSync } from 'fs'; import { Graph, alg } from 'graphlib'; import { sha1 } from 'object-hash'; @@ -119,6 +120,10 @@ function setTypescriptModule(override) { tsModule = override; } +function normalize(fileName) { + return fileName.split("\\").join("/"); +} + var LanguageServiceHost = (function () { function LanguageServiceHost(parsedConfig) { this.parsedConfig = parsedConfig; @@ -131,14 +136,14 @@ var LanguageServiceHost = (function () { this.versions = {}; }; LanguageServiceHost.prototype.setSnapshot = function (fileName, data) { - fileName = this.normalize(fileName); + fileName = normalize(fileName); var snapshot = tsModule.ScriptSnapshot.fromString(data); this.snapshots[fileName] = snapshot; this.versions[fileName] = (this.versions[fileName] || 0) + 1; return snapshot; }; LanguageServiceHost.prototype.getScriptSnapshot = function (fileName) { - fileName = this.normalize(fileName); + fileName = normalize(fileName); if (has(this.snapshots, fileName)) return this.snapshots[fileName]; if (existsSync(fileName)) { @@ -152,7 +157,7 @@ var LanguageServiceHost = (function () { return this.cwd; }; LanguageServiceHost.prototype.getScriptVersion = function (fileName) { - fileName = this.normalize(fileName); + fileName = normalize(fileName); return (this.versions[fileName] || 0).toString(); }; LanguageServiceHost.prototype.getScriptFileNames = function () { @@ -185,9 +190,6 @@ var LanguageServiceHost = (function () { LanguageServiceHost.prototype.getDirectories = function (directoryName) { return tsModule.sys.getDirectories(directoryName); }; - LanguageServiceHost.prototype.normalize = function (fileName) { - return fileName.split("\\").join("/"); - }; return LanguageServiceHost; }()); @@ -607,7 +609,9 @@ function typescript(options) { printDiagnostics(contextWrapper, diagnostics); } if (result && result.dts) { - declarations[result.dts.name] = result.dts; + var key = normalize(id); + declarations[key] = result.dts; + context.debug(blue("generated declarations") + " for '" + key + "'"); result.dts = undefined; } return result; @@ -633,23 +637,36 @@ function typescript(options) { }, onwrite: function (_a) { var dest = _a.dest; - var baseDeclarationDir = parsedConfig.options.outDir; - each(declarations, function (_a) { - var name = _a.name, text = _a.text, writeByteOrderMark = _a.writeByteOrderMark; - var writeToPath; - // If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options, - // use the path provided by Typescript's LanguageService. - if (!dest || pluginOptions.useTsconfigDeclarationDir) - writeToPath = name; - else { - // Otherwise, take the directory name from the path and make sure it is absolute. - var destDirname = dirname(dest); - var destDirectory = isAbsolute(dest) ? destDirname : join(process.cwd(), destDirname); - writeToPath = join(destDirectory, relative(baseDeclarationDir, name)); - } - // Write the declaration file to disk. - tsModule.sys.writeFile(writeToPath, text, writeByteOrderMark); - }); + if (parsedConfig.options.declaration) { + each(parsedConfig.fileNames, function (name) { + var key = normalize(name); + if (has(declarations, key) || !filter$$1(key)) + return; + context.debug("generating missed declarations for '" + key + "'"); + var output = service.getEmitOutput(key, true); + var dts = find(output.outputFiles, function (entry) { return endsWith(entry.name, ".d.ts"); }); + if (dts) + declarations[key] = dts; + }); + var baseDeclarationDir_1 = parsedConfig.options.outDir; + each(declarations, function (_a, key) { + var name = _a.name, text = _a.text, writeByteOrderMark = _a.writeByteOrderMark; + var writeToPath; + // If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options, + // use the path provided by Typescript's LanguageService. + if (!dest || pluginOptions.useTsconfigDeclarationDir) + writeToPath = name; + else { + // Otherwise, take the directory name from the path and make sure it is absolute. + var destDirname = dirname(dest); + var destDirectory = isAbsolute(dest) ? destDirname : join(process.cwd(), destDirname); + writeToPath = join(destDirectory, relative(baseDeclarationDir_1, name)); + } + context.debug(blue("writing declarations") + " for '" + key + "' to '" + writeToPath + "'"); + // Write the declaration file to disk. + tsModule.sys.writeFile(writeToPath, text, writeByteOrderMark); + }); + } }, }; } diff --git a/src/get-options-overrides.ts b/src/get-options-overrides.ts index df52d37a..99df7f40 100644 --- a/src/get-options-overrides.ts +++ b/src/get-options-overrides.ts @@ -1,11 +1,11 @@ import { tsModule } from "./tsproxy"; import * as tsTypes from "typescript"; import { IOptions } from "./ioptions"; -import { get } from "lodash"; +import * as _ from "lodash"; export function getOptionsOverrides({ useTsconfigDeclarationDir }: IOptions, tsConfigJson?: any): tsTypes.CompilerOptions { - const declaration = get(tsConfigJson, "compilerOptions.declaration", false); + const declaration = _.get(tsConfigJson, "compilerOptions.declaration", false); return { module: tsModule.ModuleKind.ES2015, noEmitHelpers: true, diff --git a/src/host.ts b/src/host.ts index 3e70d383..609d31c1 100644 --- a/src/host.ts +++ b/src/host.ts @@ -1,8 +1,8 @@ - import { tsModule } from "./tsproxy"; import * as tsTypes from "typescript"; import { existsSync } from "fs"; -import { has } from "lodash"; +import * as _ from "lodash"; +import { normalize } from "./normalize"; export class LanguageServiceHost implements tsTypes.LanguageServiceHost { @@ -22,7 +22,7 @@ export class LanguageServiceHost implements tsTypes.LanguageServiceHost public setSnapshot(fileName: string, data: string): tsTypes.IScriptSnapshot { - fileName = this.normalize(fileName); + fileName = normalize(fileName); const snapshot = tsModule.ScriptSnapshot.fromString(data); this.snapshots[fileName] = snapshot; @@ -32,9 +32,9 @@ export class LanguageServiceHost implements tsTypes.LanguageServiceHost public getScriptSnapshot(fileName: string): tsTypes.IScriptSnapshot | undefined { - fileName = this.normalize(fileName); + fileName = normalize(fileName); - if (has(this.snapshots, fileName)) + if (_.has(this.snapshots, fileName)) return this.snapshots[fileName]; if (existsSync(fileName)) @@ -54,7 +54,7 @@ export class LanguageServiceHost implements tsTypes.LanguageServiceHost public getScriptVersion(fileName: string) { - fileName = this.normalize(fileName); + fileName = normalize(fileName); return (this.versions[fileName] || 0).toString(); } @@ -108,9 +108,4 @@ export class LanguageServiceHost implements tsTypes.LanguageServiceHost { return tsModule.sys.getDirectories(directoryName); } - - private normalize(fileName: string) - { - return fileName.split("\\").join("/"); - } } diff --git a/src/index.ts b/src/index.ts index a495719c..ca891a45 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import { TsCache, convertDiagnostic, ICode } from "./tscache"; import { tsModule, setTypescriptModule } from "./tsproxy"; import * as tsTypes from "typescript"; import * as resolve from "resolve"; -import { defaults, endsWith, concat, find, isFunction, get, each } from "lodash"; +import * as _ from "lodash"; import { IRollupOptions } from "./irollup-options"; import { IOptions } from "./ioptions"; import { Partial } from "./partial"; @@ -14,6 +14,7 @@ import { printDiagnostics } from "./print-diagnostics"; import { TSLIB, tslibSource } from "./tslib"; import { blue, red, yellow } from "colors/safe"; import { join, relative, dirname, isAbsolute } from "path"; +import { normalize } from "./normalize"; export default function typescript(options?: Partial) { @@ -42,7 +43,7 @@ export default function typescript(options?: Partial) const pluginOptions = { ...options } as IOptions; - defaults(pluginOptions, + _.defaults(pluginOptions, { check: true, verbosity: VerbosityLevel.Warning, @@ -105,7 +106,7 @@ export default function typescript(options?: Partial) if (filter(result.resolvedModule.resolvedFileName)) cache().setDependency(result.resolvedModule.resolvedFileName, importer); - if (endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) + if (_.endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) return null; const resolved = pluginOptions.rollupCommonJSResolveHack @@ -148,7 +149,7 @@ export default function typescript(options?: Partial) noErrors = false; // always checking on fatal errors, even if options.check is set to false - const diagnostics = concat( + const diagnostics = _.concat( cache().getSyntacticDiagnostics(id, snapshot, () => { return service.getSyntacticDiagnostics(id); @@ -162,13 +163,13 @@ export default function typescript(options?: Partial) // since no output was generated, aborting compilation cache().done(); - if (isFunction(this.error)) + if (_.isFunction(this.error)) this.error(red(`failed to transpile '${id}'`)); } - const transpiled = find(output.outputFiles, (entry) => endsWith(entry.name, ".js") || endsWith(entry.name, ".jsx")); - const map = find(output.outputFiles, (entry) => endsWith(entry.name, ".map")); - const dts = find(output.outputFiles, (entry) => endsWith(entry.name, ".d.ts")); + const transpiled = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".js") || _.endsWith(entry.name, ".jsx")); + const map = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".map")); + const dts = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".d.ts")); return { code: transpiled ? transpiled.text : undefined, @@ -179,7 +180,7 @@ export default function typescript(options?: Partial) if (pluginOptions.check) { - const diagnostics = concat( + const diagnostics = _.concat( cache().getSyntacticDiagnostics(id, snapshot, () => { return service.getSyntacticDiagnostics(id); @@ -198,7 +199,9 @@ export default function typescript(options?: Partial) if (result && result.dts) { - declarations[result.dts.name] = result.dts; + const key = normalize(id); + declarations[key] = result.dts; + context.debug(`${blue("generated declarations")} for '${key}'`); result.dts = undefined; } @@ -207,7 +210,7 @@ export default function typescript(options?: Partial) ongenerate(bundleOptions: any): void { - targetCount = get(bundleOptions, "targets.length", 1); + targetCount = _.get(bundleOptions, "targets.length", 1); if (round >= targetCount) // ongenerate() is called for each target { @@ -222,7 +225,7 @@ export default function typescript(options?: Partial) cache().walkTree((id) => { - const diagnostics = concat( + const diagnostics = _.concat( convertDiagnostic("syntax", service.getSyntacticDiagnostics(id)), convertDiagnostic("semantic", service.getSemanticDiagnostics(id)), ); @@ -241,25 +244,42 @@ export default function typescript(options?: Partial) onwrite({ dest }: IRollupOptions) { - const baseDeclarationDir = parsedConfig.options.outDir; - each(declarations, ({ name, text, writeByteOrderMark }) => + if (parsedConfig.options.declaration) { - let writeToPath: string; - // If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options, - // use the path provided by Typescript's LanguageService. - if (!dest || pluginOptions.useTsconfigDeclarationDir) - writeToPath = name; - else + _.each(parsedConfig.fileNames, (name) => { - // Otherwise, take the directory name from the path and make sure it is absolute. - const destDirname = dirname(dest); - const destDirectory = isAbsolute(dest) ? destDirname : join(process.cwd(), destDirname); - writeToPath = join(destDirectory, relative(baseDeclarationDir!, name)); - } + const key = normalize(name); + if (_.has(declarations, key) || !filter(key)) + return; + context.debug(`generating missed declarations for '${key}'`); + const output = service.getEmitOutput(key, true); + const dts = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".d.ts")); + if (dts) + declarations[key] = dts; + }); - // Write the declaration file to disk. - tsModule.sys.writeFile(writeToPath, text, writeByteOrderMark); - }); + const baseDeclarationDir = parsedConfig.options.outDir; + _.each(declarations, ({ name, text, writeByteOrderMark }, key) => + { + let writeToPath: string; + // If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options, + // use the path provided by Typescript's LanguageService. + if (!dest || pluginOptions.useTsconfigDeclarationDir) + writeToPath = name; + else + { + // Otherwise, take the directory name from the path and make sure it is absolute. + const destDirname = dirname(dest); + const destDirectory = isAbsolute(dest) ? destDirname : join(process.cwd(), destDirname); + writeToPath = join(destDirectory, relative(baseDeclarationDir!, name)); + } + + context.debug(`${blue("writing declarations")} for '${key}' to '${writeToPath}'`); + + // Write the declaration file to disk. + tsModule.sys.writeFile(writeToPath, text, writeByteOrderMark); + }); + } }, }; } diff --git a/src/normalize.ts b/src/normalize.ts new file mode 100644 index 00000000..80ce354f --- /dev/null +++ b/src/normalize.ts @@ -0,0 +1,4 @@ +export function normalize(fileName: string) +{ + return fileName.split("\\").join("/"); +} diff --git a/src/print-diagnostics.ts b/src/print-diagnostics.ts index 35cbe8db..c9830967 100644 --- a/src/print-diagnostics.ts +++ b/src/print-diagnostics.ts @@ -1,12 +1,12 @@ import { tsModule } from "./tsproxy"; import { red, white, yellow } from "colors/safe"; -import { each } from "lodash"; import { IContext } from "./context"; import { IDiagnostics } from "./tscache"; +import * as _ from "lodash"; export function printDiagnostics(context: IContext, diagnostics: IDiagnostics[]): void { - each(diagnostics, (diagnostic) => + _.each(diagnostics, (diagnostic) => { let print; let color; diff --git a/src/rollingcache.ts b/src/rollingcache.ts index fb00538c..a041b232 100644 --- a/src/rollingcache.ts +++ b/src/rollingcache.ts @@ -1,7 +1,7 @@ import { ICache } from "./icache"; import { emptyDirSync, ensureFileSync, readJsonSync, removeSync, writeJsonSync } from "fs-extra"; import { existsSync, readdirSync, renameSync } from "fs"; -import { isEqual } from "lodash"; +import * as _ from "lodash"; /** * Saves data in new cache folder or reads it from old one. @@ -56,7 +56,7 @@ export class RollingCache implements ICache if (!existsSync(this.oldCacheRoot)) return names.length === 0; // empty folder matches - return isEqual(readdirSync(this.oldCacheRoot).sort(), names.sort()); + return _.isEqual(readdirSync(this.oldCacheRoot).sort(), names.sort()); } /** diff --git a/src/rollupcontext.ts b/src/rollupcontext.ts index 3417ae3d..13bcb7f4 100644 --- a/src/rollupcontext.ts +++ b/src/rollupcontext.ts @@ -1,5 +1,5 @@ import { IContext, IRollupContext, VerbosityLevel } from "./context"; -import { isFunction } from "lodash"; +import * as _ from "lodash"; export class RollupContext implements IContext { @@ -7,7 +7,7 @@ export class RollupContext implements IContext constructor(private verbosity: VerbosityLevel, private bail: boolean, private context: IRollupContext, private prefix: string = "") { - this.hasContext = isFunction(this.context.warn) && isFunction(this.context.error); + this.hasContext = _.isFunction(this.context.warn) && _.isFunction(this.context.error); } public warn(message: string): void diff --git a/src/tscache.ts b/src/tscache.ts index 20c1f409..2773347c 100644 --- a/src/tscache.ts +++ b/src/tscache.ts @@ -3,7 +3,7 @@ import { Graph, alg } from "graphlib"; import { sha1 } from "object-hash"; import { RollingCache } from "./rollingcache"; import { ICache } from "./icache"; -import { map, endsWith, filter, each, some } from "lodash"; +import * as _ from "lodash"; import { tsModule } from "./tsproxy"; import * as tsTypes from "typescript"; import { blue, yellow, green } from "colors/safe"; @@ -38,7 +38,7 @@ interface ITypeSnapshot export function convertDiagnostic(type: string, data: tsTypes.Diagnostic[]): IDiagnostics[] { - return map(data, (diagnostic) => + return _.map(data, (diagnostic) => { const entry: IDiagnostics = { @@ -83,11 +83,11 @@ export class TsCache this.dependencyTree = new Graph({ directed: true }); this.dependencyTree.setDefaultNodeLabel((_node: string) => ({ dirty: false })); - const automaticTypes = map(tsModule.getAutomaticTypeDirectiveNames(options, tsModule.sys), (entry) => tsModule.resolveTypeReferenceDirective(entry, undefined, options, tsModule.sys)) + const automaticTypes = _.map(tsModule.getAutomaticTypeDirectiveNames(options, tsModule.sys), (entry) => tsModule.resolveTypeReferenceDirective(entry, undefined, options, tsModule.sys)) .filter((entry) => entry.resolvedTypeReferenceDirective && entry.resolvedTypeReferenceDirective.resolvedFileName) .map((entry) => entry.resolvedTypeReferenceDirective!.resolvedFileName!); - this.ambientTypes = filter(rootFilenames, (file) => endsWith(file, ".d.ts")) + this.ambientTypes = _.filter(rootFilenames, (file) => _.endsWith(file, ".d.ts")) .concat(automaticTypes) .map((id) => ({ id, snapshot: this.host.getScriptSnapshot(id) })); @@ -118,13 +118,13 @@ export class TsCache if (acyclic) { - each(alg.topsort(this.dependencyTree), (id: string) => cb(id)); + _.each(alg.topsort(this.dependencyTree), (id: string) => cb(id)); return; } this.context.info(yellow("import tree has cycles")); - each(this.dependencyTree.nodes(), (id: string) => cb(id)); + _.each(this.dependencyTree.nodes(), (id: string) => cb(id)); } public done() @@ -173,7 +173,7 @@ export class TsCache private checkAmbientTypes(): void { this.context.debug(blue("Ambient types:")); - const typeNames = filter(this.ambientTypes, (snapshot) => snapshot.snapshot !== undefined) + const typeNames = _.filter(this.ambientTypes, (snapshot) => snapshot.snapshot !== undefined) .map((snapshot) => { this.context.debug(` ${snapshot.id}`); @@ -185,7 +185,7 @@ export class TsCache if (this.ambientTypesDirty) this.context.info(yellow("ambient types changed, redoing all semantic diagnostics")); - each(typeNames, (name) => this.typesCache.touch(name)); + _.each(typeNames, (name) => this.typesCache.touch(name)); } private getDiagnostics(type: string, cache: ICache, id: string, snapshot: tsTypes.IScriptSnapshot, check: () => tsTypes.Diagnostic[]): IDiagnostics[] @@ -240,7 +240,7 @@ export class TsCache const dependencies = alg.dijkstra(this.dependencyTree, id); - return some(dependencies, (dependency, node) => + return _.some(dependencies, (dependency, node) => { if (!node || dependency.distance === Infinity) return false;