-
Notifications
You must be signed in to change notification settings - Fork 9
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
Add JSX transpilation to compiler-generated code #30
Add JSX transpilation to compiler-generated code #30
Conversation
…e JSX runtime. default jsxRuntime = 'automatic'
I was blocked from committing to the repo, by a pre-commit hook, that I could not get to work on my machine. So I ran |
If this is to replace the mdx/jsx/react plugin for vite ( or rather: make it no longer required ), it might need more work to make HMR work..? WDYT @IanVS |
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've opened storybookjs/storybook#20241 which pulls in the canary for this change, and it works a treat!
|
||
export { mdxSync as compileSync }; | ||
if (skipCsf) { | ||
const mdxResult = await mdxCompile(input, options); |
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.
Nit: it looks like this could be moved above the if-check.
|
||
export const transformJSXAsync = async (input: string, jsxOptions: JSXOptions) => { | ||
const babelOptions = getBabelOptions(jsxOptions); | ||
const { code } = await transformAsync(input, babelOptions); |
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.
In the future we could explore transforming jsx with esbuild, which might be faster. But that feels like a separate effort.
@@ -1,4 +1,3 @@ | |||
import { parse } from '@babel/parser'; |
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 looks like this can be removed from package.json
now, it's not used elsewhere.
return mdxResult.toString(); | ||
} | ||
|
||
const mdxResult = mdxCompileSync(input, options); |
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.
Nit: This could also move up above the if (skipCsf)
check.
const babelOptions = getBabelOptions(jsxOptions); | ||
const { code } = transformSync(input, babelOptions); | ||
|
||
return code; |
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 would be ideal if we could get a hold of the map
as well, to enable sourcemaps.
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.
Thank for the review @IanVS, super appreciated. I'll leave the code as is for now, if the code works as is, I'd rather merge and release.
I do believe sourcemaps would be really great to have, but I feel like if I were tasked, I'd likely rewrite the entire thing, yet i don't understand 3/4th of the code.
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.
Fair enough. transformSync
returns a map
, so we'd need to return {code, map}
from this function, and pass it along as the return from the compile
call. Then it's a matter of updating the code that is using this package. But it looks like maybe it's only used in two spots. I'll give it a shot and see what happens. Now seems like the time to do it, while we're making breaking changes.
@@ -148,37 +150,72 @@ export const postprocess = (code: string, extractedExports: string) => { | |||
const first = lines.shift(); | |||
|
|||
return [ | |||
first, |
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.
what's going on here?
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'm dropping that first line, because it's a comment instructing the JSX compiler to use the options passed to it from mdxCompilerOptions
.
We want those options to be disregarded, because we want to use the options from jsxOptions
instead.
I didn't find a way to have the mdxCompiler NOT generate the comment. Though I gave up after like 2 attempts.
The code to extract the first line was already there. I didn't understand why it was extracted and then added back anyway, it made no sense to me. But it was close enough to what I wanted to do: remove the first line comment generated by the mdxCompiler.
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.
Must've been part of some legacy behavior 🤷
Thanks for the clarification! SGTM.
default jsxRuntime = 'automatic'
Issue: #
What Changed
This adds a new third argument to the compileAsync function:
JsxOptions
which mimicks the options you can pass to thebabel/preset-react
. Unfortunately that interface isn't public/exposed, so I copied it from their docs: https://babeljs.io/docs/en/babel-preset-react#with-a-configuration-file-recommendedI made the interface for
compileSync
the same ascompileAsync
.How to test
The unit test say: yes
We need to take this canary release and test it in the storybook repo / sandboxes.
Change Type
maintenance
documentation
patch
minor
major
Version
Published prerelease version:
v1.0.0-next.0
Changelog
💥 Breaking Change
🚀 Enhancement
AddContext
#14 (@tmeasday)🐛 Bug Fix
next
Authors: 8