Skip to content

Commit

Permalink
- option for overriding typescript version for #27
Browse files Browse the repository at this point in the history
  • Loading branch information
ezolenko committed Aug 11, 2017
1 parent 9e30977 commit 8122820
Show file tree
Hide file tree
Showing 19 changed files with 215 additions and 174 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ Plugin takes following options:

If true, declaration files will be emitted in the directory given in the tsconfig. If false, the declaration files will be placed inside the destination directory given in the Rollup configuration.

* `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

This plugin respects `declaration: true` in your `tsconfig.json` file. When set, it will emit `*.d.ts` files for your bundle. The resulting file(s) can then be used with the `types` property in your `package.json` file as described [here](https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html).
Expand Down
4 changes: 2 additions & 2 deletions dist/get-options-overrides.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,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;
14 changes: 7 additions & 7 deletions dist/host.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
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;
private cwd;
private snapshots;
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;
readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[];
readFile(path: string, encoding?: string): string;
Expand Down
4 changes: 2 additions & 2 deletions dist/parse-ts-config.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,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;
62 changes: 34 additions & 28 deletions dist/rollup-plugin-typescript2.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
'use strict';

var lodash = require('lodash');
var typescript = require('typescript');
var fs = require('fs');
var graphlib = require('graphlib');
var objectHash = require('object-hash');
Expand Down Expand Up @@ -116,6 +115,11 @@ var RollupContext = (function () {
return RollupContext;
}());

var tsModule;
function setTypescriptModule(override) {
tsModule = override;
}

var LanguageServiceHost = (function () {
function LanguageServiceHost(parsedConfig) {
this.parsedConfig = parsedConfig;
Expand All @@ -129,7 +133,7 @@ var LanguageServiceHost = (function () {
};
LanguageServiceHost.prototype.setSnapshot = function (fileName, data) {
fileName = this.normalize(fileName);
var snapshot = typescript.ScriptSnapshot.fromString(data);
var snapshot = tsModule.ScriptSnapshot.fromString(data);
this.snapshots[fileName] = snapshot;
this.versions[fileName] = (this.versions[fileName] || 0) + 1;
return snapshot;
Expand All @@ -139,7 +143,7 @@ var LanguageServiceHost = (function () {
if (lodash.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;
return this.snapshots[fileName];
}
Expand All @@ -159,28 +163,28 @@ var LanguageServiceHost = (function () {
return this.parsedConfig.options;
};
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);
};
LanguageServiceHost.prototype.getTypeRootsVersion = function () {
return 0;
};
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("/");
Expand Down Expand Up @@ -264,7 +268,7 @@ var RollingCache = (function () {
function convertDiagnostic(type, data) {
return lodash.map(data, function (diagnostic) {
var entry = {
flatMessage: typescript.flattenDiagnosticMessageText(diagnostic.messageText, "\n"),
flatMessage: tsModule.flattenDiagnosticMessageText(diagnostic.messageText, "\n"),
category: diagnostic.category,
code: diagnostic.code,
type: type,
Expand All @@ -290,11 +294,11 @@ var TsCache = (function () {
rootFilenames: rootFilenames,
options: this.options,
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 = lodash.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"); })
Expand Down Expand Up @@ -424,17 +428,17 @@ function printDiagnostics(context, diagnostics) {
var color;
var category;
switch (diagnostic.category) {
case typescript.DiagnosticCategory.Message:
case tsModule.DiagnosticCategory.Message:
print = context.info;
color = colors_safe.white;
category = "";
break;
case typescript.DiagnosticCategory.Error:
case tsModule.DiagnosticCategory.Error:
print = context.error;
color = colors_safe.red;
category = "error";
break;
case typescript.DiagnosticCategory.Warning:
case tsModule.DiagnosticCategory.Warning:
default:
print = context.warn;
color = colors_safe.yellow;
Expand All @@ -452,20 +456,20 @@ function printDiagnostics(context, diagnostics) {
function getOptionsOverrides(_a, tsConfigJson) {
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() }));
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) {
printDiagnostics(context, convertDiagnostic("config", [result.error]));
throw new Error("failed to parse " + fileName);
}
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);
}

// The injected id for helpers.
Expand All @@ -481,7 +485,7 @@ catch (e) {
throw e;
}

function typescript$1(options) {
function typescript(options) {
// tslint:disable-next-line:no-var-requires
var createFilter = require("rollup-pluginutils").createFilter;
// tslint:enable-next-line:no-var-requires
Expand Down Expand Up @@ -514,17 +518,19 @@ function typescript$1(options) {
rollupCommonJSResolveHack: false,
tsconfig: "tsconfig.json",
useTsconfigDeclarationDir: false,
typescript: require("typescript"),
});
setTypescriptModule(pluginOptions.typescript);
return {
options: function (config) {
rollupOptions = 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
if (pluginOptions.check)
printDiagnostics(context, convertDiagnostic("options", service.getCompilerOptionsDiagnostics()));
Expand All @@ -539,7 +545,7 @@ function typescript$1(options) {
return null;
importer = importer.split("\\").join("/");
// 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);
Expand Down Expand Up @@ -643,10 +649,10 @@ function typescript$1(options) {
writeToPath = path.join(destDirectory, path.relative(baseDeclarationDir, name));
}
// Write the declaration file to disk.
typescript.sys.writeFile(writeToPath, text, writeByteOrderMark);
tsModule.sys.writeFile(writeToPath, text, writeByteOrderMark);
});
},
};
}

module.exports = typescript$1;
module.exports = typescript;
Loading

0 comments on commit 8122820

Please sign in to comment.