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

Add JSX transpilation to compiler-generated code #30

Merged
merged 1 commit into from
Dec 14, 2022

Conversation

ndelangen
Copy link
Member

@ndelangen ndelangen commented Dec 13, 2022

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 the babel/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-recommended

I made the interface for compileSync the same as compileAsync.

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

  • Add JSX transpilation to compiler-generated code #30 (@ndelangen)

🚀 Enhancement

🐛 Bug Fix

⚠️ Pushed to next

Authors: 8

…e JSX runtime.

default jsxRuntime = 'automatic'
@ndelangen ndelangen self-assigned this Dec 13, 2022
@ndelangen ndelangen added enhancement New feature or request major Increment the major version when merged labels Dec 13, 2022
@ndelangen ndelangen requested a review from shilman December 13, 2022 15:46
@ndelangen
Copy link
Member Author

I was blocked from committing to the repo, by a pre-commit hook, that I could not get to work on my machine.
I also do not have prettier installed in vs-code, and don't want to do so, due to that would conflict with all my other repos do using eslint.

So I ran yarn prettier . --write every time I wanted code formatted, but that formatted a ton more stuff. Maybe I shouldn't have, but seems to to me that is the intended use?

@ndelangen ndelangen requested a review from IanVS December 13, 2022 15:59
@ndelangen
Copy link
Member Author

ndelangen commented Dec 13, 2022

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

@shilman shilman changed the title refactor to transpile JSX at the end of the process, ensuring a single JSX runtime. Refactor to transpile JSX at the end of the process, ensuring a single JSX runtime Dec 14, 2022
Copy link
Member

@IanVS IanVS left a 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);
Copy link
Member

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);
Copy link
Member

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';
Copy link
Member

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);
Copy link
Member

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;
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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,
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

@shilman shilman changed the title Refactor to transpile JSX at the end of the process, ensuring a single JSX runtime Add JSX transpilation to compiler-generated code Dec 14, 2022
@shilman shilman merged commit 30274fb into next Dec 14, 2022
@shilman shilman deleted the norbert/sb-1086-sb20190-bug-vite4-support-missing-v2 branch December 14, 2022 11:27
@github-actions github-actions bot mentioned this pull request Dec 14, 2022
5 tasks
@github-actions github-actions bot mentioned this pull request Dec 30, 2022
5 tasks
@github-actions github-actions bot mentioned this pull request Jan 6, 2023
5 tasks
@github-actions github-actions bot mentioned this pull request Jan 21, 2023
5 tasks
This was referenced Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request major Increment the major version when merged
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants