Skip to content

Commit 15bd6ec

Browse files
committed
refactor: use a common function for resolution checks
- since the same logic is used in `resolveId` and these _should_ be equivalent - in the future, we might want to add more common logic to this function, e.g. `getAllReferences` removes `undefined` and uses `moduleNameResolver` as well, similar to `resolveId` - may not be so easy, so TBD - for instance, even moving the `undefined` check into the func required adding a type guard, as the compiler wasn't quite able to infer that passing the func meant it was not `undefined`
1 parent 61ef625 commit 15bd6ec

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/index.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
8080
context.debug(() => `${blue("generated declarations")} for '${key}'`);
8181
}
8282

83+
/** common resolution check -- only resolve files that aren't declarations and pass `filter` */
84+
const shouldResolve = (id: string): boolean => {
85+
if (id.endsWith(".d.ts"))
86+
return false;
87+
88+
if (!filter(id))
89+
return false;
90+
91+
return true;
92+
}
93+
8394
/** to be called at the end of Rollup's build phase, before output generation */
8495
const buildDone = (): void =>
8596
{
@@ -204,10 +215,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
204215
if (!resolved)
205216
return;
206217

207-
if (resolved.endsWith(".d.ts"))
208-
return;
209-
210-
if (!filter(resolved))
218+
if (!shouldResolve(resolved))
211219
return;
212220

213221
cache.setDependency(resolved, importer);
@@ -275,9 +283,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
275283
if (result.references && supportsThisLoad) {
276284
for (const ref of result.references) {
277285
// pre-emptively filter out some files (for efficiency, as well as to workaround a Rollup bug: https://github.com/ezolenko/rollup-plugin-typescript2/issues/426#issuecomment-1264812897)
278-
if (ref.endsWith(".d.ts"))
279-
continue;
280-
if (!filter(ref))
286+
if (!shouldResolve(ref))
281287
continue;
282288

283289
const module = await this.resolve(ref, id);

0 commit comments

Comments
 (0)