diff --git a/.gitignore b/.gitignore index 0e82f6e..bd42e16 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /npm-debug.log /typings /.rpt2_cache -/.vscode \ No newline at end of file +/.vscode +/.idea \ No newline at end of file diff --git a/dist/index.d.ts b/dist/index.d.ts index 17449c3..84a80b1 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,5 +1,6 @@ import { IRollupContext } from "./context"; import { ICode } from "./tscache"; +import { IRollupOptions } from "./irollup-options"; export interface IOptions { include?: string; exclude?: string; @@ -17,5 +18,5 @@ export default function typescript(options?: IOptions): { load(id: string): string | undefined; transform(this: IRollupContext, code: string, id: string): ICode | undefined; ongenerate(bundleOptions: any): void; - onwrite(): void; + onwrite({dest}: IRollupOptions): void; }; diff --git a/dist/rollup-plugin-typescript2.cjs.js b/dist/rollup-plugin-typescript2.cjs.js index 2c27db6..a5981c8 100644 --- a/dist/rollup-plugin-typescript2.cjs.js +++ b/dist/rollup-plugin-typescript2.cjs.js @@ -337,10 +337,10 @@ var TsCache = (function () { this.context.debug(" cache: '" + this.codeCache.path(name) + "'"); if (!this.codeCache.exists(name) || this.isDirty(id, snapshot, false)) { this.context.debug(colors.yellow(" cache miss")); - var data_1 = transform(); - this.codeCache.write(name, data_1); + var transformedData = transform(); + this.codeCache.write(name, transformedData); this.markAsDirty(id, snapshot); - return data_1; + return transformedData; } this.context.debug(colors.green(" cache hit")); var data = this.codeCache.read(name); @@ -372,10 +372,10 @@ var TsCache = (function () { this.context.debug(" cache: '" + cache.path(name) + "'"); if (!cache.exists(name) || this.isDirty(id, snapshot, true)) { this.context.debug(colors.yellow(" cache miss")); - var data_2 = convertDiagnostic(type, check()); - cache.write(name, data_2); + var data_1 = convertDiagnostic(type, check()); + cache.write(name, data_1); this.markAsDirty(id, snapshot); - return data_2; + return data_1; } this.context.debug(colors.green(" cache hit")); var data = cache.read(name); @@ -621,10 +621,22 @@ function typescript(options) { cache().done(); round++; }, - onwrite: function () { + onwrite: function (_a) { + var dest = _a.dest; + // Expect the destination path given in the rollup bundle to be a relative path (if given). Join it with process.cwd() + var bundleDirectory = dest == null ? null : path.join(process.cwd(), path.dirname(dest)); + var bundleName = dest == null ? null : path.basename(dest); + var bundleExt = dest == null ? null : path.extname(dest); _.each(declarations, function (_a) { var name = _a.name, text = _a.text, writeByteOrderMark = _a.writeByteOrderMark; - ts.sys.writeFile(name, text, writeByteOrderMark); + // If no 'dest' is given, the bundle has no directory, name or extension. In that case, use the default declaration path given by Typescript. + if (bundleName == null || bundleExt == null || bundleDirectory == null) + return ts.sys.writeFile(name, text, writeByteOrderMark); + // Otherwise, try to play nice with the destination from the rollup config. + // Make sure that the declaration file has the same name as the bundle (but a different extension) + var declarationName = bundleExt === "" ? bundleName + ".d.ts" : bundleName.slice(0, bundleName.indexOf(bundleExt)) + ".d.ts"; + var declarationFilepath = path.join(bundleDirectory, declarationName); + ts.sys.writeFile(declarationFilepath, text, writeByteOrderMark); }); }, }; diff --git a/dist/rollup-plugin-typescript2.es.js b/dist/rollup-plugin-typescript2.es.js index 66fd291..f7a65ce 100644 --- a/dist/rollup-plugin-typescript2.es.js +++ b/dist/rollup-plugin-typescript2.es.js @@ -11,7 +11,7 @@ import { sha1 } from 'object-hash'; import * as hash from 'object-hash'; import { blue, green, red, white, yellow } from 'colors/safe'; import * as colors from 'colors/safe'; -import { dirname } from 'path'; +import { basename, dirname, extname, join } from 'path'; import * as path from 'path'; import { sync } from 'resolve'; import * as resolve from 'resolve'; @@ -343,10 +343,10 @@ var TsCache = (function () { this.context.debug(" cache: '" + this.codeCache.path(name) + "'"); if (!this.codeCache.exists(name) || this.isDirty(id, snapshot, false)) { this.context.debug(yellow(" cache miss")); - var data_1 = transform(); - this.codeCache.write(name, data_1); + var transformedData = transform(); + this.codeCache.write(name, transformedData); this.markAsDirty(id, snapshot); - return data_1; + return transformedData; } this.context.debug(green(" cache hit")); var data = this.codeCache.read(name); @@ -378,10 +378,10 @@ var TsCache = (function () { this.context.debug(" cache: '" + cache.path(name) + "'"); if (!cache.exists(name) || this.isDirty(id, snapshot, true)) { this.context.debug(yellow(" cache miss")); - var data_2 = convertDiagnostic(type, check()); - cache.write(name, data_2); + var data_1 = convertDiagnostic(type, check()); + cache.write(name, data_1); this.markAsDirty(id, snapshot); - return data_2; + return data_1; } this.context.debug(green(" cache hit")); var data = cache.read(name); @@ -627,10 +627,22 @@ function typescript(options) { cache().done(); round++; }, - onwrite: function () { + onwrite: function (_a) { + var dest = _a.dest; + // Expect the destination path given in the rollup bundle to be a relative path (if given). Join it with process.cwd() + var bundleDirectory = dest == null ? null : join(process.cwd(), dirname(dest)); + var bundleName = dest == null ? null : basename(dest); + var bundleExt = dest == null ? null : extname(dest); each(declarations, function (_a) { var name = _a.name, text = _a.text, writeByteOrderMark = _a.writeByteOrderMark; - sys.writeFile(name, text, writeByteOrderMark); + // If no 'dest' is given, the bundle has no directory, name or extension. In that case, use the default declaration path given by Typescript. + if (bundleName == null || bundleExt == null || bundleDirectory == null) + return sys.writeFile(name, text, writeByteOrderMark); + // Otherwise, try to play nice with the destination from the rollup config. + // Make sure that the declaration file has the same name as the bundle (but a different extension) + var declarationName = bundleExt === "" ? bundleName + ".d.ts" : bundleName.slice(0, bundleName.indexOf(bundleExt)) + ".d.ts"; + var declarationFilepath = join(bundleDirectory, declarationName); + sys.writeFile(declarationFilepath, text, writeByteOrderMark); }); }, }; diff --git a/src/index.ts b/src/index.ts index dd4731d..e35bde4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,11 +3,13 @@ import { IContext, ConsoleContext, IRollupContext, VerbosityLevel } from "./cont import { LanguageServiceHost } from "./host"; import { TsCache, convertDiagnostic, ICode, IDiagnostics } from "./tscache"; import * as ts from "typescript"; +import {basename, dirname, extname, join} from "path"; import * as fs from "fs-extra"; import * as path from "path"; import * as _ from "lodash"; import * as colors from "colors/safe"; import * as resolve from "resolve"; +import { IRollupOptions } from "./irollup-options"; // tslint:disable-next-line:no-var-requires const createFilter = require("rollup-pluginutils").createFilter; @@ -324,11 +326,23 @@ export default function typescript(options?: IOptions) round++; }, - onwrite() + onwrite({dest}: IRollupOptions) { + // Expect the destination path given in the rollup bundle to be a relative path (if given). Join it with process.cwd() + const bundleDirectory = dest == null ? null : join(process.cwd(), dirname(dest)); + const bundleName = dest == null ? null : basename(dest); + const bundleExt = dest == null ? null : extname(dest); _.each(declarations, ({ name, text, writeByteOrderMark }) => { - ts.sys.writeFile(name, text, writeByteOrderMark); + // If no 'dest' is given, the bundle has no directory, name or extension. In that case, use the default declaration path given by Typescript. + if (bundleName == null || bundleExt == null || bundleDirectory == null) return ts.sys.writeFile(name, text, writeByteOrderMark); + + // Otherwise, try to play nice with the destination from the rollup config. + // Make sure that the declaration file has the same name as the bundle (but a different extension) + const declarationName = bundleExt === "" ? `${bundleName}.d.ts` : `${bundleName.slice(0, bundleName.indexOf(bundleExt))}.d.ts`; + const declarationFilepath = join(bundleDirectory, declarationName); + + ts.sys.writeFile(declarationFilepath, text, writeByteOrderMark); }); }, }; diff --git a/src/irollup-options.ts b/src/irollup-options.ts new file mode 100644 index 0000000..0c786b2 --- /dev/null +++ b/src/irollup-options.ts @@ -0,0 +1,3 @@ +export interface IRollupOptions { + dest?: string; +} diff --git a/src/tscache.ts b/src/tscache.ts index c0725bd..0cc9b2e 100644 --- a/src/tscache.ts +++ b/src/tscache.ts @@ -148,10 +148,10 @@ export class TsCache { this.context.debug(colors.yellow(" cache miss")); - const data = transform(); - this.codeCache.write(name, data); + const transformedData = transform(); + this.codeCache.write(name, transformedData); this.markAsDirty(id, snapshot); - return data; + return transformedData; } this.context.debug(colors.green(" cache hit")); @@ -200,10 +200,10 @@ export class TsCache { this.context.debug(colors.yellow(" cache miss")); - const data = convertDiagnostic(type, check()); - cache.write(name, data); + const convertedData = convertDiagnostic(type, check()); + cache.write(name, convertedData); this.markAsDirty(id, snapshot); - return data; + return convertedData; } this.context.debug(colors.green(" cache hit"));