From 80a42b63fa64c93d49bff50d497d7b6d38cc7e7d Mon Sep 17 00:00:00 2001 From: ezolenko Date: Mon, 29 May 2017 22:41:44 -0600 Subject: [PATCH] - fix for type definitions eating memory in watch mode - publishing type definitions --- README.md | 3 +- dist/context.d.ts | 25 +++++++++++++++ dist/host.d.ts | 17 +++++++++++ dist/index.d.ts | 20 ++++++++++++ dist/rollingcache.d.ts | 36 ++++++++++++++++++++++ dist/rollup-plugin-typescript2.cjs.js | 7 +++-- dist/rollup-plugin-typescript2.es.js | 7 +++-- dist/rollupcontext.d.ts | 13 ++++++++ dist/tscache.d.ts | 44 +++++++++++++++++++++++++++ src/index.ts | 20 +++++++----- tsconfig.json | 4 ++- 11 files changed, 179 insertions(+), 17 deletions(-) create mode 100644 dist/context.d.ts create mode 100644 dist/host.d.ts create mode 100644 dist/index.d.ts create mode 100644 dist/rollingcache.d.ts create mode 100644 dist/rollupcontext.d.ts create mode 100644 dist/tscache.d.ts diff --git a/README.md b/README.md index 9159c57..8d9b399 100644 --- a/README.md +++ b/README.md @@ -75,8 +75,7 @@ Plugin takes following options: ### 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). +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). ### Watch mode diff --git a/dist/context.d.ts b/dist/context.d.ts new file mode 100644 index 0000000..be5b08c --- /dev/null +++ b/dist/context.d.ts @@ -0,0 +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; +} diff --git a/dist/host.d.ts b/dist/host.d.ts new file mode 100644 index 0000000..cb47529 --- /dev/null +++ b/dist/host.d.ts @@ -0,0 +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); +} diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..7852e21 --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,20 @@ +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; +}; diff --git a/dist/rollingcache.d.ts b/dist/rollingcache.d.ts new file mode 100644 index 0000000..7340fef --- /dev/null +++ b/dist/rollingcache.d.ts @@ -0,0 +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; +} diff --git a/dist/rollup-plugin-typescript2.cjs.js b/dist/rollup-plugin-typescript2.cjs.js index 3eb6b6e..37cdd03 100644 --- a/dist/rollup-plugin-typescript2.cjs.js +++ b/dist/rollup-plugin-typescript2.cjs.js @@ -492,7 +492,7 @@ function typescript(options) { return _cache; }; var noErrors = true; - var declarations = []; + var declarations = {}; // printing compiler option errors if (options.check) printDiagnostics(context, convertDiagnostic("options", service.getCompilerOptionsDiagnostics())); @@ -573,7 +573,8 @@ function typescript(options) { printDiagnostics(contextWrapper, diagnostics); } if (result && result.dts) { - declarations.push(result.dts); + declarations[result.dts.name] = result.dts; + result.dts = undefined; } return result; }, @@ -597,7 +598,7 @@ function typescript(options) { round++; }, onwrite: function () { - declarations.forEach(function (_a) { + _.each(declarations, function (_a) { var name = _a.name, text = _a.text, writeByteOrderMark = _a.writeByteOrderMark; ts.sys.writeFile(name, text, writeByteOrderMark); }); diff --git a/dist/rollup-plugin-typescript2.es.js b/dist/rollup-plugin-typescript2.es.js index 77107c5..5be01de 100644 --- a/dist/rollup-plugin-typescript2.es.js +++ b/dist/rollup-plugin-typescript2.es.js @@ -498,7 +498,7 @@ function typescript(options) { return _cache; }; var noErrors = true; - var declarations = []; + var declarations = {}; // printing compiler option errors if (options.check) printDiagnostics(context, convertDiagnostic("options", service.getCompilerOptionsDiagnostics())); @@ -579,7 +579,8 @@ function typescript(options) { printDiagnostics(contextWrapper, diagnostics); } if (result && result.dts) { - declarations.push(result.dts); + declarations[result.dts.name] = result.dts; + result.dts = undefined; } return result; }, @@ -603,7 +604,7 @@ function typescript(options) { round++; }, onwrite: function () { - declarations.forEach(function (_a) { + each(declarations, function (_a) { var name = _a.name, text = _a.text, writeByteOrderMark = _a.writeByteOrderMark; sys.writeFile(name, text, writeByteOrderMark); }); diff --git a/dist/rollupcontext.d.ts b/dist/rollupcontext.d.ts new file mode 100644 index 0000000..d80592e --- /dev/null +++ b/dist/rollupcontext.d.ts @@ -0,0 +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; +} diff --git a/dist/tscache.d.ts b/dist/tscache.d.ts new file mode 100644 index 0000000..064c003 --- /dev/null +++ b/dist/tscache.d.ts @@ -0,0 +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); +} diff --git a/src/index.ts b/src/index.ts index 08f3e76..3711e1b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -94,7 +94,7 @@ function printDiagnostics(context: IContext, diagnostics: IDiagnostics[]) }); } -interface IOptions +export interface IOptions { include: string; exclude: string; @@ -152,7 +152,7 @@ export default function typescript(options: IOptions) let noErrors = true; - const declarations: ts.OutputFile[] = []; + const declarations: { [name: string]: ts.OutputFile } = {}; // printing compiler option errors if (options.check) @@ -249,8 +249,8 @@ export default function typescript(options: IOptions) this.error(colors.red(`failed to transpile '${id}'`)); } - const transpiled = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".js") ); - const map = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".map") ); + const transpiled = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".js")); + const map = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".map")); const dts = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".d.ts")); return { @@ -279,8 +279,10 @@ export default function typescript(options: IOptions) printDiagnostics(contextWrapper, diagnostics); } - if (result && result.dts) { - declarations.push(result.dts); + if (result && result.dts) + { + declarations[result.dts.name] = result.dts; + result.dts = undefined; } return result; @@ -320,8 +322,10 @@ export default function typescript(options: IOptions) round++; }, - onwrite() { - declarations.forEach(({ name, text, writeByteOrderMark }) => { + onwrite() + { + _.each(declarations, ({ name, text, writeByteOrderMark }) => + { ts.sys.writeFile(name, text, writeByteOrderMark); }); }, diff --git a/tsconfig.json b/tsconfig.json index c346a36..a91d638 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,7 +14,9 @@ "noEmitOnError": false, "strictNullChecks": true, "forceConsistentCasingInFileNames": true, - "noImplicitReturns": true + "noImplicitReturns": true, + "declaration": true, + "declarationDir": "./dist" }, "include": [ "src/**/*.ts"