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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: split out an addDeclaration func #392

Merged
merged 1 commit into from
Aug 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import findCacheDir from "find-cache-dir";
import { RollupContext } from "./rollupcontext";
import { ConsoleContext, IContext, VerbosityLevel } from "./context";
import { LanguageServiceHost } from "./host";
import { TsCache, convertDiagnostic, convertEmitOutput, getAllReferences } from "./tscache";
import { TsCache, convertDiagnostic, convertEmitOutput, getAllReferences, ICode } from "./tscache";
import { tsModule, setTypescriptModule } from "./tsproxy";
import { IOptions } from "./ioptions";
import { parseTsConfig } from "./parse-tsconfig";
Expand Down Expand Up @@ -66,6 +66,16 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
noErrors = false;
}

const addDeclaration = (id: string, result: ICode) =>
{
if (!result.dts)
return;

const key = normalize(id);
declarations[key] = { type: result.dts, map: result.dtsmap };
context.debug(() => `${blue("generated declarations")} for '${key}'`);
}

/** to be called at the end of Rollup's build phase, before output generation */
const buildDone = (): void =>
{
Expand Down Expand Up @@ -238,12 +248,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
context.debug(() => `${green(" watching")}: ${result.references!.join("\nrpt2: ")}`);
}

if (result.dts)
{
const key = normalize(id);
declarations[key] = { type: result.dts, map: result.dtsmap };
context.debug(() => `${blue("generated declarations")} for '${key}'`);
}
addDeclaration(id, result);

// if a user sets this compilerOption, they probably want another plugin (e.g. Babel, ESBuild) to transform their TS instead, while rpt2 just type-checks and/or outputs declarations
// note that result.code is non-existent if emitDeclarationOnly per https://github.com/ezolenko/rollup-plugin-typescript2/issues/268
Expand Down Expand Up @@ -328,10 +333,8 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
return;

context.debug(() => `generating missed declarations for '${key}'`);
const output = service.getEmitOutput(key, true);
const out = convertEmitOutput(output);
if (out.dts)
declarations[key] = { type: out.dts, map: out.dtsmap };
const out = convertEmitOutput(service.getEmitOutput(key, true));
addDeclaration(key, out);
});

const emitDeclaration = (key: string, extension: string, entry?: tsTypes.OutputFile) =>
Expand Down