Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix): declaration maps should have correct sources #221

Merged
merged 1 commit into from Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -46,7 +46,7 @@ The plugin inherits all compiler options and file lists from your `tsconfig.json
* `noEmit`: false
* `inlineSourceMap`: false (see [#71](https://github.com/ezolenko/rollup-plugin-typescript2/issues/71))
* `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)
* `declarationDir`: `process.cwd()` (*only if `useTsconfigDeclarationDir` is false in the plugin options*)
* `declarationDir`: Rollup's `output.file` or `output.dir` (*only if `useTsconfigDeclarationDir` is false in the plugin options*)
* `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)*)
* `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))

Expand Down
8 changes: 3 additions & 5 deletions src/get-options-overrides.ts
Expand Up @@ -6,7 +6,7 @@ import * as _ from "lodash";
import { join } from "path";
import { IContext } from "./context";

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

const declaration = preParsedTsconfig.options.declaration;
if (!declaration)
// only set declarationDir if useTsconfigDeclarationDir is enabled
if (!useTsconfigDeclarationDir)
overrides.declarationDir = undefined;
if (declaration && !useTsconfigDeclarationDir)
overrides.declarationDir = cwd;

// unsetting sourceRoot if sourceMap is not enabled (in case original tsconfig had inlineSourceMap set that is being unset and would cause TS5051)
const sourceMap = preParsedTsconfig.options.sourceMap;
Expand Down
24 changes: 20 additions & 4 deletions src/index.ts
Expand Up @@ -12,11 +12,11 @@ import { parseTsConfig } from "./parse-tsconfig";
import { printDiagnostics } from "./print-diagnostics";
import { TSLIB, TSLIB_VIRTUAL, tslibSource, tslibVersion } from "./tslib";
import { blue, red, yellow, green } from "colors/safe";
import { relative } from "path";
import { relative, dirname, resolve as pathResolve } from "path";
import { normalize } from "./normalize";
import { satisfies } from "semver";
import findCacheDir from "find-cache-dir";
import { PluginImpl, PluginContext, InputOptions, OutputOptions, MinimalPluginContext, TransformResult } from "rollup";
import { PluginImpl, PluginContext, InputOptions, OutputOptions, MinimalPluginContext, TransformResult, SourceMap } from "rollup";
import { createFilter } from "./get-options-overrides";

type RPT2Options = Partial<IOptions>;
Expand Down Expand Up @@ -365,11 +365,27 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
}
else
{
const relativePath = relative(pluginOptions.cwd, fileName);
// don't mutate the entry because generateBundle gets called multiple times
let entryText = entry.text
Comment on lines +368 to +369
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this took me a few hours to debug and was by far the most time-consuming piece 馃槙 I originally changed entry.text itself which then got changed on top of itself for each output...

Ideally I think generateBundle should have some caching so this isn't repeated per output. I could do that with a cache key inside of declarations{}, but there's a good question as to what that cache key should be (in this case, I think the declarationDir variable is the only thing that is subject to change, but that sounds brittle if more stuff gets added).

const declarationDir = (_output.file ? dirname(_output.file) : _output.dir) as string;
const cachePlaceholder = `${pluginOptions.cacheRoot}/placeholder`

// modify declaration map sources to correct relative path
if (extension === ".d.ts.map") {
const parsedText = JSON.parse(entryText) as SourceMap;
// invert back to absolute, then make relative to declarationDir
parsedText.sources = parsedText.sources.map(source => {
const absolutePath = pathResolve(cachePlaceholder, source);
return relative(declarationDir, absolutePath);
});
entryText = JSON.stringify(parsedText);
}

const relativePath = relative(cachePlaceholder, fileName);
context.debug(() => `${blue("emitting declarations")} for '${key}' to '${relativePath}'`);
this.emitFile({
type: "asset",
source: entry.text,
source: entryText,
fileName: relativePath,
});
}
Expand Down