diff --git a/.gitignore b/.gitignore index 0b325ff..0e82f6e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /node_modules /npm-debug.log /typings -/.rpt2_cache \ No newline at end of file +/.rpt2_cache +/.vscode \ No newline at end of file diff --git a/README.md b/README.md index 8d9b399..16a0582 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ![npm-monthly-downloads](https://img.shields.io/npm/dm/rollup-plugin-typescript2.svg?maxAge=2592000) [![Codeship Status](https://app.codeship.com/projects/fe9cf8f0-e8d4-0134-ec88-4e3d33dcd7ed/status?branch=master)](https://app.codeship.com/projects/207445) -Rollup plugin for typescript with compiler errors. +Rollup plugin for typescript with compiler errors. This is a rewrite of original rollup-plugin-typescript, starting and borrowing from [this fork](https://github.com/alexlur/rollup-plugin-typescript). @@ -26,9 +26,21 @@ export default { } ``` -The plugin depends on existence of `tsconfig.json` file. All compiler options and file lists are loaded from that. +The plugin inherits all compiler options and file lists from your `tsconfig.json` file. +If your tsconfig has another name or another relative path from the root directory, you can pass in a custom path: -Following compiler options are forced though: +```js +// ... +plugins: [ + typescript({ + tsconfig: "other_dir/tsconfig.json" + }) +] +``` + +This also allows for passing in different tsconfig files depending on your build target. + +The following compiler options are forced though: * `module`: es2015 * `noEmitHelpers`: true * `importHelpers`: true @@ -38,6 +50,10 @@ You will need to set `"moduleResolution": "node"` in `tsconfig.json` if typescri Plugin takes following options: +* `tsconfig`: "tsconfig.json" + + Override this if your tsconfig has another name or relative location from the project directory. + * `check`: true Set to false to avoid doing any diagnostic checks on the code. diff --git a/dist/context.d.ts b/dist/context.d.ts index be5b08c..71e0d5c 100644 --- a/dist/context.d.ts +++ b/dist/context.d.ts @@ -1,25 +1,25 @@ -export interface IRollupContext { - warn(message: string): void; - error(message: string): void; -} -export interface IContext { - warn(message: string): void; - error(message: string): void; - info(message: string): void; - debug(message: string): void; -} -export declare enum VerbosityLevel { - Error = 0, - Warning = 1, - Info = 2, - Debug = 3, -} -export declare class ConsoleContext implements IContext { - private verbosity; - private prefix; - constructor(verbosity: VerbosityLevel, prefix?: string); - warn(message: string): void; - error(message: string): void; - info(message: string): void; - debug(message: string): void; -} +export interface IRollupContext { + warn(message: string): void; + error(message: string): void; +} +export interface IContext { + warn(message: string): void; + error(message: string): void; + info(message: string): void; + debug(message: string): void; +} +export declare enum VerbosityLevel { + Error = 0, + Warning = 1, + Info = 2, + Debug = 3, +} +export declare class ConsoleContext implements IContext { + private verbosity; + private prefix; + constructor(verbosity: VerbosityLevel, prefix?: string); + warn(message: string): void; + error(message: string): void; + info(message: string): void; + debug(message: string): void; +} diff --git a/dist/host.d.ts b/dist/host.d.ts index cb47529..c3d5677 100644 --- a/dist/host.d.ts +++ b/dist/host.d.ts @@ -1,17 +1,17 @@ -import * as ts from "typescript"; -export declare class LanguageServiceHost implements ts.LanguageServiceHost { - private parsedConfig; - private cwd; - private snapshots; - private versions; - constructor(parsedConfig: ts.ParsedCommandLine); - reset(): void; - setSnapshot(fileName: string, data: string): ts.IScriptSnapshot; - getScriptSnapshot(fileName: string): ts.IScriptSnapshot | undefined; - getCurrentDirectory(): string; - getScriptVersion(fileName: string): string; - getScriptFileNames(): string[]; - getCompilationSettings(): ts.CompilerOptions; - getDefaultLibFileName(opts: ts.CompilerOptions): string; - private normalize(fileName); -} +import * as ts from "typescript"; +export declare class LanguageServiceHost implements ts.LanguageServiceHost { + private parsedConfig; + private cwd; + private snapshots; + private versions; + constructor(parsedConfig: ts.ParsedCommandLine); + reset(): void; + setSnapshot(fileName: string, data: string): ts.IScriptSnapshot; + getScriptSnapshot(fileName: string): ts.IScriptSnapshot | undefined; + getCurrentDirectory(): string; + getScriptVersion(fileName: string): string; + getScriptFileNames(): string[]; + getCompilationSettings(): ts.CompilerOptions; + getDefaultLibFileName(opts: ts.CompilerOptions): string; + private normalize(fileName); +} diff --git a/dist/index.d.ts b/dist/index.d.ts index 7852e21..c87a139 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,20 +1,21 @@ -import { IRollupContext } from "./context"; -import { ICode } from "./tscache"; -export interface IOptions { - include: string; - exclude: string; - check: boolean; - verbosity: number; - clean: boolean; - cacheRoot: string; - abortOnError: boolean; - rollupCommonJSResolveHack: boolean; -} -export default function typescript(options: IOptions): { - options(config: any): void; - resolveId(importee: string, importer: string): string | null; - load(id: string): string | undefined; - transform(this: IRollupContext, code: string, id: string): ICode | undefined; - ongenerate(bundleOptions: any): void; - onwrite(): void; -}; +import { IRollupContext } from "./context"; +import { ICode } from "./tscache"; +export interface IOptions { + include: string; + exclude: string; + check: boolean; + verbosity: number; + clean: boolean; + cacheRoot: string; + abortOnError: boolean; + rollupCommonJSResolveHack: boolean; + tsconfig: string; +} +export default function typescript(options: IOptions): { + options(config: any): void; + resolveId(importee: string, importer: string): string | null; + load(id: string): string | undefined; + transform(this: IRollupContext, code: string, id: string): ICode | undefined; + ongenerate(bundleOptions: any): void; + onwrite(): void; +}; diff --git a/dist/rollingcache.d.ts b/dist/rollingcache.d.ts index 7340fef..4c923aa 100644 --- a/dist/rollingcache.d.ts +++ b/dist/rollingcache.d.ts @@ -1,36 +1,36 @@ -import { ICache } from "./icache"; -/** - * Saves data in new cache folder or reads it from old one. - * Avoids perpetually growing cache and situations when things need to consider changed and then reverted data to be changed. - */ -export declare class RollingCache implements ICache { - private cacheRoot; - private checkNewCache; - private oldCacheRoot; - private newCacheRoot; - private rolled; - /** - * @param cacheRoot: root folder for the cache - * @param checkNewCache: whether to also look in new cache when reading from cache - */ - constructor(cacheRoot: string, checkNewCache: boolean); - /** - * @returns true if name exist in old cache (or either old of new cache if checkNewCache is true) - */ - exists(name: string): boolean; - path(name: string): string; - /** - * @returns true if old cache contains all names and nothing more - */ - match(names: string[]): boolean; - /** - * @returns data for name, must exist in old cache (or either old of new cache if checkNewCache is true) - */ - read(name: string): DataType; - write(name: string, data: DataType): void; - touch(name: string): void; - /** - * clears old cache and moves new in its place - */ - roll(): void; -} +import { ICache } from "./icache"; +/** + * Saves data in new cache folder or reads it from old one. + * Avoids perpetually growing cache and situations when things need to consider changed and then reverted data to be changed. + */ +export declare class RollingCache implements ICache { + private cacheRoot; + private checkNewCache; + private oldCacheRoot; + private newCacheRoot; + private rolled; + /** + * @param cacheRoot: root folder for the cache + * @param checkNewCache: whether to also look in new cache when reading from cache + */ + constructor(cacheRoot: string, checkNewCache: boolean); + /** + * @returns true if name exist in old cache (or either old of new cache if checkNewCache is true) + */ + exists(name: string): boolean; + path(name: string): string; + /** + * @returns true if old cache contains all names and nothing more + */ + match(names: string[]): boolean; + /** + * @returns data for name, must exist in old cache (or either old of new cache if checkNewCache is true) + */ + read(name: string): DataType; + write(name: string, data: DataType): void; + touch(name: string): void; + /** + * clears old cache and moves new in its place + */ + roll(): void; +} diff --git a/dist/rollup-plugin-typescript2.cjs.js b/dist/rollup-plugin-typescript2.cjs.js index 37cdd03..43a2921 100644 --- a/dist/rollup-plugin-typescript2.cjs.js +++ b/dist/rollup-plugin-typescript2.cjs.js @@ -418,10 +418,10 @@ catch (e) { console.warn("Error loading `tslib` helper library."); throw e; } -function parseTsConfig(context) { - var fileName = ts.findConfigFile(process.cwd(), ts.sys.fileExists, "tsconfig.json"); +function parseTsConfig(tsconfig, context) { + var fileName = ts.findConfigFile(process.cwd(), ts.sys.fileExists, tsconfig); if (!fileName) - throw new Error("couldn't find 'tsconfig.json' in " + process.cwd()); + throw new Error("couldn't find '" + tsconfig + "' in " + process.cwd()); var text = ts.sys.readFile(fileName); var result = ts.parseConfigFileTextToJson(fileName, text); if (result.error) { @@ -473,6 +473,7 @@ function typescript(options) { exclude: ["*.d.ts", "**/*.d.ts"], abortOnError: true, rollupCommonJSResolveHack: false, + tsconfig: "tsconfig.json" }); var rollupConfig; var watchMode = false; @@ -482,7 +483,7 @@ function typescript(options) { context.info("Typescript version: " + ts.version); context.debug("Options: " + JSON.stringify(options, undefined, 4)); var filter$$1 = createFilter(options.include, options.exclude); - var parsedConfig = parseTsConfig(context); + var parsedConfig = parseTsConfig(options.tsconfig, context); var servicesHost = new LanguageServiceHost(parsedConfig); var service = ts.createLanguageService(servicesHost, ts.createDocumentRegistry()); var _cache; diff --git a/dist/rollup-plugin-typescript2.es.js b/dist/rollup-plugin-typescript2.es.js index 5be01de..cb82991 100644 --- a/dist/rollup-plugin-typescript2.es.js +++ b/dist/rollup-plugin-typescript2.es.js @@ -424,10 +424,10 @@ catch (e) { console.warn("Error loading `tslib` helper library."); throw e; } -function parseTsConfig(context) { - var fileName = findConfigFile(process.cwd(), sys.fileExists, "tsconfig.json"); +function parseTsConfig(tsconfig, context) { + var fileName = findConfigFile(process.cwd(), sys.fileExists, tsconfig); if (!fileName) - throw new Error("couldn't find 'tsconfig.json' in " + process.cwd()); + throw new Error("couldn't find '" + tsconfig + "' in " + process.cwd()); var text = sys.readFile(fileName); var result = parseConfigFileTextToJson(fileName, text); if (result.error) { @@ -479,6 +479,7 @@ function typescript(options) { exclude: ["*.d.ts", "**/*.d.ts"], abortOnError: true, rollupCommonJSResolveHack: false, + tsconfig: "tsconfig.json" }); var rollupConfig; var watchMode = false; @@ -488,7 +489,7 @@ function typescript(options) { context.info("Typescript version: " + version); context.debug("Options: " + JSON.stringify(options, undefined, 4)); var filter$$1 = createFilter(options.include, options.exclude); - var parsedConfig = parseTsConfig(context); + var parsedConfig = parseTsConfig(options.tsconfig, context); var servicesHost = new LanguageServiceHost(parsedConfig); var service = createLanguageService(servicesHost, createDocumentRegistry()); var _cache; diff --git a/dist/rollupcontext.d.ts b/dist/rollupcontext.d.ts index d80592e..a5cda72 100644 --- a/dist/rollupcontext.d.ts +++ b/dist/rollupcontext.d.ts @@ -1,13 +1,13 @@ -import { IContext, IRollupContext, VerbosityLevel } from "./context"; -export declare class RollupContext implements IContext { - private verbosity; - private bail; - private context; - private prefix; - private hasContext; - constructor(verbosity: VerbosityLevel, bail: boolean, context: IRollupContext, prefix?: string); - warn(message: string): void; - error(message: string): void; - info(message: string): void; - debug(message: string): void; -} +import { IContext, IRollupContext, VerbosityLevel } from "./context"; +export declare class RollupContext implements IContext { + private verbosity; + private bail; + private context; + private prefix; + private hasContext; + constructor(verbosity: VerbosityLevel, bail: boolean, context: IRollupContext, prefix?: string); + warn(message: string): void; + error(message: string): void; + info(message: string): void; + debug(message: string): void; +} diff --git a/dist/tscache.d.ts b/dist/tscache.d.ts index 064c003..0bf3613 100644 --- a/dist/tscache.d.ts +++ b/dist/tscache.d.ts @@ -1,44 +1,44 @@ -import { IContext } from "./context"; -import * as ts from "typescript"; -export interface ICode { - code: string | undefined; - map: string | undefined; - dts?: ts.OutputFile | undefined; -} -export interface IDiagnostics { - flatMessage: string; - fileLine?: string; - category: ts.DiagnosticCategory; - code: number; - type: string; -} -export declare function convertDiagnostic(type: string, data: ts.Diagnostic[]): IDiagnostics[]; -export declare class TsCache { - private host; - private options; - private rollupConfig; - private context; - private cacheVersion; - private dependencyTree; - private ambientTypes; - private ambientTypesDirty; - private cacheDir; - private codeCache; - private typesCache; - private semanticDiagnosticsCache; - private syntacticDiagnosticsCache; - constructor(host: ts.LanguageServiceHost, cache: string, options: ts.CompilerOptions, rollupConfig: any, rootFilenames: string[], context: IContext); - clean(): void; - setDependency(importee: string, importer: string): void; - walkTree(cb: (id: string) => void | false): void; - done(): void; - getCompiled(id: string, snapshot: ts.IScriptSnapshot, transform: () => ICode | undefined): ICode | undefined; - getSyntacticDiagnostics(id: string, snapshot: ts.IScriptSnapshot, check: () => ts.Diagnostic[]): IDiagnostics[]; - getSemanticDiagnostics(id: string, snapshot: ts.IScriptSnapshot, check: () => ts.Diagnostic[]): IDiagnostics[]; - private checkAmbientTypes(); - private getDiagnostics(type, cache, id, snapshot, check); - private init(); - private markAsDirty(id, _snapshot); - private isDirty(id, _snapshot, checkImports); - private makeName(id, snapshot); -} +import { IContext } from "./context"; +import * as ts from "typescript"; +export interface ICode { + code: string | undefined; + map: string | undefined; + dts?: ts.OutputFile | undefined; +} +export interface IDiagnostics { + flatMessage: string; + fileLine?: string; + category: ts.DiagnosticCategory; + code: number; + type: string; +} +export declare function convertDiagnostic(type: string, data: ts.Diagnostic[]): IDiagnostics[]; +export declare class TsCache { + private host; + private options; + private rollupConfig; + private context; + private cacheVersion; + private dependencyTree; + private ambientTypes; + private ambientTypesDirty; + private cacheDir; + private codeCache; + private typesCache; + private semanticDiagnosticsCache; + private syntacticDiagnosticsCache; + constructor(host: ts.LanguageServiceHost, cache: string, options: ts.CompilerOptions, rollupConfig: any, rootFilenames: string[], context: IContext); + clean(): void; + setDependency(importee: string, importer: string): void; + walkTree(cb: (id: string) => void | false): void; + done(): void; + getCompiled(id: string, snapshot: ts.IScriptSnapshot, transform: () => ICode | undefined): ICode | undefined; + getSyntacticDiagnostics(id: string, snapshot: ts.IScriptSnapshot, check: () => ts.Diagnostic[]): IDiagnostics[]; + getSemanticDiagnostics(id: string, snapshot: ts.IScriptSnapshot, check: () => ts.Diagnostic[]): IDiagnostics[]; + private checkAmbientTypes(); + private getDiagnostics(type, cache, id, snapshot, check); + private init(); + private markAsDirty(id, _snapshot); + private isDirty(id, _snapshot, checkImports); + private makeName(id, snapshot); +} diff --git a/src/index.ts b/src/index.ts index 3711e1b..d456678 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,12 +36,12 @@ try throw e; } -function parseTsConfig(context: IContext) +function parseTsConfig(tsconfig: string, context: IContext) { - const fileName = ts.findConfigFile(process.cwd(), ts.sys.fileExists, "tsconfig.json"); + const fileName = ts.findConfigFile(process.cwd(), ts.sys.fileExists, tsconfig); if (!fileName) - throw new Error(`couldn't find 'tsconfig.json' in ${process.cwd()}`); + throw new Error(`couldn't find '${tsconfig}' in ${process.cwd()}`); const text = ts.sys.readFile(fileName); const result = ts.parseConfigFileTextToJson(fileName, text); @@ -104,6 +104,7 @@ export interface IOptions cacheRoot: string; abortOnError: boolean; rollupCommonJSResolveHack: boolean; + tsconfig: string; } export default function typescript(options: IOptions) @@ -120,6 +121,7 @@ export default function typescript(options: IOptions) exclude: [ "*.d.ts", "**/*.d.ts" ], abortOnError: true, rollupCommonJSResolveHack: false, + tsconfig: "tsconfig.json" }); let rollupConfig: any; @@ -135,7 +137,7 @@ export default function typescript(options: IOptions) const filter = createFilter(options.include, options.exclude); - const parsedConfig = parseTsConfig(context); + const parsedConfig = parseTsConfig(options.tsconfig, context); const servicesHost = new LanguageServiceHost(parsedConfig);