Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello! First of all, thanks for creating and maintaining this loader.
I noticed that this project recommends always using the
tsx
loader even for.ts
files, and then tries to automatically fall back to using thets
loader in cases where there is a syntax error. I'm reaching out to let you know that this is a performance and correctness issue, and to propose one way of fixing it.It's not valid to compile
.ts
files intsx
mode. Thetsx
syntax is not a superset of thets
syntax. Instead they are two separate but related syntaxes. Here is an example:This file has two valid compilations, one in each syntax:
With the approach of always using the
tsx
loader, you will incorrectly always end up with the second compilation. I gave this example because it's a case where both loaders succeed. But there are other cases where the approach of using thetsx
loader first and then trying thets
loader next will fail to handle valid TypeScript code. This example should compile fine withts
but doesn't compile withtsx
, and this loader doesn't fall back to thets
loader afterward:I don't think you should fix this by extending the set of cases in which the
ts
loader is tried after thetsx
loader fails. Not only is doing this fallback not correct in all cases (e.g. the regular expression case above), but it's also a performance issue because it causes esbuild to run twice for TypeScript files with type casts in them. Given that people are using this loader primarily for the performance benefit, I think it would be a good idea to make sure this loader is only run once.The simplest fix is just to update the documentation to recommend configuring a separate loader for
.ts
files and.tsx
files and to remove the fallback fromtsx
tots
. I suggested this fix because it requires minimal code changes. Note that this is a breaking change for existing users.I just made this a PR instead of an issue so I could show you one way of fixing it. But feel free to close this PR if you would like to fix this a different way. If you want to keep the simplicity of being able to specify a single
/\.tsx?$/
rule, you will need to change your loader to decide to use either thets
loader or thetsx
loader depending on the file extension of the input file.