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

Introduce JSX support #5668

Merged
merged 62 commits into from
Oct 2, 2024
Merged

Introduce JSX support #5668

merged 62 commits into from
Oct 2, 2024

Conversation

lukastaegert
Copy link
Member

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

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:

  • preserving JSX: This keeps JSX tags in the output but will take care of tree-shaking and name deconflicting. This is the main reason this feature is introduced in the first place. With the next major Rollup version, I would ideally like to remove support for returning an AST from plugins. Together with the removal of acorn plugin support, this means it is no longer possible to run Rollup with unsupported syntax. As JSX was one major use case, we are adding this now as a core feature, which will also provide better tree-shaking support and avoid hacks for handling deconflicting.
  • classic transformation: This is the classic React transformation, but it can be configured for any other framework, see below.
  • automatic transformation: This uses the "new" React transformation, but with fallback to the classic transformation in edge cases.

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

Type: false | JsxPreset | JsxOptions
CLI: --jsx <preset>/--no-jsx
Default: false
type JsxPreset = 'react' | 'react-jsx' | 'preserve' | 'preserve-react';

type JsxOptions =
	| {
			mode: 'preserve';
			factory: string | null;
			fragment: string | null;
			importSource: string | null;
			preset: JsxPreset | null;
	  }
	| {
			mode: 'classic';
			factory: string;
			fragment: string;
			importSource: string | null;
			preset: JsxPreset | null;
	  }
	| {
			mode: 'automatic';
			factory: string;
			importSource: string;
			jsxImportSource: string;
			preset: JsxPreset | null;
	  };

Allows Rollup to process JSX syntax to either preserve or transform it depending on the jsx.mode. If set to false, 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 to React.createElement calls, where React is the default import from "react". This is the same as setting "jsx": "react" in the TypeScript compiler options.
    ({
    	mode: 'classic',
    	factory: 'React.createElement',
    	fragment: 'React.Fragment',
    	importSource: 'react'
    });
  • "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.
    ({
    	mode: 'automatic',
    	factory: 'React.createElement',
    	importSource: 'react',
    	jsxImportSource: 'react/jsx-runtime'
    });
  • "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.
    ({
    	mode: 'preserve',
    	factory: null,
    	fragment: null,
    	importSource: null
    });
  • "preserve-react": This will preserve JSX in the output but ensure that the default export of "react" is in scope as the React variable.
    ({
    	mode: 'preserve',
    	factory: 'React.createElement',
    	fragment: 'React.Fragment',
    	importSource: 'react'
    });

jsx.mode

Type: "preserve" | "classic" | "automatic"
CLI: --jsx.mode <mode>
Default: "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:

    ({
    	mode: 'classic',
    	factory: 'h',
    	fragment: 'Fragment',
    	importSource: 'preact'
    });

    This would perform the following transformation:

    // input
    console.log(<div>hello</div>);
    
    // output
    import { h } from 'preact';
    console.log(/*#__PURE__*/ h('div', null, 'hello'));
  • "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 the jsx.jsxImportSource to transform JSX. As there are certain edge cases, this mode may still fall back to using the classic transformations when using the key property together with spread attributes. To this end, you can still specify jsx.importSource, jsx.factory, and jsx.fragment to configure classic mode.

jsx.factory

Type: string | null
CLI: --jsx.factory <factory>
Default: "React.createElement" or null

The function Rollup uses to create JSX elements in "classic" mode or as a fallback in "automatic" mode. This is usually React.createElement for React or h for other frameworks. In "preserve" mode, this will ensure that the factory is present if a jsx.importSource is specified, or otherwise not overridden by local variables. Only in "preserve" mode it is possible to set this value to null, in which case Rollup will not take care to keep any particular factory function in scope.

If the value contains a "." like React.createElement and an jsx.importSource is specified, Rollup will assume that the left part, e.g. React, refers to the default export of the jsx.importSource. Otherwise, Rollup assumes it is a named export.

jsx.fragment

Type: string | null
CLI: --jsx.fragment <fragment>
Default: "React.Fragment" or null

The element function Rollup uses to create JSX fragments. This is usually React.Fragment for React or Fragment for other frameworks. In "preserve" mode, this will ensure that the fragment is present as an import if an jsx.importSource is specified, or otherwise not overridden by local variables. Only in "preserve" mode it is possible to set this value to null, in which case Rollup will not take care to keep any particular fragment function in scope.

If the value contains a "." like React.Fragment and an jsx.importSource is specified, Rollup will assume that the left part, e.g. React, refers to the default export of the jsx.importSource. Otherwise, Rollup assumes it is a named export.

jsx.importSource

Type: string | null
CLI: --jsx.importSource <library>
Default: null

Where to import the element factory function and/or the fragment element from. If left to null, Rollup will assume that factory and fragment refer to global variables and make sure they are not shadowed by local variables.

jsx.jsxImportSource

Type: string
CLI: --jsx.jsxImportSource <library>
Default: "react/jsx-runtime"

When using "automatic" mode, this will specify from where to import the jsx, jsxs and Fragment helpers needed for that transformation. It is not possible to get those from a global variable.

jsx.preset

Type: JsxPreset
CLI: --jsx.preset <value>

Allows choosing one of the presets listed above while overriding some options.

// ---cut-start---
/** @type {import('rollup').RollupOptions} */
// ---cut-end---
export default {
	jsx: {
		preset: 'react',
		importSource: 'preact',
		factory: 'h'
	}
	// ...
};

Copy link

vercel bot commented Sep 20, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rollup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 2, 2024 7:11am

Copy link

github-actions bot commented Sep 20, 2024

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:
https://rollup-h2uuycpjy-rollup-js.vercel.app/repl/?pr=5668

Copy link

github-actions bot commented Sep 20, 2024

Performance report!

Rough benchmark

Command Mean [s] Min [s] Max [s] Relative
node _benchmark/previous/bin/rollup -i ./perf/entry.js -o _benchmark/result/previous.js 9.837 ± 0.058 9.784 9.899 1.01 ± 0.01
node _benchmark/current/bin/rollup -i ./perf/entry.js -o _benchmark/result/current.js 9.740 ± 0.042 9.697 9.781 1.00

Internal benchmark

  • BUILD: 8812ms, 751 MB
    • initialize: 0ms, 26.2 MB
    • generate module graph: 3372ms, 575 MB
      • generate ast: 1626ms (+48ms, +3.1%), 568 MB
    • sort and bind modules: 478ms, 617 MB
    • mark included statements: 4930ms, 751 MB
      • treeshaking pass 1: 1696ms, 717 MB
      • treeshaking pass 2: 805ms, 743 MB
      • treeshaking pass 3: 315ms, 748 MB
      • treeshaking pass 4: 292ms, 745 MB
      • treeshaking pass 5: 342ms, 750 MB
      • treeshaking pass 6: 278ms, 748 MB
      • treeshaking pass 7: 257ms, 754 MB
      • treeshaking pass 8: 250ms, 752 MB
      • treeshaking pass 9: 224ms, 758 MB
      • treeshaking pass 10: 227ms, 756 MB
      • treeshaking pass 11: 224ms, 751 MB
  • GENERATE: 956ms, 1.02 GB
    • initialize render: 0ms, 915 MB
    • generate chunks: 89ms, 921 MB
      • optimize chunks: 0ms, 918 MB
    • render chunks: 843ms, 1 GB
    • transform chunks: 23ms, 1.02 GB
    • generate bundle: 0ms, 1.02 GB

Copy link

codecov bot commented Sep 20, 2024

Codecov Report

Attention: Patch coverage is 99.52941% with 2 lines in your changes missing coverage. Please review.

Project coverage is 99.06%. Comparing base (ed98e08) to head (98f5c5a).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/ast/nodes/JSXAttribute.ts 91.66% 1 Missing ⚠️
src/ast/nodes/JSXIdentifier.ts 94.44% 0 Missing and 1 partial ⚠️
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.
📢 Have feedback on the report? Share it here.

@Decruiseweb3

This comment has been minimized.

@lukastaegert lukastaegert merged commit ca186ee into master Oct 2, 2024
38 checks passed
@lukastaegert lukastaegert deleted the wr24-jsx branch October 2, 2024 09:25
Copy link

github-actions bot commented Oct 2, 2024

This PR has been released as part of rollup@4.24.0. You can test it via npm install rollup.

Ali3219

This comment was marked as spam.

@Maliksb11
Copy link

ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants