-
Notifications
You must be signed in to change notification settings - Fork 93
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
Support React JSX automatic transform #94
base: master
Are you sure you want to change the base?
Conversation
You can test this today using |
if (typeof filename === 'string' && typeof opts.filename !== 'undefined') { | ||
throw new TypeError('the "filename" option may only be provided when transforming code'); | ||
} | ||
if (typeof filename === 'undefined' && typeof opts.filename !== 'string') { | ||
throw new TypeError('the "filename" option is required when transforming code'); | ||
} | ||
if (!path.scope.hasBinding('React')) { | ||
|
||
if (!opts.noReactAutoImport && !rootPath.scope.hasBinding('React')) { |
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.
Can you help me understand why this line alone is not sufficient to address the issue?
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.
The applyPlugin
calls need to be made before subsequent plugins (specifically the next/babel
preset) are called. The previous method called the JSX transform's visitor.Program.enter
before doing those, causing the JSX transform to incorrectly think that the file didn't contain any JSX.
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.
Transform ordering is something that is usually dealt with in the babel config. This does mean, if you need a plugin to run before a preset, that you have to create your own preset that uses the plugin, and then use that preset in your own config instead of that plugin.
If you do that, does that obviate the need for all the larger changes?
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.
Yes, but that would require a lot of work for anyone that uses presets. I think it makes more sense to ensure the plugin runs before everything else does. I believe that other plugins in the plugins
array are also run before applyPlugin
.
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.
That's the nature of babel, though - and not something individual transforms should be solving.
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.
I'd assume that those using it need to ensure that it runs last.
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.
The thing is, it can never run last if the visitor isn't written this way. visitor.Program.enter
of all plugins is run before we ever hit an applyPlugin
, and the React JSX transform's visitor.Program.enter
determines whether or not it will operate on the file.
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.
aha, ok thanks for clarifying - iow, we must write it this way explicitly because of the way the new jsx transform is authored.
Might this cause an ordering change that could break existing users?
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.
It's possible, since subsequent plugins will now be seeing the SVGs as JSX instead of as imports
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.
Could we make the ordering changes pivot based on the noReactAutoImport
setting?
wow! this is so cool I need this. |
Hi! |
@diegopelusa its been reviewed, and is waiting for a response from the PR author. |
Many thanks @ljharb !!! |
Hey sorry, I've been a bit busy recently, but if you want my changes you can install |
Many thanks @macalinao!!!! |
Any chance to merge this code? |
You may upgrade to Next.js 10 and React 17 while using the classic React runtime via {
"presets": [["next/babel", { "preset-react": { "runtime": "classic" } }]],
"plugins": ["babel-plugin-inline-react-svg"]
} Please be patient towards the maintainers and the PR’s author, especially in the rough times we are living in nowadays. |
Hi @kripod ! |
This is waiting on the OP to make the changes asked about here, or, for someone else to comment here (not a new PR, please) with a link to those updated changes, so i can pull them in to this PR. |
@macalinao Any chance you could follow this one up? Would love to see your work get merged. :) |
The React JSX transform detects JSX by seeing if it's already in the file, but it won't know about transformed JSX if the node visitors were never hit.
https://github.com/babel/babel/blob/02fc9e835ceac71eb4de7dac6137fd0489176c29/packages/babel-helper-builder-react-jsx-experimental/src/index.js#L328
This new functionality can be enabled with
noReactAutoImport: true
in.babelrc
.Fixes #91.