rollup-plugin-typescript2
Advanced tools
Comparing version 0.5.0 to 0.5.1
@@ -1,3 +0,3 @@ | ||
import { CompilerOptions } from "typescript"; | ||
import * as tsTypes from "typescript"; | ||
import { IOptions } from "./ioptions"; | ||
export declare function getOptionsOverrides({useTsconfigDeclarationDir}: IOptions, tsConfigJson?: any): CompilerOptions; | ||
export declare function getOptionsOverrides({useTsconfigDeclarationDir}: IOptions, tsConfigJson?: any): tsTypes.CompilerOptions; |
@@ -1,3 +0,3 @@ | ||
import { LanguageServiceHost as TypescriptLanguageServiceHost, IScriptSnapshot, ParsedCommandLine, CompilerOptions } from "typescript"; | ||
export declare class LanguageServiceHost implements TypescriptLanguageServiceHost { | ||
import * as tsTypes from "typescript"; | ||
export declare class LanguageServiceHost implements tsTypes.LanguageServiceHost { | ||
private parsedConfig; | ||
@@ -7,11 +7,11 @@ private cwd; | ||
private versions; | ||
constructor(parsedConfig: ParsedCommandLine); | ||
constructor(parsedConfig: tsTypes.ParsedCommandLine); | ||
reset(): void; | ||
setSnapshot(fileName: string, data: string): IScriptSnapshot; | ||
getScriptSnapshot(fileName: string): IScriptSnapshot | undefined; | ||
setSnapshot(fileName: string, data: string): tsTypes.IScriptSnapshot; | ||
getScriptSnapshot(fileName: string): tsTypes.IScriptSnapshot | undefined; | ||
getCurrentDirectory(): string; | ||
getScriptVersion(fileName: string): string; | ||
getScriptFileNames(): string[]; | ||
getCompilationSettings(): CompilerOptions; | ||
getDefaultLibFileName(opts: CompilerOptions): string; | ||
getCompilationSettings(): tsTypes.CompilerOptions; | ||
getDefaultLibFileName(opts: tsTypes.CompilerOptions): string; | ||
useCaseSensitiveFileNames(): boolean; | ||
@@ -24,3 +24,2 @@ readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[]; | ||
getDirectories(directoryName: string): string[]; | ||
private normalize(fileName); | ||
} |
@@ -1,4 +0,4 @@ | ||
import { ParsedCommandLine } from "typescript"; | ||
import * as tsTypes from "typescript"; | ||
import { IContext } from "./context"; | ||
import { IOptions } from "./ioptions"; | ||
export declare function parseTsConfig(tsconfig: string, context: IContext, pluginOptions: IOptions): ParsedCommandLine; | ||
export declare function parseTsConfig(tsconfig: string, context: IContext, pluginOptions: IOptions): tsTypes.ParsedCommandLine; |
/* eslint-disable */ | ||
'use strict'; | ||
var lodash = require('lodash'); | ||
var typescript = require('typescript'); | ||
var _ = require('lodash'); | ||
var fs = require('fs'); | ||
@@ -84,3 +83,3 @@ var graphlib = require('graphlib'); | ||
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); | ||
} | ||
@@ -120,2 +119,11 @@ RollupContext.prototype.warn = function (message) { | ||
var tsModule; | ||
function setTypescriptModule(override) { | ||
tsModule = override; | ||
} | ||
function normalize(fileName) { | ||
return fileName.split("\\").join("/"); | ||
} | ||
var LanguageServiceHost = (function () { | ||
@@ -133,4 +141,4 @@ function LanguageServiceHost(parsedConfig) { | ||
LanguageServiceHost.prototype.setSnapshot = function (fileName, data) { | ||
fileName = this.normalize(fileName); | ||
var snapshot = typescript.ScriptSnapshot.fromString(data); | ||
fileName = normalize(fileName); | ||
var snapshot = tsModule.ScriptSnapshot.fromString(data); | ||
this.snapshots[fileName] = snapshot; | ||
@@ -141,7 +149,7 @@ this.versions[fileName] = (this.versions[fileName] || 0) + 1; | ||
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] = typescript.ScriptSnapshot.fromString(typescript.sys.readFile(fileName)); | ||
this.snapshots[fileName] = tsModule.ScriptSnapshot.fromString(tsModule.sys.readFile(fileName)); | ||
this.versions[fileName] = (this.versions[fileName] || 0) + 1; | ||
@@ -156,3 +164,3 @@ return this.snapshots[fileName]; | ||
LanguageServiceHost.prototype.getScriptVersion = function (fileName) { | ||
fileName = this.normalize(fileName); | ||
fileName = normalize(fileName); | ||
return (this.versions[fileName] || 0).toString(); | ||
@@ -167,15 +175,15 @@ }; | ||
LanguageServiceHost.prototype.getDefaultLibFileName = function (opts) { | ||
return typescript.getDefaultLibFilePath(opts); | ||
return tsModule.getDefaultLibFilePath(opts); | ||
}; | ||
LanguageServiceHost.prototype.useCaseSensitiveFileNames = function () { | ||
return typescript.sys.useCaseSensitiveFileNames; | ||
return tsModule.sys.useCaseSensitiveFileNames; | ||
}; | ||
LanguageServiceHost.prototype.readDirectory = function (path$$1, extensions, exclude, include) { | ||
return typescript.sys.readDirectory(path$$1, extensions, exclude, include); | ||
return tsModule.sys.readDirectory(path$$1, extensions, exclude, include); | ||
}; | ||
LanguageServiceHost.prototype.readFile = function (path$$1, encoding) { | ||
return typescript.sys.readFile(path$$1, encoding); | ||
return tsModule.sys.readFile(path$$1, encoding); | ||
}; | ||
LanguageServiceHost.prototype.fileExists = function (path$$1) { | ||
return typescript.sys.fileExists(path$$1); | ||
return tsModule.sys.fileExists(path$$1); | ||
}; | ||
@@ -186,10 +194,7 @@ LanguageServiceHost.prototype.getTypeRootsVersion = function () { | ||
LanguageServiceHost.prototype.directoryExists = function (directoryName) { | ||
return typescript.sys.directoryExists(directoryName); | ||
return tsModule.sys.directoryExists(directoryName); | ||
}; | ||
LanguageServiceHost.prototype.getDirectories = function (directoryName) { | ||
return typescript.sys.getDirectories(directoryName); | ||
return tsModule.sys.getDirectories(directoryName); | ||
}; | ||
LanguageServiceHost.prototype.normalize = function (fileName) { | ||
return fileName.split("\\").join("/"); | ||
}; | ||
return LanguageServiceHost; | ||
@@ -236,3 +241,3 @@ }()); | ||
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()); | ||
}; | ||
@@ -273,5 +278,5 @@ /** | ||
function convertDiagnostic(type, data) { | ||
return lodash.map(data, function (diagnostic) { | ||
return _.map(data, function (diagnostic) { | ||
var entry = { | ||
flatMessage: typescript.flattenDiagnosticMessageText(diagnostic.messageText, "\n"), | ||
flatMessage: tsModule.flattenDiagnosticMessageText(diagnostic.messageText, "\n"), | ||
category: diagnostic.category, | ||
@@ -302,10 +307,10 @@ code: diagnostic.code, | ||
rollupConfig: this.rollupConfig, | ||
tsVersion: typescript.version, | ||
tsVersion: tsModule.version, | ||
}); | ||
this.dependencyTree = new graphlib.Graph({ directed: true }); | ||
this.dependencyTree.setDefaultNodeLabel(function (_node) { return ({ dirty: false }); }); | ||
var automaticTypes = lodash.map(typescript.getAutomaticTypeDirectiveNames(options, typescript.sys), function (entry) { return typescript.resolveTypeReferenceDirective(entry, undefined, options, typescript.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) | ||
@@ -330,7 +335,7 @@ .map(function (id) { return ({ id: id, snapshot: _this.host.getScriptSnapshot(id) }); }); | ||
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); }); | ||
}; | ||
@@ -369,3 +374,3 @@ TsCache.prototype.done = function () { | ||
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) { | ||
@@ -379,3 +384,3 @@ _this.context.debug(" " + snapshot.id); | ||
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); }); | ||
}; | ||
@@ -417,3 +422,3 @@ TsCache.prototype.getDiagnostics = function (type, cache, id, snapshot, check) { | ||
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) | ||
@@ -436,3 +441,3 @@ return false; | ||
function printDiagnostics(context, diagnostics) { | ||
lodash.each(diagnostics, function (diagnostic) { | ||
_.each(diagnostics, function (diagnostic) { | ||
var print; | ||
@@ -442,3 +447,3 @@ var color; | ||
switch (diagnostic.category) { | ||
case typescript.DiagnosticCategory.Message: | ||
case tsModule.DiagnosticCategory.Message: | ||
print = context.info; | ||
@@ -448,3 +453,3 @@ color = colors_safe.white; | ||
break; | ||
case typescript.DiagnosticCategory.Error: | ||
case tsModule.DiagnosticCategory.Error: | ||
print = context.error; | ||
@@ -454,3 +459,3 @@ color = colors_safe.red; | ||
break; | ||
case typescript.DiagnosticCategory.Warning: | ||
case tsModule.DiagnosticCategory.Warning: | ||
default: | ||
@@ -472,12 +477,12 @@ print = context.warn; | ||
var useTsconfigDeclarationDir = _a.useTsconfigDeclarationDir; | ||
var declaration = lodash.get(tsConfigJson, "compilerOptions.declaration", false); | ||
return __assign({ module: typescript.ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd() }, (!declaration || useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() })); | ||
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() })); | ||
} | ||
function parseTsConfig(tsconfig, context, pluginOptions) { | ||
var fileName = typescript.findConfigFile(process.cwd(), typescript.sys.fileExists, tsconfig); | ||
var fileName = tsModule.findConfigFile(process.cwd(), tsModule.sys.fileExists, tsconfig); | ||
if (!fileName) | ||
throw new Error("couldn't find '" + tsconfig + "' in " + process.cwd()); | ||
var text = typescript.sys.readFile(fileName); | ||
var result = typescript.parseConfigFileTextToJson(fileName, text); | ||
var text = tsModule.sys.readFile(fileName); | ||
var result = tsModule.parseConfigFileTextToJson(fileName, text); | ||
if (result.error) { | ||
@@ -487,3 +492,3 @@ printDiagnostics(context, convertDiagnostic("config", [result.error])); | ||
} | ||
return typescript.parseJsonConfigFileContent(result.config, typescript.sys, path.dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName); | ||
return tsModule.parseJsonConfigFileContent(result.config, tsModule.sys, path.dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName); | ||
} | ||
@@ -504,3 +509,3 @@ | ||
function typescript$1(options) { | ||
function typescript(options) { | ||
// tslint:disable-next-line:no-var-requires | ||
@@ -527,3 +532,3 @@ var createFilter = require("rollup-pluginutils").createFilter; | ||
var pluginOptions = __assign({}, options); | ||
lodash.defaults(pluginOptions, { | ||
_.defaults(pluginOptions, { | ||
check: true, | ||
@@ -539,3 +544,5 @@ verbosity: VerbosityLevel.Warning, | ||
useTsconfigDeclarationDir: false, | ||
typescript: require("typescript"), | ||
}); | ||
setTypescriptModule(pluginOptions.typescript); | ||
return { | ||
@@ -545,8 +552,8 @@ options: function (config) { | ||
context = new ConsoleContext(pluginOptions.verbosity, "rpt2: "); | ||
context.info("Typescript version: " + typescript.version); | ||
context.debug("Plugin Options: " + JSON.stringify(pluginOptions, undefined, 4)); | ||
context.info("Typescript version: " + tsModule.version); | ||
context.debug("Plugin Options: " + JSON.stringify(pluginOptions, function (key, value) { return key === "typescript" ? "version " + value.version : value; }, 4)); | ||
filter$$1 = createFilter(pluginOptions.include, pluginOptions.exclude); | ||
parsedConfig = parseTsConfig(pluginOptions.tsconfig, context, pluginOptions); | ||
servicesHost = new LanguageServiceHost(parsedConfig); | ||
service = typescript.createLanguageService(servicesHost, typescript.createDocumentRegistry()); | ||
service = tsModule.createLanguageService(servicesHost, tsModule.createDocumentRegistry()); | ||
// printing compiler option errors | ||
@@ -566,7 +573,7 @@ if (pluginOptions.check) | ||
// TODO: use module resolution cache | ||
var result = typescript.nodeModuleNameResolver(importee, importer, parsedConfig.options, typescript.sys); | ||
var result = tsModule.nodeModuleNameResolver(importee, importer, parsedConfig.options, tsModule.sys); | ||
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; | ||
@@ -599,3 +606,3 @@ var resolved = pluginOptions.rollupCommonJSResolveHack | ||
// 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); | ||
@@ -608,8 +615,8 @@ }), cache().getSemanticDiagnostics(id, snapshot, function () { | ||
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 { | ||
@@ -622,3 +629,3 @@ code: transpiled ? transpiled.text : undefined, | ||
if (pluginOptions.check) { | ||
var diagnostics = lodash.concat(cache().getSyntacticDiagnostics(id, snapshot, function () { | ||
var diagnostics = _.concat(cache().getSyntacticDiagnostics(id, snapshot, function () { | ||
return service.getSyntacticDiagnostics(id); | ||
@@ -633,3 +640,5 @@ }), cache().getSemanticDiagnostics(id, snapshot, function () { | ||
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; | ||
@@ -640,3 +649,3 @@ } | ||
ongenerate: function (bundleOptions) { | ||
targetCount = lodash.get(bundleOptions, "targets.length", 1); | ||
targetCount = _.get(bundleOptions, "targets.length", 1); | ||
if (round >= targetCount) { | ||
@@ -650,3 +659,3 @@ watchMode = true; | ||
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); | ||
@@ -662,19 +671,32 @@ }); | ||
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. | ||
typescript.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); | ||
}); | ||
} | ||
}, | ||
@@ -684,2 +706,2 @@ }; | ||
module.exports = typescript$1; | ||
module.exports = typescript; |
/* eslint-disable */ | ||
import { concat, defaults, each, endsWith, filter, find, get, has, isEqual, isFunction, map, some } from 'lodash'; | ||
import { DiagnosticCategory, ModuleKind, ScriptSnapshot, createDocumentRegistry, createLanguageService, findConfigFile, flattenDiagnosticMessageText, getAutomaticTypeDirectiveNames, getDefaultLibFilePath, nodeModuleNameResolver, parseConfigFileTextToJson, parseJsonConfigFileContent, resolveTypeReferenceDirective, sys, version } from 'typescript'; | ||
import * as _ from 'lodash'; | ||
import { existsSync, readFileSync, readdirSync, renameSync } from 'fs'; | ||
@@ -118,2 +118,11 @@ import { Graph, alg } from 'graphlib'; | ||
var tsModule; | ||
function setTypescriptModule(override) { | ||
tsModule = override; | ||
} | ||
function normalize(fileName) { | ||
return fileName.split("\\").join("/"); | ||
} | ||
var LanguageServiceHost = (function () { | ||
@@ -131,4 +140,4 @@ function LanguageServiceHost(parsedConfig) { | ||
LanguageServiceHost.prototype.setSnapshot = function (fileName, data) { | ||
fileName = this.normalize(fileName); | ||
var snapshot = ScriptSnapshot.fromString(data); | ||
fileName = normalize(fileName); | ||
var snapshot = tsModule.ScriptSnapshot.fromString(data); | ||
this.snapshots[fileName] = snapshot; | ||
@@ -139,7 +148,7 @@ this.versions[fileName] = (this.versions[fileName] || 0) + 1; | ||
LanguageServiceHost.prototype.getScriptSnapshot = function (fileName) { | ||
fileName = this.normalize(fileName); | ||
fileName = normalize(fileName); | ||
if (has(this.snapshots, fileName)) | ||
return this.snapshots[fileName]; | ||
if (existsSync(fileName)) { | ||
this.snapshots[fileName] = ScriptSnapshot.fromString(sys.readFile(fileName)); | ||
this.snapshots[fileName] = tsModule.ScriptSnapshot.fromString(tsModule.sys.readFile(fileName)); | ||
this.versions[fileName] = (this.versions[fileName] || 0) + 1; | ||
@@ -154,3 +163,3 @@ return this.snapshots[fileName]; | ||
LanguageServiceHost.prototype.getScriptVersion = function (fileName) { | ||
fileName = this.normalize(fileName); | ||
fileName = normalize(fileName); | ||
return (this.versions[fileName] || 0).toString(); | ||
@@ -165,15 +174,15 @@ }; | ||
LanguageServiceHost.prototype.getDefaultLibFileName = function (opts) { | ||
return getDefaultLibFilePath(opts); | ||
return tsModule.getDefaultLibFilePath(opts); | ||
}; | ||
LanguageServiceHost.prototype.useCaseSensitiveFileNames = function () { | ||
return sys.useCaseSensitiveFileNames; | ||
return tsModule.sys.useCaseSensitiveFileNames; | ||
}; | ||
LanguageServiceHost.prototype.readDirectory = function (path$$1, extensions, exclude, include) { | ||
return sys.readDirectory(path$$1, extensions, exclude, include); | ||
return tsModule.sys.readDirectory(path$$1, extensions, exclude, include); | ||
}; | ||
LanguageServiceHost.prototype.readFile = function (path$$1, encoding) { | ||
return sys.readFile(path$$1, encoding); | ||
return tsModule.sys.readFile(path$$1, encoding); | ||
}; | ||
LanguageServiceHost.prototype.fileExists = function (path$$1) { | ||
return sys.fileExists(path$$1); | ||
return tsModule.sys.fileExists(path$$1); | ||
}; | ||
@@ -184,10 +193,7 @@ LanguageServiceHost.prototype.getTypeRootsVersion = function () { | ||
LanguageServiceHost.prototype.directoryExists = function (directoryName) { | ||
return sys.directoryExists(directoryName); | ||
return tsModule.sys.directoryExists(directoryName); | ||
}; | ||
LanguageServiceHost.prototype.getDirectories = function (directoryName) { | ||
return sys.getDirectories(directoryName); | ||
return tsModule.sys.getDirectories(directoryName); | ||
}; | ||
LanguageServiceHost.prototype.normalize = function (fileName) { | ||
return fileName.split("\\").join("/"); | ||
}; | ||
return LanguageServiceHost; | ||
@@ -272,3 +278,3 @@ }()); | ||
var entry = { | ||
flatMessage: flattenDiagnosticMessageText(diagnostic.messageText, "\n"), | ||
flatMessage: tsModule.flattenDiagnosticMessageText(diagnostic.messageText, "\n"), | ||
category: diagnostic.category, | ||
@@ -299,7 +305,7 @@ code: diagnostic.code, | ||
rollupConfig: this.rollupConfig, | ||
tsVersion: version, | ||
tsVersion: tsModule.version, | ||
}); | ||
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); }) | ||
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; }) | ||
@@ -434,3 +440,3 @@ .map(function (entry) { return entry.resolvedTypeReferenceDirective.resolvedFileName; }); | ||
switch (diagnostic.category) { | ||
case DiagnosticCategory.Message: | ||
case tsModule.DiagnosticCategory.Message: | ||
print = context.info; | ||
@@ -440,3 +446,3 @@ color = white; | ||
break; | ||
case DiagnosticCategory.Error: | ||
case tsModule.DiagnosticCategory.Error: | ||
print = context.error; | ||
@@ -446,3 +452,3 @@ color = red; | ||
break; | ||
case DiagnosticCategory.Warning: | ||
case tsModule.DiagnosticCategory.Warning: | ||
default: | ||
@@ -465,11 +471,11 @@ print = context.warn; | ||
var declaration = get(tsConfigJson, "compilerOptions.declaration", false); | ||
return __assign({ module: ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd() }, (!declaration || useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() })); | ||
return __assign({ module: tsModule.ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd() }, (!declaration || useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() })); | ||
} | ||
function parseTsConfig(tsconfig, context, pluginOptions) { | ||
var fileName = findConfigFile(process.cwd(), sys.fileExists, tsconfig); | ||
var fileName = tsModule.findConfigFile(process.cwd(), tsModule.sys.fileExists, tsconfig); | ||
if (!fileName) | ||
throw new Error("couldn't find '" + tsconfig + "' in " + process.cwd()); | ||
var text = sys.readFile(fileName); | ||
var result = parseConfigFileTextToJson(fileName, text); | ||
var text = tsModule.sys.readFile(fileName); | ||
var result = tsModule.parseConfigFileTextToJson(fileName, text); | ||
if (result.error) { | ||
@@ -479,3 +485,3 @@ printDiagnostics(context, convertDiagnostic("config", [result.error])); | ||
} | ||
return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName); | ||
return tsModule.parseJsonConfigFileContent(result.config, tsModule.sys, dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName); | ||
} | ||
@@ -496,3 +502,3 @@ | ||
function typescript$1(options) { | ||
function typescript(options) { | ||
// tslint:disable-next-line:no-var-requires | ||
@@ -530,3 +536,5 @@ var createFilter = require("rollup-pluginutils").createFilter; | ||
useTsconfigDeclarationDir: false, | ||
typescript: require("typescript"), | ||
}); | ||
setTypescriptModule(pluginOptions.typescript); | ||
return { | ||
@@ -536,8 +544,8 @@ options: function (config) { | ||
context = new ConsoleContext(pluginOptions.verbosity, "rpt2: "); | ||
context.info("Typescript version: " + version); | ||
context.debug("Plugin Options: " + JSON.stringify(pluginOptions, undefined, 4)); | ||
context.info("Typescript version: " + tsModule.version); | ||
context.debug("Plugin Options: " + JSON.stringify(pluginOptions, function (key, value) { return key === "typescript" ? "version " + value.version : value; }, 4)); | ||
filter$$1 = createFilter(pluginOptions.include, pluginOptions.exclude); | ||
parsedConfig = parseTsConfig(pluginOptions.tsconfig, context, pluginOptions); | ||
servicesHost = new LanguageServiceHost(parsedConfig); | ||
service = createLanguageService(servicesHost, createDocumentRegistry()); | ||
service = tsModule.createLanguageService(servicesHost, tsModule.createDocumentRegistry()); | ||
// printing compiler option errors | ||
@@ -557,3 +565,3 @@ if (pluginOptions.check) | ||
// TODO: use module resolution cache | ||
var result = nodeModuleNameResolver(importee, importer, parsedConfig.options, sys); | ||
var result = tsModule.nodeModuleNameResolver(importee, importer, parsedConfig.options, tsModule.sys); | ||
if (result.resolvedModule && result.resolvedModule.resolvedFileName) { | ||
@@ -621,3 +629,5 @@ if (filter$$1(result.resolvedModule.resolvedFileName)) | ||
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; | ||
@@ -648,19 +658,32 @@ } | ||
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. | ||
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); | ||
}); | ||
} | ||
}, | ||
@@ -670,2 +693,2 @@ }; | ||
export default typescript$1; | ||
export default typescript; |
import { IContext } from "./context"; | ||
import { Diagnostic, DiagnosticCategory, IScriptSnapshot, OutputFile, LanguageServiceHost, CompilerOptions } from "typescript"; | ||
import * as tsTypes from "typescript"; | ||
export interface ICode { | ||
code: string | undefined; | ||
map: string | undefined; | ||
dts?: OutputFile | undefined; | ||
dts?: tsTypes.OutputFile | undefined; | ||
} | ||
@@ -11,7 +11,7 @@ export interface IDiagnostics { | ||
fileLine?: string; | ||
category: DiagnosticCategory; | ||
category: tsTypes.DiagnosticCategory; | ||
code: number; | ||
type: string; | ||
} | ||
export declare function convertDiagnostic(type: string, data: Diagnostic[]): IDiagnostics[]; | ||
export declare function convertDiagnostic(type: string, data: tsTypes.Diagnostic[]): IDiagnostics[]; | ||
export declare class TsCache { | ||
@@ -31,3 +31,3 @@ private host; | ||
private syntacticDiagnosticsCache; | ||
constructor(host: LanguageServiceHost, cache: string, options: CompilerOptions, rollupConfig: any, rootFilenames: string[], context: IContext); | ||
constructor(host: tsTypes.LanguageServiceHost, cache: string, options: tsTypes.CompilerOptions, rollupConfig: any, rootFilenames: string[], context: IContext); | ||
clean(): void; | ||
@@ -37,5 +37,5 @@ setDependency(importee: string, importer: string): void; | ||
done(): void; | ||
getCompiled(id: string, snapshot: IScriptSnapshot, transform: () => ICode | undefined): ICode | undefined; | ||
getSyntacticDiagnostics(id: string, snapshot: IScriptSnapshot, check: () => Diagnostic[]): IDiagnostics[]; | ||
getSemanticDiagnostics(id: string, snapshot: IScriptSnapshot, check: () => Diagnostic[]): IDiagnostics[]; | ||
getCompiled(id: string, snapshot: tsTypes.IScriptSnapshot, transform: () => ICode | undefined): ICode | undefined; | ||
getSyntacticDiagnostics(id: string, snapshot: tsTypes.IScriptSnapshot, check: () => tsTypes.Diagnostic[]): IDiagnostics[]; | ||
getSemanticDiagnostics(id: string, snapshot: tsTypes.IScriptSnapshot, check: () => tsTypes.Diagnostic[]): IDiagnostics[]; | ||
private checkAmbientTypes(); | ||
@@ -42,0 +42,0 @@ private getDiagnostics(type, cache, id, snapshot, check); |
{ | ||
"name": "rollup-plugin-typescript2", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "Seamless integration between Rollup and TypeScript. Now with errors.", | ||
@@ -5,0 +5,0 @@ "main": "dist/rollup-plugin-typescript2.cjs.js", |
@@ -98,2 +98,6 @@ # rollup-plugin-typescript2 | ||
* `typescript`: typescript module installed with the plugin | ||
When typescript version installed by the plugin (latest 2.x) is unacceptable, you can import your own typescript module and pass it in as `typescript: require("typescript")`. Must be 2.0+, things might break if transpiler interfaces changed enough from what the plugin was built against. | ||
### Declarations | ||
@@ -100,0 +104,0 @@ |
@@ -1,10 +0,11 @@ | ||
import { CompilerOptions, ModuleKind } from "typescript"; | ||
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): CompilerOptions | ||
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: ModuleKind.ES2015, | ||
module: tsModule.ModuleKind.ES2015, | ||
noEmitHelpers: true, | ||
@@ -14,4 +15,4 @@ importHelpers: true, | ||
outDir: process.cwd(), | ||
...(!declaration || useTsconfigDeclarationDir ? {} : {declarationDir: process.cwd()}), | ||
...(!declaration || useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() }), | ||
}; | ||
} |
@@ -1,12 +0,14 @@ | ||
import {LanguageServiceHost as TypescriptLanguageServiceHost, IScriptSnapshot, ParsedCommandLine, ScriptSnapshot, sys, CompilerOptions, getDefaultLibFilePath} from "typescript"; | ||
import {existsSync} from "fs"; | ||
import {has} from "lodash"; | ||
import { tsModule } from "./tsproxy"; | ||
import * as tsTypes from "typescript"; | ||
import { existsSync } from "fs"; | ||
import * as _ from "lodash"; | ||
import { normalize } from "./normalize"; | ||
export class LanguageServiceHost implements TypescriptLanguageServiceHost | ||
export class LanguageServiceHost implements tsTypes.LanguageServiceHost | ||
{ | ||
private cwd = process.cwd(); | ||
private snapshots: { [fileName: string]: IScriptSnapshot } = {}; | ||
private snapshots: { [fileName: string]: tsTypes.IScriptSnapshot } = {}; | ||
private versions: { [fileName: string]: number } = {}; | ||
constructor(private parsedConfig: ParsedCommandLine) | ||
constructor(private parsedConfig: tsTypes.ParsedCommandLine) | ||
{ | ||
@@ -21,7 +23,7 @@ } | ||
public setSnapshot(fileName: string, data: string): IScriptSnapshot | ||
public setSnapshot(fileName: string, data: string): tsTypes.IScriptSnapshot | ||
{ | ||
fileName = this.normalize(fileName); | ||
fileName = normalize(fileName); | ||
const snapshot = ScriptSnapshot.fromString(data); | ||
const snapshot = tsModule.ScriptSnapshot.fromString(data); | ||
this.snapshots[fileName] = snapshot; | ||
@@ -32,7 +34,7 @@ this.versions[fileName] = (this.versions[fileName] || 0) + 1; | ||
public getScriptSnapshot(fileName: string): IScriptSnapshot | undefined | ||
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]; | ||
@@ -42,3 +44,3 @@ | ||
{ | ||
this.snapshots[fileName] = ScriptSnapshot.fromString(sys.readFile(fileName)); | ||
this.snapshots[fileName] = tsModule.ScriptSnapshot.fromString(tsModule.sys.readFile(fileName)); | ||
this.versions[fileName] = (this.versions[fileName] || 0) + 1; | ||
@@ -58,3 +60,3 @@ return this.snapshots[fileName]; | ||
{ | ||
fileName = this.normalize(fileName); | ||
fileName = normalize(fileName); | ||
@@ -69,3 +71,3 @@ return (this.versions[fileName] || 0).toString(); | ||
public getCompilationSettings(): CompilerOptions | ||
public getCompilationSettings(): tsTypes.CompilerOptions | ||
{ | ||
@@ -75,5 +77,5 @@ return this.parsedConfig.options; | ||
public getDefaultLibFileName(opts: CompilerOptions) | ||
public getDefaultLibFileName(opts: tsTypes.CompilerOptions) | ||
{ | ||
return getDefaultLibFilePath(opts); | ||
return tsModule.getDefaultLibFilePath(opts); | ||
} | ||
@@ -83,3 +85,3 @@ | ||
{ | ||
return sys.useCaseSensitiveFileNames; | ||
return tsModule.sys.useCaseSensitiveFileNames; | ||
} | ||
@@ -89,3 +91,3 @@ | ||
{ | ||
return sys.readDirectory(path, extensions, exclude, include); | ||
return tsModule.sys.readDirectory(path, extensions, exclude, include); | ||
} | ||
@@ -95,3 +97,3 @@ | ||
{ | ||
return sys.readFile(path, encoding); | ||
return tsModule.sys.readFile(path, encoding); | ||
} | ||
@@ -101,3 +103,3 @@ | ||
{ | ||
return sys.fileExists(path); | ||
return tsModule.sys.fileExists(path); | ||
} | ||
@@ -112,3 +114,3 @@ | ||
{ | ||
return sys.directoryExists(directoryName); | ||
return tsModule.sys.directoryExists(directoryName); | ||
} | ||
@@ -118,9 +120,4 @@ | ||
{ | ||
return sys.getDirectories(directoryName); | ||
return tsModule.sys.getDirectories(directoryName); | ||
} | ||
private normalize(fileName: string) | ||
{ | ||
return fileName.split("\\").join("/"); | ||
} | ||
} |
125
src/index.ts
@@ -5,5 +5,6 @@ import { RollupContext } from "./rollupcontext"; | ||
import { TsCache, convertDiagnostic, ICode } from "./tscache"; | ||
import { createLanguageService, version, createDocumentRegistry, OutputFile, ParsedCommandLine, sys, LanguageService, nodeModuleNameResolver } from "typescript"; | ||
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"; | ||
@@ -17,2 +18,3 @@ import { IOptions } from "./ioptions"; | ||
import { join, relative, dirname, isAbsolute } from "path"; | ||
import { normalize } from "./normalize"; | ||
@@ -30,7 +32,7 @@ export default function typescript(options?: Partial<IOptions>) | ||
let filter: any; | ||
let parsedConfig: ParsedCommandLine; | ||
let parsedConfig: tsTypes.ParsedCommandLine; | ||
let servicesHost: LanguageServiceHost; | ||
let service: LanguageService; | ||
let service: tsTypes.LanguageService; | ||
let noErrors = true; | ||
const declarations: { [name: string]: OutputFile } = {}; | ||
const declarations: { [name: string]: tsTypes.OutputFile } = {}; | ||
@@ -44,18 +46,22 @@ let _cache: TsCache; | ||
}; | ||
const pluginOptions = { ... options } as IOptions; | ||
defaults(pluginOptions, | ||
{ | ||
check: true, | ||
verbosity: VerbosityLevel.Warning, | ||
clean: false, | ||
cacheRoot: `${process.cwd()}/.rpt2_cache`, | ||
include: [ "*.ts+(|x)", "**/*.ts+(|x)" ], | ||
exclude: [ "*.d.ts", "**/*.d.ts" ], | ||
abortOnError: true, | ||
rollupCommonJSResolveHack: false, | ||
tsconfig: "tsconfig.json", | ||
useTsconfigDeclarationDir: false, | ||
}); | ||
const pluginOptions = { ...options } as IOptions; | ||
_.defaults(pluginOptions, | ||
{ | ||
check: true, | ||
verbosity: VerbosityLevel.Warning, | ||
clean: false, | ||
cacheRoot: `${process.cwd()}/.rpt2_cache`, | ||
include: ["*.ts+(|x)", "**/*.ts+(|x)"], | ||
exclude: ["*.d.ts", "**/*.d.ts"], | ||
abortOnError: true, | ||
rollupCommonJSResolveHack: false, | ||
tsconfig: "tsconfig.json", | ||
useTsconfigDeclarationDir: false, | ||
typescript: require("typescript"), | ||
}); | ||
setTypescriptModule(pluginOptions.typescript); | ||
return { | ||
@@ -68,4 +74,4 @@ | ||
context.info(`Typescript version: ${version}`); | ||
context.debug(`Plugin Options: ${JSON.stringify(pluginOptions, undefined, 4)}`); | ||
context.info(`Typescript version: ${tsModule.version}`); | ||
context.debug(`Plugin Options: ${JSON.stringify(pluginOptions, (key, value) => key === "typescript" ? `version ${(value as typeof tsModule).version}` : value, 4)}`); | ||
@@ -78,3 +84,3 @@ filter = createFilter(pluginOptions.include, pluginOptions.exclude); | ||
service = createLanguageService(servicesHost, createDocumentRegistry()); | ||
service = tsModule.createLanguageService(servicesHost, tsModule.createDocumentRegistry()); | ||
@@ -102,3 +108,3 @@ // printing compiler option errors | ||
// TODO: use module resolution cache | ||
const result = nodeModuleNameResolver(importee, importer, parsedConfig.options, sys); | ||
const result = tsModule.nodeModuleNameResolver(importee, importer, parsedConfig.options, tsModule.sys); | ||
@@ -110,3 +116,3 @@ if (result.resolvedModule && result.resolvedModule.resolvedFileName) | ||
if (endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) | ||
if (_.endsWith(result.resolvedModule.resolvedFileName, ".d.ts")) | ||
return null; | ||
@@ -154,3 +160,3 @@ | ||
// always checking on fatal errors, even if options.check is set to false | ||
const diagnostics = concat( | ||
const diagnostics = _.concat( | ||
cache().getSyntacticDiagnostics(id, snapshot, () => | ||
@@ -169,9 +175,9 @@ { | ||
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")); | ||
@@ -187,3 +193,3 @@ return { | ||
{ | ||
const diagnostics = concat( | ||
const diagnostics = _.concat( | ||
cache().getSyntacticDiagnostics(id, snapshot, () => | ||
@@ -207,3 +213,5 @@ { | ||
{ | ||
declarations[result.dts.name] = result.dts; | ||
const key = normalize(id); | ||
declarations[key] = result.dts; | ||
context.debug(`${blue("generated declarations")} for '${key}'`); | ||
result.dts = undefined; | ||
@@ -217,3 +225,3 @@ } | ||
{ | ||
targetCount = get(bundleOptions, "targets.length", 1); | ||
targetCount = _.get(bundleOptions, "targets.length", 1); | ||
@@ -233,3 +241,3 @@ if (round >= targetCount) // ongenerate() is called for each target | ||
{ | ||
const diagnostics = concat( | ||
const diagnostics = _.concat( | ||
convertDiagnostic("syntax", service.getSyntacticDiagnostics(id)), | ||
@@ -251,25 +259,42 @@ convertDiagnostic("semantic", service.getSemanticDiagnostics(id)), | ||
onwrite({dest}: IRollupOptions) | ||
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. | ||
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); | ||
}); | ||
} | ||
}, | ||
}; | ||
} |
@@ -0,1 +1,3 @@ | ||
import { tsModule } from "./tsproxy"; | ||
export interface IOptions | ||
@@ -13,2 +15,3 @@ { | ||
useTsconfigDeclarationDir: boolean; | ||
typescript: typeof tsModule; | ||
} |
@@ -1,12 +0,13 @@ | ||
import {findConfigFile, parseConfigFileTextToJson, ParsedCommandLine, parseJsonConfigFileContent, sys} from "typescript"; | ||
import {IContext} from "./context"; | ||
import {dirname} from "path"; | ||
import {printDiagnostics} from "./print-diagnostics"; | ||
import {convertDiagnostic} from "./tscache"; | ||
import {getOptionsOverrides} from "./get-options-overrides"; | ||
import {IOptions} from "./ioptions"; | ||
import { tsModule } from "./tsproxy"; | ||
import * as tsTypes from "typescript"; | ||
import { IContext } from "./context"; | ||
import { dirname } from "path"; | ||
import { printDiagnostics } from "./print-diagnostics"; | ||
import { convertDiagnostic } from "./tscache"; | ||
import { getOptionsOverrides } from "./get-options-overrides"; | ||
import { IOptions } from "./ioptions"; | ||
export function parseTsConfig(tsconfig: string, context: IContext, pluginOptions: IOptions): ParsedCommandLine | ||
export function parseTsConfig(tsconfig: string, context: IContext, pluginOptions: IOptions): tsTypes.ParsedCommandLine | ||
{ | ||
const fileName = findConfigFile(process.cwd(), sys.fileExists, tsconfig); | ||
const fileName = tsModule.findConfigFile(process.cwd(), tsModule.sys.fileExists, tsconfig); | ||
@@ -16,6 +17,7 @@ if (!fileName) | ||
const text = sys.readFile(fileName); | ||
const result = parseConfigFileTextToJson(fileName, text); | ||
const text = tsModule.sys.readFile(fileName); | ||
const result = tsModule.parseConfigFileTextToJson(fileName, text); | ||
if (result.error) { | ||
if (result.error) | ||
{ | ||
printDiagnostics(context, convertDiagnostic("config", [result.error])); | ||
@@ -25,3 +27,3 @@ throw new Error(`failed to parse ${fileName}`); | ||
return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName); | ||
return tsModule.parseJsonConfigFileContent(result.config, tsModule.sys, dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName); | ||
} |
@@ -1,10 +0,10 @@ | ||
import {DiagnosticCategory} from "typescript"; | ||
import {red, white, yellow} from "colors/safe"; | ||
import {each} from "lodash"; | ||
import {IContext} from "./context"; | ||
import {IDiagnostics} from "./tscache"; | ||
import { tsModule } from "./tsproxy"; | ||
import { red, white, yellow } from "colors/safe"; | ||
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) => | ||
{ | ||
@@ -16,3 +16,3 @@ let print; | ||
{ | ||
case DiagnosticCategory.Message: | ||
case tsModule.DiagnosticCategory.Message: | ||
print = context.info; | ||
@@ -22,3 +22,3 @@ color = white; | ||
break; | ||
case DiagnosticCategory.Error: | ||
case tsModule.DiagnosticCategory.Error: | ||
print = context.error; | ||
@@ -28,3 +28,3 @@ color = red; | ||
break; | ||
case DiagnosticCategory.Warning: | ||
case tsModule.DiagnosticCategory.Warning: | ||
default: | ||
@@ -31,0 +31,0 @@ print = context.warn; |
import { ICache } from "./icache"; | ||
import {emptyDirSync, ensureFileSync, readJsonSync, removeSync, writeJsonSync} from "fs-extra"; | ||
import {existsSync, readdirSync, renameSync} from "fs"; | ||
import {isEqual} from "lodash"; | ||
import { emptyDirSync, ensureFileSync, readJsonSync, removeSync, writeJsonSync } from "fs-extra"; | ||
import { existsSync, readdirSync, renameSync } from "fs"; | ||
import * as _ from "lodash"; | ||
@@ -10,3 +10,3 @@ /** | ||
*/ | ||
export class RollingCache <DataType> implements ICache<DataType> | ||
export class RollingCache<DataType> implements ICache<DataType> | ||
{ | ||
@@ -60,3 +60,3 @@ private oldCacheRoot: string; | ||
return isEqual(readdirSync(this.oldCacheRoot).sort(), names.sort()); | ||
return _.isEqual(readdirSync(this.oldCacheRoot).sort(), names.sort()); | ||
} | ||
@@ -63,0 +63,0 @@ |
import { IContext, IRollupContext, VerbosityLevel } from "./context"; | ||
import {isFunction} from "lodash"; | ||
import * as _ from "lodash"; | ||
@@ -10,3 +10,3 @@ export class RollupContext implements IContext | ||
{ | ||
this.hasContext = isFunction(this.context.warn) && isFunction(this.context.error); | ||
this.hasContext = _.isFunction(this.context.warn) && _.isFunction(this.context.error); | ||
} | ||
@@ -13,0 +13,0 @@ |
import { IContext } from "./context"; | ||
import {Graph, alg} from "graphlib"; | ||
import {sha1} from "object-hash"; | ||
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 {Diagnostic, DiagnosticCategory, IScriptSnapshot, OutputFile, LanguageServiceHost, version, getAutomaticTypeDirectiveNames, sys, resolveTypeReferenceDirective, flattenDiagnosticMessageText, CompilerOptions} from "typescript"; | ||
import {blue, yellow, green} from "colors/safe"; | ||
import {emptyDirSync} from "fs-extra"; | ||
import * as _ from "lodash"; | ||
import { tsModule } from "./tsproxy"; | ||
import * as tsTypes from "typescript"; | ||
import { blue, yellow, green } from "colors/safe"; | ||
import { emptyDirSync } from "fs-extra"; | ||
@@ -15,3 +16,3 @@ export interface ICode | ||
map: string | undefined; | ||
dts?: OutputFile | undefined; | ||
dts?: tsTypes.OutputFile | undefined; | ||
} | ||
@@ -28,3 +29,3 @@ | ||
fileLine?: string; | ||
category: DiagnosticCategory; | ||
category: tsTypes.DiagnosticCategory; | ||
code: number; | ||
@@ -37,16 +38,16 @@ type: string; | ||
id: string; | ||
snapshot: IScriptSnapshot | undefined; | ||
snapshot: tsTypes.IScriptSnapshot | undefined; | ||
} | ||
export function convertDiagnostic(type: string, data: Diagnostic[]): IDiagnostics[] | ||
export function convertDiagnostic(type: string, data: tsTypes.Diagnostic[]): IDiagnostics[] | ||
{ | ||
return map(data, (diagnostic) => | ||
return _.map(data, (diagnostic) => | ||
{ | ||
const entry: IDiagnostics = | ||
{ | ||
flatMessage: flattenDiagnosticMessageText(diagnostic.messageText, "\n"), | ||
category: diagnostic.category, | ||
code: diagnostic.code, | ||
type, | ||
}; | ||
{ | ||
flatMessage: tsModule.flattenDiagnosticMessageText(diagnostic.messageText, "\n"), | ||
category: diagnostic.category, | ||
code: diagnostic.code, | ||
type, | ||
}; | ||
@@ -75,3 +76,3 @@ if (diagnostic.file && diagnostic.start !== undefined) | ||
constructor(private host: LanguageServiceHost, cache: string, private options: CompilerOptions, private rollupConfig: any, rootFilenames: string[], private context: IContext) | ||
constructor(private host: tsTypes.LanguageServiceHost, cache: string, private options: tsTypes.CompilerOptions, private rollupConfig: any, rootFilenames: string[], private context: IContext) | ||
{ | ||
@@ -83,13 +84,13 @@ this.cacheDir = `${cache}/${sha1({ | ||
rollupConfig: this.rollupConfig, | ||
tsVersion : version, | ||
tsVersion: tsModule.version, | ||
})}`; | ||
this.dependencyTree = new Graph({ directed: true }); | ||
this.dependencyTree.setDefaultNodeLabel((_node: string) => ({ dirty: false }) ); | ||
this.dependencyTree.setDefaultNodeLabel((_node: string) => ({ dirty: false })); | ||
const automaticTypes = map(getAutomaticTypeDirectiveNames(options, sys), (entry) => resolveTypeReferenceDirective(entry, undefined, options, 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) | ||
@@ -125,3 +126,3 @@ .map((id) => ({ id, snapshot: this.host.getScriptSnapshot(id) })); | ||
{ | ||
each(alg.topsort(this.dependencyTree), (id: string) => cb(id)); | ||
_.each(alg.topsort(this.dependencyTree), (id: string) => cb(id)); | ||
return; | ||
@@ -132,3 +133,3 @@ } | ||
each(this.dependencyTree.nodes(), (id: string) => cb(id)); | ||
_.each(this.dependencyTree.nodes(), (id: string) => cb(id)); | ||
} | ||
@@ -145,3 +146,3 @@ | ||
public getCompiled(id: string, snapshot: IScriptSnapshot, transform: () => ICode | undefined): ICode | undefined | ||
public getCompiled(id: string, snapshot: tsTypes.IScriptSnapshot, transform: () => ICode | undefined): ICode | undefined | ||
{ | ||
@@ -170,3 +171,3 @@ const name = this.makeName(id, snapshot); | ||
public getSyntacticDiagnostics(id: string, snapshot: IScriptSnapshot, check: () => Diagnostic[]): IDiagnostics[] | ||
public getSyntacticDiagnostics(id: string, snapshot: tsTypes.IScriptSnapshot, check: () => tsTypes.Diagnostic[]): IDiagnostics[] | ||
{ | ||
@@ -176,3 +177,3 @@ return this.getDiagnostics("syntax", this.syntacticDiagnosticsCache, id, snapshot, check); | ||
public getSemanticDiagnostics(id: string, snapshot: IScriptSnapshot, check: () => Diagnostic[]): IDiagnostics[] | ||
public getSemanticDiagnostics(id: string, snapshot: tsTypes.IScriptSnapshot, check: () => tsTypes.Diagnostic[]): IDiagnostics[] | ||
{ | ||
@@ -185,3 +186,3 @@ return this.getDiagnostics("semantic", this.semanticDiagnosticsCache, id, snapshot, check); | ||
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) => | ||
@@ -198,6 +199,6 @@ { | ||
each(typeNames, (name) => this.typesCache.touch(name)); | ||
_.each(typeNames, (name) => this.typesCache.touch(name)); | ||
} | ||
private getDiagnostics(type: string, cache: ICache<IDiagnostics[]>, id: string, snapshot: IScriptSnapshot, check: () => Diagnostic[]): IDiagnostics[] | ||
private getDiagnostics(type: string, cache: ICache<IDiagnostics[]>, id: string, snapshot: tsTypes.IScriptSnapshot, check: () => tsTypes.Diagnostic[]): IDiagnostics[] | ||
{ | ||
@@ -254,3 +255,3 @@ const name = this.makeName(id, snapshot); | ||
return some(dependencies, (dependency, node) => | ||
return _.some(dependencies, (dependency, node) => | ||
{ | ||
@@ -270,3 +271,3 @@ if (!node || dependency.distance === Infinity) | ||
private makeName(id: string, snapshot: IScriptSnapshot) | ||
private makeName(id: string, snapshot: tsTypes.IScriptSnapshot) | ||
{ | ||
@@ -273,0 +274,0 @@ const data = snapshot.getText(0, snapshot.getLength()); |
@@ -1,2 +0,2 @@ | ||
import {readFileSync} from "fs"; | ||
import { readFileSync } from "fs"; | ||
@@ -3,0 +3,0 @@ // The injected id for helpers. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
104247
33
2320
134