-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Refactor MDX transformJSX handling #10688
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"astro": patch | ||
--- | ||
|
||
Marks renderer `jsxImportSource` and `jsxTransformOptions` options as deprecated as they are no longer used since Astro 3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,11 @@ | ||
const renderer = { | ||
import type { AstroRenderer } from '../@types/astro.js'; | ||
import { jsxTransformOptions } from './transform-options.js'; | ||
|
||
const renderer: AstroRenderer = { | ||
name: 'astro:jsx', | ||
serverEntrypoint: 'astro/jsx/server.js', | ||
jsxImportSource: 'astro', | ||
jsxTransformOptions: async () => { | ||
// @ts-expect-error types not found | ||
const plugin = await import('@babel/plugin-transform-react-jsx'); | ||
const jsx = plugin.default?.default ?? plugin.default; | ||
const { default: astroJSX } = await import('./babel.js'); | ||
return { | ||
plugins: [ | ||
astroJSX(), | ||
jsx({}, { throwIfNamespace: false, runtime: 'automatic', importSource: 'astro' }), | ||
], | ||
}; | ||
}, | ||
jsxTransformOptions, | ||
}; | ||
|
||
export default renderer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { JSXTransformConfig } from '../@types/astro.js'; | ||
|
||
export async function jsxTransformOptions(): Promise<JSXTransformConfig> { | ||
// @ts-expect-error types not found | ||
const plugin = await import('@babel/plugin-transform-react-jsx'); | ||
const jsx = plugin.default?.default ?? plugin.default; | ||
const { default: astroJSX } = await import('./babel.js'); | ||
return { | ||
plugins: [ | ||
astroJSX(), | ||
jsx({}, { throwIfNamespace: false, runtime: 'automatic', importSource: 'astro' }), | ||
], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# vite-plugin-jsx | ||
# vite-plugin-mdx | ||
|
||
Modifies Vite’s built-in JSX behavior to allow for React, Preact, and Solid.js to coexist and all use `.jsx` and `.tsx` extensions. | ||
Handles transforming MDX via the `astro:jsx` renderer. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,19 @@ | ||
import type { TransformResult } from 'rollup'; | ||
import { type Plugin, type ResolvedConfig, transformWithEsbuild } from 'vite'; | ||
import type { AstroRenderer, AstroSettings } from '../@types/astro.js'; | ||
import type { Logger } from '../core/logger/core.js'; | ||
import type { PluginMetadata } from '../vite-plugin-astro/types.js'; | ||
|
||
import babel from '@babel/core'; | ||
import { type Plugin, transformWithEsbuild } from 'vite'; | ||
import { CONTENT_FLAG, PROPAGATED_ASSET_FLAG } from '../content/index.js'; | ||
import { astroEntryPrefix } from '../core/build/plugins/plugin-component-entry.js'; | ||
import { removeQueryString } from '../core/path.js'; | ||
import tagExportsPlugin from './tag.js'; | ||
|
||
interface TransformJSXOptions { | ||
code: string; | ||
id: string; | ||
mode: string; | ||
renderer: AstroRenderer; | ||
ssr: boolean; | ||
root: URL; | ||
} | ||
|
||
async function transformJSX({ | ||
code, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is moved to |
||
mode, | ||
id, | ||
ssr, | ||
renderer, | ||
root, | ||
}: TransformJSXOptions): Promise<TransformResult> { | ||
const { jsxTransformOptions } = renderer; | ||
const options = await jsxTransformOptions!({ mode, ssr }); | ||
const plugins = [...(options.plugins || [])]; | ||
if (ssr) { | ||
plugins.push(await tagExportsPlugin({ rendererName: renderer.name, root })); | ||
} | ||
const result = await babel.transformAsync(code, { | ||
presets: options.presets, | ||
plugins, | ||
cwd: process.cwd(), | ||
filename: id, | ||
ast: false, | ||
compact: false, | ||
sourceMaps: true, | ||
configFile: false, | ||
babelrc: false, | ||
inputSourceMap: options.inputSourceMap, | ||
}); | ||
// TODO: Be more strict about bad return values here. | ||
// Should we throw an error instead? Should we never return `{code: ""}`? | ||
if (!result) return null; | ||
|
||
if (renderer.name === 'astro:jsx') { | ||
const { astro } = result.metadata as unknown as PluginMetadata; | ||
return { | ||
code: result.code || '', | ||
map: result.map, | ||
meta: { | ||
astro, | ||
vite: { | ||
// Setting this vite metadata to `ts` causes Vite to resolve .js | ||
// extensions to .ts files. | ||
lang: 'ts', | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
code: result.code || '', | ||
map: result.map, | ||
}; | ||
} | ||
|
||
interface AstroPluginJSXOptions { | ||
settings: AstroSettings; | ||
logger: Logger; | ||
} | ||
import { transformJSX } from './transform-jsx.js'; | ||
|
||
// Format inspired by https://github.com/vitejs/vite/blob/main/packages/vite/src/node/constants.ts#L54 | ||
const SPECIAL_QUERY_REGEX = new RegExp( | ||
`[?&](?:worker|sharedworker|raw|url|${CONTENT_FLAG}|${PROPAGATED_ASSET_FLAG})\\b` | ||
); | ||
|
||
/** Use Astro config to allow for alternate or multiple JSX renderers (by default Vite will assume React) */ | ||
export default function mdxVitePlugin({ settings }: AstroPluginJSXOptions): Plugin { | ||
let viteConfig: ResolvedConfig; | ||
// A reference to Astro's internal JSX renderer. | ||
let astroJSXRenderer: AstroRenderer; | ||
|
||
// TODO: Move this Vite plugin into `@astrojs/mdx` in Astro 5 | ||
export default function mdxVitePlugin(): Plugin { | ||
return { | ||
name: 'astro:jsx', | ||
enforce: 'pre', // run transforms before other plugins | ||
async configResolved(resolvedConfig) { | ||
viteConfig = resolvedConfig; | ||
astroJSXRenderer = settings.renderers.find((r) => r.jsxImportSource === 'astro')!; | ||
}, | ||
async transform(code, id, opts) { | ||
// Skip special queries and astro entries. We skip astro entries here as we know it doesn't contain | ||
// JSX code, and also because we can't detect the import source to apply JSX transforms. | ||
|
@@ -117,14 +37,7 @@ export default function mdxVitePlugin({ settings }: AstroPluginJSXOptions): Plug | |
}, | ||
}, | ||
}); | ||
return transformJSX({ | ||
code: jsxCode, | ||
id, | ||
renderer: astroJSXRenderer, | ||
mode: viteConfig.mode, | ||
ssr: Boolean(opts?.ssr), | ||
root: settings.config.root, | ||
}); | ||
return await transformJSX(jsxCode, id, opts?.ssr); | ||
}, | ||
}; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This function is moved to
./transform-options.js
without changes. This allows using thejsxTransformOptions
function directly.