-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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(remix-dev): add jsxImportSource
config option
#2216
Conversation
Hi @jolle, Welcome, and thank you for contributing to Remix! Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once. You may review the CLA and sign it by adding your name to contributors.yml. Once the CLA is signed, the If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at [email protected]. Thanks! - The Remix team |
Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳 |
@jolle Can you please update this PR to fix conflicts? 🙏🏼 |
Merge conflicts have been fixed. I noticed there has been changes to the testing env that causes the new tests to fail (related to fixtures trying to import the remix package). I'll try to fix the tests soon, though the solution isn't immediately obvious to me – I'll have to go through the changes and find what has broken the fixtures! |
@jolle Please rebase your branch onto latest |
customJSXShimPath
config option
// Mock ESM-only modules as these are not well-supported in | ||
// the Jest environment available. | ||
jest.mock("xdm", () => ({ | ||
__esModule: true, | ||
compile: () => ({}), | ||
})); | ||
jest.mock("remark-frontmatter", () => ({ | ||
__esModule: true, | ||
})); | ||
jest.mock("tsconfig-paths", () => ({ | ||
__esModule: true, | ||
default: { | ||
loadConfig() { | ||
return { | ||
resultType: "failed", | ||
}; | ||
}, | ||
}, | ||
})); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not optimal but regardless of what I did to Babel or Jest these modules seemed to break Jest one way or another.
I've resolved the merge conflicts and fixed the tests. In addition to the caveat above, I downgraded |
customJSXShimPath
config optionjsxImportSource
config option
I have removed the temporary file system injection for adding the custom JSX factory; instead, the import source is loaded by adding a |
jsxImportSource
config optionjsxImportSource
config option
This now works natively on esbuild, adding:
To package.json (and using npm) works without any configs. |
Support for automatic |
Libraries such as emotion (via
@emotion/react
) are commonly used with the tsconfig optionjsxImportSource
, which changes the defaultReact.createElement
calls to the specified source automatically. Because esbuild does not support this out-of-the-box (see evanw/esbuild#718), by using a dynamically created shim file it is possible to emulate this behavior. Although an inline pragma works, it is required to add this to every file instead of being automatically detected.As esbuild does not support inline code injection (see evanw/esbuild#1169), this PR creates a temporary directory with a temporary file for the custom shim. While this is not very optimal, it is the best option I've found (didn't seem like esbuild plugins could do this?). In this PR, the tsconfig.json file is read (if one exists), and a shim is created based on
jsxImportSource
, if one is specified. A new config option,customJSXShimPath
, is exposed as well if a custom path to the JSX factory is required for the shim.This pull request also adds tests for this new behavior, and refactors the tests of
build-test.ts
that were being skipped (because they didn't work). Due to ESM-related issues and conflicts with the Jest environment, a new, simpler app needed to be created without any ESM-only modules, and some modules needed to be mocked during build-time. The JSX factory tests run the built output within the test in a "sandbox" where the calls to the JSX factory can be monitored.This is a quite big new feature – apologies for not creating a discussion first!
Closes: #1633. Related to: #2209.