ezolenko / rollup-plugin-typescript2

Rollup plugin for typescript with compiler errors.
MIT License
820 stars 71 forks source link

fix: handle all type-only imports by piping TS imports #406

Closed agilgur5 closed 2 years ago

agilgur5 commented 2 years ago

~NOTE: this is built on top of #403 as it changes its code a bit. #403 is itself built on top of #386. As such, I've marked this PR as "Draft" until those PRs are merged.~ Rebased on top and marked as ready for review. (surprisingly clean rebase)

Also this might be the first time this repo has more open PRs than open issues 😮

Summary

Completely handle the long-standing issue of type-only imports

Details

Review Notes

  1. I double-checked that the transform hook could be async in the minimum Rollup version we support.

    • The in-line linked docs are from Rollup v1.18.0 as I couldn't find a mention of async in the CHANGELOG.md.
  2. This could be considered "breaking", but the bundle ordering should realistically not affect anyone (only tests that rely on ordering, which was never guaranteed by Rollup as this.load could be called by any plugin at any time) and this should only be additive, in that it increases the number of files that are type-checked / generated declarations for.

    • So personally, I don't think this needs a minor bump. If it does get a minor bump, we should release a patch before merging this as I've had a bunch of other fixes in the past month or two that have not yet been released.
  3. Related to that, I specifically did not touch the "missed" type-checking / declaration generation that uses parsedConfig.fileNames to partly workaround this issue (i.e. #345 and the respective declaration generation block)

    • Removing those would almost certainly be considered breaking, since it could remove some files from type-checking / declaration generation. See also the "Note" in #345
    • While #211 says rpt2 shouldn't process those, I don't necessarily agree with that statement, because tsc will process those. Not processing parsedConfig.fileNames would mean only processing the Rollup input, acting as if the tsconfig had files with only that single file from the Rollup input in it. That behavior could make sense to some, but not others, so I'm not sure that that's ideal. The "Note" in #345 mentions how users may use include globs or files arrays to list other files they want type-checking and declarations. These files may not be considered part of the Rollup "bundle" however, so I could see an argument for either direction.
    • In any case, this has not been changed for now. If warranted, we could remove this in a separate PR, and that one would be breaking, but I would not say that this PR in its current state is breaking.

References

This also fixes various downstream issues:

and probably (hard to tell without more details from issue author):

agilgur5 commented 2 years ago
  • While #211 says rpt2 shouldn't process those, I don't necessarily agree with that statement, because tsc will process those. Not processing parsedConfig.fileNames would mean only processing the Rollup input, acting as if the tsconfig had files with only that single file from the Rollup input in it. That behavior could make sense to some, but not others, so I'm not sure that that's ideal. [...] These files may not be considered part of the Rollup "bundle" however, so I could see an argument for either direction.

I happened to stumble upon this yesterday -- it turns out that ts-loader (in Webpack-land) actually does this as well, acting like tsc by default, with the onlyCompileBundledFiles option available to do, as named, only consider files that are part of the "bundle".

So doing the same as ts-loader's default is probably optimal community-wise. I don't think we necessarily need the onlyCompileBundledFiles option though, as the same is achievable by changing tsconfig files (in a tsconfigOverride, for instance)

ezolenko commented 2 years ago

There is a conflict with previous PR, but otherwise looks good

agilgur5 commented 2 years ago

Fixed the conflict 👍

agilgur5 commented 1 year ago

Welp, realized while testing something that this.load was only added in Rollup 2.60.0, much later than this.resolve...

That's my bad, adding a hotfix now to make this backward-compatible with our minimum 1.26.3 requirement -- see #424