Repository navigation
Retain JSX pragma if defined as a comment - #9095
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/9476/ |
|
👍 This is needed to get the new Emotion 10 CSS Prop working when using Typescript with Babel. |
|
Until this is merged, I'm doing: /** @jsx jsx */
import { css, jsx } from '@emotion/core'
+ export const jsxBabelFix = jsxTo prevent linters from complaining 👍 |
|
Isn't it enough to use the |
|
@jfrolich says: #8958 (comment)
|
|
With this PR parsing the comment has exactly the same behavior of using the jsxPragma option. |
|
@nicolo-ribaudo The goal here is to allow the use of different pragmas for different files, which mirrors the behavior of Babel in non-TypeScript projects. |
|
@dstaley Is there a way that you update within this PR the The TypeScript preset seems not being maintained (last publish 3 months ago) (or maybe should I make a side PR, to keep things clear, the thing is that your PR should result in a newer version of |
|
@kevinmarrec yeah it's probably best to keep those separate since there might be a reason the presentation hasn't been updated. |
|
Any update on getting this merge in? @dstaley - Some of us Emotion users are pretty stuck right now. |
|
@Meebix I'm not stuck anymore. I'm using https://emotion.sh/docs/@emotion/babel-preset-css-prop and it works just fine. One thing to note is that I had to make sure that I combined it w/ a custom plugin to handle fragments: // With emotion 10 and @emotion/babel-preset-css-prop, the only reason to import
// React is to get Fragment syntax. This plugin will automatically add a React
// import if a fragment is found so we don't have to remember to import React.
module.exports = function importReactForFragments(babel) {
const { types: t } = babel
const getFragmentImport = () =>
t.importDeclaration(
[t.importSpecifier(t.identifier('React'), t.identifier('default'))],
t.stringLiteral('react')
)
const detectReactImport = (path, state) => {
if (path.node.local.name === 'React') {
state.set('jsxFragmentImportDetected', true)
}
}
return {
visitor: {
Program: {
exit(path, state) {
if (!state.get('jsxFragmentDetected')) return
if (state.get('jsxFragmentImportDetected')) return
path.unshiftContainer('body', getFragmentImport())
},
},
ImportNamespaceSpecifier: detectReactImport,
ImportDefaultSpecifier: detectReactImport,
ImportSpecifier: detectReactImport,
JSXFragment(path, state) {
state.set('jsxFragmentDetected', true)
},
},
}
} |
|
Ah, ok cool @aaronjensen - thanks for the details! |
|
It's still a hack/workaround. Would be great if this can be resolved. |
|
@nicolo-ribaudo is it anything needed to get this merged? This is very much needed for using the latest Emotion version with Typescript & Babel without using hacks. |
…pescript@^7.3.2 (#9497) [skip ci] Bumps to 7.3.2 the version of `@babel/plugin-transform-typescript` used by `@babel/preset-typescript`. This allows us to use `@babel/preset-typescript` and pick up at least one useful bug fix (#9095). A similar PR (#9181) was closed as it bumped to a version that didn't have changes. However reading the comments from that PR, I get the sense that bumping the version in this manner should not be necessary, and should be handled by Lerna. Is this correct? Please let me know if I can make any improvements to this PR, the description or if there's anything else I can do to help.
…ween files (#9798) This PR fixes a bug in support of JSX pragma in Typescript (introduced in #9095). We've noticed this bug while building a large codebase with webpack+babel+typescript. Some files from our projects are using `/** @jsx h*/` to use a custom function for JSX syntax; and some others are using the React syntax. We've noticed that our react imports were being removed by babel because the plugin instance was being reused by `babel-loader` and webpack; and it was causing an error : ``` ReferenceError: React is not defined ``` This PR resets the `jsxPragma` being used to the initial value for each new file being processed. I can't add a unit test for this bug because it requires processing 2 files (one with `/** @jsx something */` then one with the react syntax). I've tested a build of this PR and it correctly fixes the issue for us.
Currently, the TypeScript transform elides JSX pragma imports unless used explicitly. This PR modifies the transform to properly detect the JSX pragma declaration when defined as a comment, and appropriately retains imports if the import matches the pragma. The check was added by copying over the pragma detection from
babel-plugin-transform-react-jsx.