Skip to content

Commit

Permalink
refactor: use a common function for resolution checks
Browse files Browse the repository at this point in the history
- 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`
  • Loading branch information
agilgur5 committed Oct 10, 2022
1 parent 61ef625 commit 15bd6ec
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
context.debug(() => `${blue("generated declarations")} for '${key}'`);
}

/** common resolution check -- only resolve files that aren't declarations and pass `filter` */
const shouldResolve = (id: string): boolean => {
if (id.endsWith(".d.ts"))
return false;

if (!filter(id))
return false;

return true;
}

/** to be called at the end of Rollup's build phase, before output generation */
const buildDone = (): void =>
{
Expand Down Expand Up @@ -204,10 +215,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
if (!resolved)
return;

if (resolved.endsWith(".d.ts"))
return;

if (!filter(resolved))
if (!shouldResolve(resolved))
return;

cache.setDependency(resolved, importer);
Expand Down Expand Up @@ -275,9 +283,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
if (result.references && supportsThisLoad) {
for (const ref of result.references) {
// 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)
if (ref.endsWith(".d.ts"))
continue;
if (!filter(ref))
if (!shouldResolve(ref))
continue;

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

0 comments on commit 15bd6ec

Please sign in to comment.