diff --git a/dist/get-options-overrides.d.ts b/dist/get-options-overrides.d.ts index 794156e..cb8433a 100644 --- a/dist/get-options-overrides.d.ts +++ b/dist/get-options-overrides.d.ts @@ -1,3 +1,3 @@ import { CompilerOptions } from "typescript"; import { IOptions } from "./ioptions"; -export declare function getOptionsOverrides({useTsconfigDeclarationDir}: IOptions): CompilerOptions; +export declare function getOptionsOverrides({useTsconfigDeclarationDir}: IOptions, tsConfigJson?: any): CompilerOptions; diff --git a/dist/rollup-plugin-typescript2.cjs.js b/dist/rollup-plugin-typescript2.cjs.js index a9e9469..2561051 100644 --- a/dist/rollup-plugin-typescript2.cjs.js +++ b/dist/rollup-plugin-typescript2.cjs.js @@ -449,9 +449,10 @@ function printDiagnostics(context, diagnostics) { }); } -function getOptionsOverrides(_a) { +function getOptionsOverrides(_a, tsConfigJson) { var useTsconfigDeclarationDir = _a.useTsconfigDeclarationDir; - return __assign({ module: typescript.ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd() }, (useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() })); + 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() })); } function parseTsConfig(tsconfig, context, pluginOptions) { @@ -464,7 +465,7 @@ function parseTsConfig(tsconfig, context, pluginOptions) { 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), fileName); + return typescript.parseJsonConfigFileContent(result.config, typescript.sys, path.dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName); } // The injected id for helpers. @@ -512,6 +513,7 @@ function typescript$1(options) { abortOnError: true, rollupCommonJSResolveHack: false, tsconfig: "tsconfig.json", + useTsconfigDeclarationDir: false, }); return { options: function (config) { @@ -629,14 +631,17 @@ function typescript$1(options) { 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 == null || pluginOptions.useTsconfigDeclarationDir) - return typescript.sys.writeFile(name, text, writeByteOrderMark); - // 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); - var writeToPath = path.join(destDirectory, path.relative(baseDeclarationDir, name)); + 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); }); diff --git a/dist/rollup-plugin-typescript2.es.js b/dist/rollup-plugin-typescript2.es.js index 2f14aec..d0997d6 100644 --- a/dist/rollup-plugin-typescript2.es.js +++ b/dist/rollup-plugin-typescript2.es.js @@ -448,9 +448,10 @@ function printDiagnostics(context, diagnostics) { }); } -function getOptionsOverrides(_a) { +function getOptionsOverrides(_a, tsConfigJson) { var useTsconfigDeclarationDir = _a.useTsconfigDeclarationDir; - return __assign({ module: ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd() }, (useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() })); + 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() })); } function parseTsConfig(tsconfig, context, pluginOptions) { @@ -463,7 +464,7 @@ function parseTsConfig(tsconfig, context, pluginOptions) { printDiagnostics(context, convertDiagnostic("config", [result.error])); throw new Error("failed to parse " + fileName); } - return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions), fileName); + return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName); } // The injected id for helpers. @@ -511,6 +512,7 @@ function typescript$1(options) { abortOnError: true, rollupCommonJSResolveHack: false, tsconfig: "tsconfig.json", + useTsconfigDeclarationDir: false, }); return { options: function (config) { @@ -628,14 +630,17 @@ function typescript$1(options) { 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 == null || pluginOptions.useTsconfigDeclarationDir) - return sys.writeFile(name, text, writeByteOrderMark); - // 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); - var writeToPath = join(destDirectory, relative(baseDeclarationDir, name)); + 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); }); diff --git a/rollup.config.js b/rollup.config.js index 7c41a54..525ed7a 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -7,6 +7,7 @@ export default { external: [ 'path', + 'fs', 'fs-extra', 'object-assign', 'rollup-pluginutils', diff --git a/src/get-options-overrides.ts b/src/get-options-overrides.ts index 4c26ca1..69adc55 100644 --- a/src/get-options-overrides.ts +++ b/src/get-options-overrides.ts @@ -1,13 +1,16 @@ -import {CompilerOptions, ModuleKind} from "typescript"; -import {IOptions} from "./ioptions"; +import { CompilerOptions, ModuleKind } from "typescript"; +import { IOptions } from "./ioptions"; +import { get } from "lodash"; -export function getOptionsOverrides({useTsconfigDeclarationDir}: IOptions): CompilerOptions { +export function getOptionsOverrides({ useTsconfigDeclarationDir }: IOptions, tsConfigJson?: any): CompilerOptions +{ + const declaration = get(tsConfigJson, "compilerOptions.declaration", false); return { module: ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd(), - ...(useTsconfigDeclarationDir ? {} : {declarationDir: process.cwd()}), + ...(!declaration || useTsconfigDeclarationDir ? {} : {declarationDir: process.cwd()}), }; } diff --git a/src/index.ts b/src/index.ts index 8f97dd5..e912315 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,6 +51,7 @@ export default function typescript(options?: Partial) abortOnError: true, rollupCommonJSResolveHack: false, tsconfig: "tsconfig.json", + useTsconfigDeclarationDir: false, }); return { @@ -235,17 +236,22 @@ export default function typescript(options?: Partial) onwrite({dest}: IRollupOptions) { - const baseDeclarationDir = parsedConfig.options.outDir as string; + const baseDeclarationDir = parsedConfig.options.outDir; each(declarations, ({ name, text, writeByteOrderMark }) => { + 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 == null || pluginOptions.useTsconfigDeclarationDir) return sys.writeFile(name, text, writeByteOrderMark); + 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)); + } - // 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); - const writeToPath = join(destDirectory, relative(baseDeclarationDir, name)); // Write the declaration file to disk. sys.writeFile(writeToPath, text, writeByteOrderMark); }); diff --git a/src/irollup-options.ts b/src/irollup-options.ts index 0c786b2..2505404 100644 --- a/src/irollup-options.ts +++ b/src/irollup-options.ts @@ -1,3 +1,4 @@ -export interface IRollupOptions { +export interface IRollupOptions +{ dest?: string; } diff --git a/src/parse-ts-config.ts b/src/parse-ts-config.ts index f7bf7f2..ff6d423 100644 --- a/src/parse-ts-config.ts +++ b/src/parse-ts-config.ts @@ -6,7 +6,8 @@ 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): ParsedCommandLine +{ const fileName = findConfigFile(process.cwd(), sys.fileExists, tsconfig); if (!fileName) @@ -20,5 +21,5 @@ export function parseTsConfig(tsconfig: string, context: IContext, pluginOptions throw new Error(`failed to parse ${fileName}`); } - return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions), fileName); + return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName); } diff --git a/src/partial.ts b/src/partial.ts index a8a5105..23bd602 100644 --- a/src/partial.ts +++ b/src/partial.ts @@ -1,3 +1 @@ -export declare type Partial = { - [P in keyof T]?: T[P]; -}; +export declare type Partial = { [P in keyof T]?: T[P]; }; diff --git a/tsconfig.json b/tsconfig.json index a91d638..ef879d5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,8 +15,7 @@ "strictNullChecks": true, "forceConsistentCasingInFileNames": true, "noImplicitReturns": true, - "declaration": true, - "declarationDir": "./dist" + "declaration": true }, "include": [ "src/**/*.ts"