Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(typescript): add support for "jsx: preserve" compiler option #2574

Merged
merged 1 commit into from
Apr 6, 2021

Conversation

duarten
Copy link
Contributor

@duarten duarten commented Mar 30, 2021

When tsc is instructed to preserve jsx, the emitted files have a .jsx
extension for .tsx and .jsx inputs, which means ts_project needs to be
aware of the option to properly define outputs.

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature (please, look at the "Scope of the project" section in the README.md file)
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

The current behavior causes the ts_project rule to fail as it is expecting different outputs.

Issue Number: N/A

What is the new behavior?

In the new behavior, ts_project is aware of the jsx compiler option and sets output files accordingly.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Copy link
Collaborator

@alexeagle alexeagle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need help to get it green?

load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test")
load("//packages/typescript:index.bzl", "ts_project")

# Ensure that a.js produces outDir/a.js, outDir/a.d.ts, and outDir/a.d.ts.map
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment looks out-of-date

# Ensure that a.js produces outDir/a.js, outDir/a.d.ts, and outDir/a.d.ts.map
SRCS = [
"a.tsx",
"b.jsx",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the second file is just redundant? reduce the test case to one file if that gives the same assertions/coverage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea of the second file is to assert that jsx files are not transformed to js files (which happens if jsx is not set to preserve).

@@ -0,0 +1,10 @@
const assert = require('assert');

console.log(process.argv)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove console.log ?

const files = process.argv.slice(2);
assert.ok(files.some(f => f.endsWith('out/a.d.ts')), 'Missing a.d.ts');
assert.ok(files.some(f => f.endsWith('out/a.d.ts.map')), 'Missing a.d.ts.map');
assert.ok(files.some(f => f.endsWith('out/a.jsx'), 'Missing a.jsx.map'));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed some copy-paste here, the endsWith doesn't match the message

if (option === 'jsx') {
return jsxEmit[options[option] as ts.JsxEmit]
}
if (typeof (options[option]) === 'string' && option !== 'jsx') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if rather than repeat the inverse of the jsx condition

@duarten duarten force-pushed the feature/jsx-preserve branch 2 times, most recently from 8f321bf to f6e5fbe Compare March 30, 2021 19:35
@duarten
Copy link
Contributor Author

duarten commented Mar 30, 2021

@alexeagle Trying to get it to green :) I actually just refactored the approach a bit, there's now a "preserve_jsx" attribute instead of "jsx", since we just care about the "preserve" option.

When tsc is instructed to preserve jsx, the emitted files have a .jsx
extension for .tsx and .jsx inputs, which means ts_project needs to be
aware of the option to properly define outputs.
Copy link
Collaborator

@alexeagle alexeagle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at first my reaction was "it's semantically similar to just map jsx=foo from tsconfig into BUILD" but then thinking more, the common case of jsx=react would force people to add a line to their BUILD file. So I like your boolean attribute more.
Thanks for the fix!!

@alexeagle alexeagle merged commit 425dbd6 into bazel-contrib:stable Apr 6, 2021
@duarten duarten deleted the feature/jsx-preserve branch July 22, 2021 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants