diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index a7201b0..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "editor.tabSize": 4, - "editor.useTabStops": true, - "editor.dragAndDrop": true -} \ No newline at end of file diff --git a/README.md b/README.md index 547f624..3e497db 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,8 @@ The following compiler options are forced though: * `noEmitHelpers`: true * `importHelpers`: true * `noResolve`: false -* `outDir`: `process.cwd()` +* `outDir`: `process.cwd()`, +* (`declarationDir`: `process.cwd()`) (*only if `useTsconfigDeclarationDir is false in the plugin options*) You will need to set `"moduleResolution": "node"` in `tsconfig.json` if typescript complains about missing `tslib`. See [#12](https://github.com/ezolenko/rollup-plugin-typescript2/issues/12) and [#14](https://github.com/ezolenko/rollup-plugin-typescript2/issues/14). @@ -89,10 +90,15 @@ Plugin takes following options: * `rollupCommonJSResolveHack`: false On windows typescript resolver favors POSIX path, while commonjs plugin (and maybe others?) uses native path as module id. This can result in `namedExports` being ignored if rollup happened to use typescript's resolution. Set to true to pass resolved module path through `resolve()` to match up with `rollup-plugin-commonjs`. + +* `useTsconfigDeclarationDir`: false + + 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. ### 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). +By default, the declaration files will be located in the same directory as the generated Rollup bundle. If you want to override this behavior and instead use the declarationDir ### Watch mode diff --git a/src/get-options-overrides.ts b/src/get-options-overrides.ts index 1ae5c42..4c26ca1 100644 --- a/src/get-options-overrides.ts +++ b/src/get-options-overrides.ts @@ -1,11 +1,13 @@ import {CompilerOptions, ModuleKind} from "typescript"; +import {IOptions} from "./ioptions"; -export function getOptionsOverrides(): CompilerOptions { +export function getOptionsOverrides({useTsconfigDeclarationDir}: IOptions): CompilerOptions { return { module: ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd(), + ...(useTsconfigDeclarationDir ? {} : {declarationDir: process.cwd()}), }; } diff --git a/src/index.ts b/src/index.ts index a819865..6c0de40 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,7 @@ import {join, relative, dirname} from "path"; // tslint:disable-next-line:no-var-requires const createFilter = require("rollup-pluginutils").createFilter; +// tslint:enable-next-line:no-var-requires let watchMode = false; let round = 0; let targetCount = 0; @@ -66,7 +67,7 @@ export default function typescript(options?: Partial) filter = createFilter(pluginOptions.include, pluginOptions.exclude); - parsedConfig = parseTsConfig(pluginOptions.tsconfig, context); + parsedConfig = parseTsConfig(pluginOptions.tsconfig, context, pluginOptions); servicesHost = new LanguageServiceHost(parsedConfig); @@ -240,8 +241,7 @@ export default function typescript(options?: Partial) const baseDeclarationDir = parsedConfig.options.outDir as string; each(declarations, ({ name, text, writeByteOrderMark }) => { - const relativeFromBaseDeclarationDir = relative(baseDeclarationDir, name); - const writeToPath = join(destDirectory, relativeFromBaseDeclarationDir); + const writeToPath = pluginOptions.useTsconfigDeclarationDir ? name : join(destDirectory, relative(baseDeclarationDir, name)); sys.writeFile(writeToPath, text, writeByteOrderMark); }); }, diff --git a/src/ioptions.ts b/src/ioptions.ts index 8d3ef8b..0749ac8 100644 --- a/src/ioptions.ts +++ b/src/ioptions.ts @@ -9,4 +9,5 @@ export interface IOptions abortOnError: boolean; rollupCommonJSResolveHack: boolean; tsconfig: string; + useTsconfigDeclarationDir: boolean; } diff --git a/src/parse-ts-config.ts b/src/parse-ts-config.ts index ab11c05..f7bf7f2 100644 --- a/src/parse-ts-config.ts +++ b/src/parse-ts-config.ts @@ -4,8 +4,9 @@ import {dirname} from "path"; import {printDiagnostics} from "./print-diagnostics"; import {convertDiagnostic} from "./tscache"; import {getOptionsOverrides} from "./get-options-overrides"; +import {IOptions} from "./ioptions"; -export function parseTsConfig(tsconfig: string, context: IContext): ParsedCommandLine { +export function parseTsConfig(tsconfig: string, context: IContext, pluginOptions: IOptions): ParsedCommandLine { const fileName = findConfigFile(process.cwd(), sys.fileExists, tsconfig); if (!fileName) @@ -19,5 +20,5 @@ export function parseTsConfig(tsconfig: string, context: IContext): ParsedComman throw new Error(`failed to parse ${fileName}`); } - return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(), fileName); + return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions), fileName); }