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

Babel JSX import plugin: Support short Fragment syntax #12207

Closed
sirreal opened this issue Nov 22, 2018 · 2 comments · Fixed by #15120
Closed

Babel JSX import plugin: Support short Fragment syntax #12207

sirreal opened this issue Nov 22, 2018 · 2 comments · Fixed by #15120
Assignees
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience Good First Issue An issue that's suitable for someone looking to contribute for the first time [Status] In Progress Tracking issues with work in progress [Tool] Babel plugin import JSX pragma /packages/babel-plugin-import-jsx-pragma

Comments

@sirreal
Copy link
Member

sirreal commented Nov 22, 2018

Is your feature request related to a problem? Please describe.

The @wordpress/babel-plugin-import-jsx-pragma package allows you to use JSX without adding the React import. In combination with @babel/transform-react-jsx, this makes it simple to use @wordpress/element to write JSX as the README describes.

However, when using the JSX Fragment <>short syntax</>, the recommended @babel/transform-react-jsx plugin errors if the pragma option is set but pragmaFrag is not set and it needs to transform <></>.

transform-react-jsx: pragma has been set but pragmafrag has not been set

See the JSX transform options for details.

Describe the solution you'd like

The existing options for the don't make it clear where the plugin might be extended to support Fragment options. With React.Fragment, no additions should be necessary. However, when using createElement and Fragment from @wordpress/element, a Fragment import is also necessary. It might look something like this, closely mirroring the JSX transform options:

module.exports = {
	plugins: [
		[ '@wordpress/babel-plugin-import-jsx-pragma', {
			scopeVariable: 'createElement',
			source: '@wordpress/element',
			isDefault: false,

			/**
			 * Additional option
			 * Maintains backwards compatibility
			 * "Frag" suffix mirrors transform plugin
			 */
			scopeVariableFrag: 'Fragment', // default to null: no import is added when <></> is observed

			/**
			 * Can we assume `Fragment` is from the same package and _not_ the default?
			 * These options are then redundant.
			 * In particular, multiple `isDefault` from the same package would likely be problematic.
			 */
			// sourceFrag: 'Fragment',
			// isDefaultFrag: false,
		} ],
		[ '@babel/transform-react-jsx', {
			pragma: 'createElement',
			pragmaFrag: 'Fragment',
		} ],
	],
};

Then, when the transform is applied:

function MyComponent( props ) {
  return <>
    <h1>Hello!</h1>
    <h2>👋</h2>
  </>;
}

/* ⬇️ After transform ⬇️ */

import { createElement, Fragment } from '@wordpress/element';
function MyComponent( props ) {
  return <>
    <h1>Hello!</h1>
    <h2>👋</h2>
  </>;
}

Describe alternatives you've considered

You can set the pragmaFrag for the @babel/transform-react-jsx, but you need to ensure that the import is present, which seems to defeat the purpose of the way these plugins are designed to work.

// Original needs to import Fragment. Defeats purpose 😞
import { Fragment } from '@wordpress/element';
function MyComponent( props ) {
  return <>
    <h1>Hello!</h1>
    <h2>👋</h2>
  </>;
}

/* ⬇️ After transform ⬇️ */

// Original needs to import Fragment. Defeats purpose 😞
import { createElement, Fragment } from '@wordpress/element';
function MyComponent( props ) {
  return <>
    <h1>Hello!</h1>
    <h2>👋</h2>
  </>;
}
@designsimply designsimply added [Feature] Extensibility The ability to extend blocks or the editing experience [Tool] Babel plugin import JSX pragma /packages/babel-plugin-import-jsx-pragma Needs Technical Feedback Needs testing from a developer perspective. labels Nov 23, 2018
@gziolo gziolo added the Good First Issue An issue that's suitable for someone looking to contribute for the first time label Apr 2, 2019
@gziolo
Copy link
Member

gziolo commented Apr 2, 2019

Yes, this sounds great. Let’s do it to make it easier to use Fragment.

@gziolo gziolo removed the Needs Technical Feedback Needs testing from a developer perspective. label Apr 2, 2019
@gziolo gziolo added the Needs Dev Ready for, and needs developer efforts label Apr 10, 2019
@sirreal
Copy link
Member Author

sirreal commented Apr 23, 2019

Working on this in #15120

@sirreal sirreal self-assigned this Apr 23, 2019
@gziolo gziolo added [Status] In Progress Tracking issues with work in progress and removed Needs Dev Ready for, and needs developer efforts labels Apr 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience Good First Issue An issue that's suitable for someone looking to contribute for the first time [Status] In Progress Tracking issues with work in progress [Tool] Babel plugin import JSX pragma /packages/babel-plugin-import-jsx-pragma
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants