Skip to content

Commit

Permalink
refactor: split out an addDeclaration func (#392)
Browse files Browse the repository at this point in the history
- DRY it up and follow the same pattern as `typecheckFile`
- this also standardizes some things like normalization etc so that they're harder to miss
  • Loading branch information
agilgur5 authored Aug 8, 2022
1 parent ed0fbd9 commit ad29112
Showing 1 changed file with 14 additions and 11 deletions.
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 @@ -70,6 +70,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 @@ -240,12 +250,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 @@ -329,10 +334,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

0 comments on commit ad29112

Please sign in to comment.