-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Introduce JSX support #5668
Introduce JSX support #5668
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
5325540
to
468863a
Compare
Thank you for your contribution! ❤️You can try out this pull request locally by installing Rollup via npm install rollup/rollup#wr24-jsx Notice: Ensure you have installed the latest stable Rust toolchain. If you haven't installed it yet, please see https://www.rust-lang.org/tools/install to learn how to download Rustup and install Rust. or load it into the REPL: |
Performance report!Rough benchmark
Internal benchmark
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #5668 +/- ##
==========================================
+ Coverage 99.05% 99.06% +0.01%
==========================================
Files 242 257 +15
Lines 9387 9735 +348
Branches 2482 2574 +92
==========================================
+ Hits 9298 9644 +346
- Misses 58 59 +1
- Partials 31 32 +1 ☔ View full report in Codecov by Sentry. |
This comment has been minimized.
This comment has been minimized.
Signed-off-by: Martin Idel <martin.idel@tngtech.com>
This adds acorn-jsx to test the AST against. Tests will fail if the check fails.
This PR has been released as part of rollup@4.24.0. You can test it via |
ok |
This PR contains:
Are tests included?
Breaking Changes?
List any relevant issue numbers:
Description
This is a big feature that has been in the works since early that year. It was the result of a workshop on that topic and has been thoroughly refined since then. It introduces JSX support in three flavors:
In a way, were are now matching what TypeScript is providing. Special thanks to @Martin-Idel, @felixhuttmann, @AlexDroll and @tiptr who contributed to the original workshop.
Copied from the new documentation:
jsx
false | JsxPreset | JsxOptions
--jsx <preset>
/--no-jsx
false
Allows Rollup to process JSX syntax to either preserve or transform it depending on the
jsx.mode
. If set tofalse
, an error will be thrown if JSX syntax is encountered. You may also choose a preset that will set all options together:"react"
: For transpiling JSX toReact.createElement
calls, whereReact
is the default import from"react"
. This is the same as setting"jsx": "react"
in the TypeScript compiler options."react-jsx"
: This will use the new optimized React transformation introduced with React 17 and is similar to setting"jsx": "react-jsx"
in the TypeScript compiler options."preserve"
: This will preserve JSX in the output. This will still tree-shake unused JSX code and may rename JSX identifiers if there are conflicts in the output."preserve-react"
: This will preserve JSX in the output but ensure that the default export of"react"
is in scope as theReact
variable.jsx.mode
"preserve" | "classic" | "automatic"
--jsx.mode <mode>
"classic"
This will determine how JSX is processed:
"preserve"
: Will keep JSX syntax in the output."classic"
: This will perform a JSX transformation as it is needed by older React versions or other frameworks like for instance Preact. As an example, here is how you would configure jsx for Preact:This would perform the following transformation:
"automatic"
: This will perform a JSX transformation using the new JSX transform introduced with React 17. In this mode, Rollup will try to use helpers from thejsx.jsxImportSource
to transform JSX. As there are certain edge cases, this mode may still fall back to using the classic transformations when using thekey
property together with spread attributes. To this end, you can still specifyjsx.importSource
,jsx.factory
, andjsx.fragment
to configure classic mode.jsx.factory
string | null
--jsx.factory <factory>
"React.createElement"
ornull
The function Rollup uses to create JSX elements in
"classic"
mode or as a fallback in"automatic"
mode. This is usuallyReact.createElement
for React orh
for other frameworks. In"preserve"
mode, this will ensure that the factory is present if ajsx.importSource
is specified, or otherwise not overridden by local variables. Only in"preserve"
mode it is possible to set this value tonull
, in which case Rollup will not take care to keep any particular factory function in scope.If the value contains a
"."
likeReact.createElement
and anjsx.importSource
is specified, Rollup will assume that the left part, e.g.React
, refers to the default export of thejsx.importSource
. Otherwise, Rollup assumes it is a named export.jsx.fragment
string | null
--jsx.fragment <fragment>
"React.Fragment"
ornull
The element function Rollup uses to create JSX fragments. This is usually
React.Fragment
for React orFragment
for other frameworks. In"preserve"
mode, this will ensure that the fragment is present as an import if anjsx.importSource
is specified, or otherwise not overridden by local variables. Only in"preserve"
mode it is possible to set this value tonull
, in which case Rollup will not take care to keep any particular fragment function in scope.If the value contains a
"."
likeReact.Fragment
and anjsx.importSource
is specified, Rollup will assume that the left part, e.g.React
, refers to the default export of thejsx.importSource
. Otherwise, Rollup assumes it is a named export.jsx.importSource
string | null
--jsx.importSource <library>
null
Where to import the element factory function and/or the fragment element from. If left to
null
, Rollup will assume thatfactory
andfragment
refer to global variables and make sure they are not shadowed by local variables.jsx.jsxImportSource
string
--jsx.jsxImportSource <library>
"react/jsx-runtime"
When using
"automatic"
mode, this will specify from where to import thejsx
,jsxs
andFragment
helpers needed for that transformation. It is not possible to get those from a global variable.jsx.preset
--jsx.preset <value>
Allows choosing one of the presets listed above while overriding some options.