Skip to content

Commit ec0568b

Browse files
committed
(fix): declaration maps should have correct sources
- previously, declarationDir was set to cwd if useTsconfigDeclarationDir wasn't true, however, declarations aren't output to cwd, but to Rollup's output destination, so this was incorrect - instead, don't set declarationDir, which defaults it to outDir, which is currently set to a placeholder - previously, it rewrote declarations to output to Rollup's dest from cwd, now rewrite from outDir placeholder instead - and add a rewrite of sources to match relative path from Rollup's output dest instead of outDir placeholder - also change the one line in the docs that says it'll be `process.cwd()`; every other reference says it'll be the output dest
1 parent 294d654 commit ec0568b

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ The plugin inherits all compiler options and file lists from your `tsconfig.json
4646
* `noEmit`: false
4747
* `inlineSourceMap`: false (see [#71](https://github.com/ezolenko/rollup-plugin-typescript2/issues/71))
4848
* `outDir`: `./placeholder` in cache root, see [83](https://github.com/ezolenko/rollup-plugin-typescript2/issues/83) and [Microsoft/TypeScript/issues/24715](https://github.com/Microsoft/TypeScript/issues/24715)
49-
* `declarationDir`: `process.cwd()` (*only if `useTsconfigDeclarationDir` is false in the plugin options*)
49+
* `declarationDir`: Rollup's `output.file` or `output.dir` (*only if `useTsconfigDeclarationDir` is false in the plugin options*)
5050
* `moduleResolution`: `node` (*`classic` is [deprecated](https://www.typescriptlang.org/docs/handbook/module-resolution.html). It also breaks this plugin, see [#12](https://github.com/ezolenko/rollup-plugin-typescript2/issues/12) and [#14](https://github.com/ezolenko/rollup-plugin-typescript2/issues/14)*)
5151
* `allowNonTsExtensions`: true to let other plugins on the chain generate typescript, update plugin's include filter to pick them up (see [#111](https://github.com/ezolenko/rollup-plugin-typescript2/issues/111))
5252

src/get-options-overrides.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as _ from "lodash";
66
import { join } from "path";
77
import { IContext } from "./context";
88

9-
export function getOptionsOverrides({ useTsconfigDeclarationDir, cacheRoot, cwd }: IOptions, preParsedTsconfig?: tsTypes.ParsedCommandLine): tsTypes.CompilerOptions
9+
export function getOptionsOverrides({ useTsconfigDeclarationDir, cacheRoot }: IOptions, preParsedTsconfig?: tsTypes.ParsedCommandLine): tsTypes.CompilerOptions
1010
{
1111
const overrides: tsTypes.CompilerOptions = {
1212
noEmitHelpers: false,
@@ -24,11 +24,9 @@ export function getOptionsOverrides({ useTsconfigDeclarationDir, cacheRoot, cwd
2424
if (preParsedTsconfig.options.module === undefined)
2525
overrides.module = tsModule.ModuleKind.ES2015;
2626

27-
const declaration = preParsedTsconfig.options.declaration;
28-
if (!declaration)
27+
// only set declarationDir if useTsconfigDeclarationDir is enabled
28+
if (!useTsconfigDeclarationDir)
2929
overrides.declarationDir = undefined;
30-
if (declaration && !useTsconfigDeclarationDir)
31-
overrides.declarationDir = cwd;
3230

3331
// unsetting sourceRoot if sourceMap is not enabled (in case original tsconfig had inlineSourceMap set that is being unset and would cause TS5051)
3432
const sourceMap = preParsedTsconfig.options.sourceMap;

src/index.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import { parseTsConfig } from "./parse-tsconfig";
1212
import { printDiagnostics } from "./print-diagnostics";
1313
import { TSLIB, TSLIB_VIRTUAL, tslibSource, tslibVersion } from "./tslib";
1414
import { blue, red, yellow, green } from "colors/safe";
15-
import { relative } from "path";
15+
import { relative, dirname, resolve as pathResolve } from "path";
1616
import { normalize } from "./normalize";
1717
import { satisfies } from "semver";
1818
import findCacheDir from "find-cache-dir";
19-
import { PluginImpl, PluginContext, InputOptions, OutputOptions, MinimalPluginContext, TransformResult } from "rollup";
19+
import { PluginImpl, PluginContext, InputOptions, OutputOptions, MinimalPluginContext, TransformResult, SourceMap } from "rollup";
2020
import { createFilter } from "./get-options-overrides";
2121

2222
type RPT2Options = Partial<IOptions>;
@@ -365,11 +365,27 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
365365
}
366366
else
367367
{
368-
const relativePath = relative(pluginOptions.cwd, fileName);
368+
// don't mutate the entry because generateBundle gets called multiple times
369+
let entryText = entry.text
370+
const declarationDir = (_output.file ? dirname(_output.file) : _output.dir) as string;
371+
const cachePlaceholder = `${pluginOptions.cacheRoot}/placeholder`
372+
373+
// modify declaration map sources to correct relative path
374+
if (extension === ".d.ts.map") {
375+
const parsedText = JSON.parse(entryText) as SourceMap;
376+
// invert back to absolute, then make relative to declarationDir
377+
parsedText.sources = parsedText.sources.map(source => {
378+
const absolutePath = pathResolve(cachePlaceholder, source);
379+
return relative(declarationDir, absolutePath);
380+
});
381+
entryText = JSON.stringify(parsedText);
382+
}
383+
384+
const relativePath = relative(cachePlaceholder, fileName);
369385
context.debug(() => `${blue("emitting declarations")} for '${key}' to '${relativePath}'`);
370386
this.emitFile({
371387
type: "asset",
372-
source: entry.text,
388+
source: entryText,
373389
fileName: relativePath,
374390
});
375391
}

0 commit comments

Comments
 (0)