Skip to content

Commit ad29112

Browse files
authored
refactor: split out an addDeclaration func (#392)
- 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
1 parent ed0fbd9 commit ad29112

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/index.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import findCacheDir from "find-cache-dir";
88
import { RollupContext } from "./rollupcontext";
99
import { ConsoleContext, IContext, VerbosityLevel } from "./context";
1010
import { LanguageServiceHost } from "./host";
11-
import { TsCache, convertDiagnostic, convertEmitOutput, getAllReferences } from "./tscache";
11+
import { TsCache, convertDiagnostic, convertEmitOutput, getAllReferences, ICode } from "./tscache";
1212
import { tsModule, setTypescriptModule } from "./tsproxy";
1313
import { IOptions } from "./ioptions";
1414
import { parseTsConfig } from "./parse-tsconfig";
@@ -70,6 +70,16 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
7070
noErrors = false;
7171
}
7272

73+
const addDeclaration = (id: string, result: ICode) =>
74+
{
75+
if (!result.dts)
76+
return;
77+
78+
const key = normalize(id);
79+
declarations[key] = { type: result.dts, map: result.dtsmap };
80+
context.debug(() => `${blue("generated declarations")} for '${key}'`);
81+
}
82+
7383
/** to be called at the end of Rollup's build phase, before output generation */
7484
const buildDone = (): void =>
7585
{
@@ -240,12 +250,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
240250
context.debug(() => `${green(" watching")}: ${result.references!.join("\nrpt2: ")}`);
241251
}
242252

243-
if (result.dts)
244-
{
245-
const key = normalize(id);
246-
declarations[key] = { type: result.dts, map: result.dtsmap };
247-
context.debug(() => `${blue("generated declarations")} for '${key}'`);
248-
}
253+
addDeclaration(id, result);
249254

250255
// 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
251256
// note that result.code is non-existent if emitDeclarationOnly per https://github.com/ezolenko/rollup-plugin-typescript2/issues/268
@@ -329,10 +334,8 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
329334
return;
330335

331336
context.debug(() => `generating missed declarations for '${key}'`);
332-
const output = service.getEmitOutput(key, true);
333-
const out = convertEmitOutput(output);
334-
if (out.dts)
335-
declarations[key] = { type: out.dts, map: out.dtsmap };
337+
const out = convertEmitOutput(service.getEmitOutput(key, true));
338+
addDeclaration(key, out);
336339
});
337340

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

0 commit comments

Comments
 (0)