Skip to content

Conversation

@sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Apr 3, 2025

Description

A new plugin for react that leverages Oxc built into Rolldown.

How the test works

For each playground with __tests__/oxc directory, vitestGlobalSetup.ts copies the original playground to a separate directory for the oxc plugin (this is similar to what we have with variant playgrounds in Vite core repo, for example playground/css/__tests__/lightningcss). After that vitestGlobalSetup.ts replaces node_modules/vite and node_modules/@vitejs/plugin-react with a symlink that points to rolldown-vite and @vitejs/plugin-react-oxc.

For each playground, vitestGlobalSetup.ts adds _vite-proxy.js that re-exports vite (export * from 'vite'). This is to import vite resolved from each playground directory. In vitestSetup.ts, vite is imported via _vite-proxy.js so that rolldown-vite is used for oxc variant playgrounds.

Making it work with ecosystem-ci

@vitejs/plugin-react-oxc has "vite": "catalog:rolldown-vite" as a dev dependency. This catalog item points to npm:rolldown-vite@^6.3.0-beta.5. This is to allow ecosystem-ci to keep that dep as-is.
Because ecosystem-ci adds an override for vite, it also changes the vite devDep for plugin-react-oxc. To avoid that from happening, in the config for this repo for ecosystem-ci, I added '@vitejs/plugin-react-oxc>vite': 'catalog:rolldown-vite'. This way the devDep will keep pointing the rolldown-vite package.

@sapphi-red
Copy link
Member Author

I'm wondering how to make this work without overrides.

@sapphi-red
Copy link
Member Author

OK. I managed to make the tests working. Now I'm going to check if this works ok with ecosystem-ci.

Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

For the testing, I was thinking about using Vitest workspace to split babel and oxc then setting up alias for each, but I realized we still need to split temp dir to avoid mixing hmr and also @vitejs/plugin-react needs to be aliased for vite config's esbuild to understand. Directly swapping node_modules seems like a reliable approach 👍

One question, it looks like fast-refresh.ts and refreshUtils.js are almost identical to the ones in packages/plugin-react. Should we try to dedupe them? I think copyRefreshUtils.ts script can handle just exact copy and fast-refresh.ts can be also replaced with a weird direct import of ../../plugin-react/src/fast-refresh.

@ArnaudBarre
Copy link
Member

What do you think about using a setup closer to the SWC plugin for fast refresh utils?
It makes the code cleaner and doesn't change a lot (I just checked there was not anything change upstream since 01/2023)

@sapphi-red
Copy link
Member Author

Sure. Let me dedupe and refactor the fast refresh handling in a separate PR first.

@sapphi-red sapphi-red marked this pull request as draft April 7, 2025 02:23
@sapphi-red
Copy link
Member Author

Made a PR for #439 (comment) here: #443

@sapphi-red sapphi-red marked this pull request as ready for review April 7, 2025 10:19
@sapphi-red
Copy link
Member Author

I've merged the main branch. The addRefreshWrapper is a bit complex because there's no sourcemap to modify in oxc plugin, but the overall complexity should be now reduced!

const onlyReactComp = !hasRefresh && reactCompRE.test(code)
if (!hasRefresh && !onlyReactComp) return { code, map }
const newMap =
map === avoidSourceMapOption
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try generated an empty source map that will then only be ';'.repeat(17) + newMap.mappings?
Not a big deal but cleaner when you inspect the pipeline changes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because newMap doesn't exist here.
https://github.com/vitejs/vite-plugin-react/pull/439/files#diff-57c3aa7ebcc7d479870d1da730fee916a15a41215f2a6af821383235f62f3b59R114-R129
We can generate it by new MagicString(code).generateMap({ hires: 'boundary' }), but I guess that has a bigger overhead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does rollup allows to get the current sourceMap of the transform pipeline?
So we can edit it instead of generating a new one and having Vite merge them after

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has a way to get the current combined source map (this.getCombinedSourcemap). But there isn't a way to tell rollup that the returned sourcemap is a combined one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some experiment but using an empty sourceMappings where only ; is apprend breaks the concatenation somewhere because the output doesn't contain any source map (without logs)

So I tried this:

        const { code: newCode, map } = addRefreshWrapper(
          code,
          {
            version: 3,
            file: undefined,
            sources: [''],
            sourcesContent: undefined,
            names: [],
            mappings: 'AAAA;'.repeat(code.split('\n').length - 1),
          },
          '@vitejs/plugin-react-oxc',
          id,
        )
        return { code: newCode, map }

Inspired by what magic-string does as a default and once again was surprised out slow unpredictable JS perf is.

This test also made me find that the ';'.repeat should be 16 and not 17 because there are 17 lines but 16 line breaks.

With the change I have this kind of output.

For Babel I found that's it's a bit broken.

For SWC it map a bit more (onClick) but fails the children is off

I'm still ok for going the full first line inline way, it feels hacky for me but probably only people like me look at generated code, the same way normal people never look at the URL 😅

Copy link
Member Author

@sapphi-red sapphi-red Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'AAAA;'.repeat(code.split('\n').length - 1),

I think this works similarly to hires: false and probably that's the reason the sourcemap is a bit broken when combined with babel.

I prefer to go with the current way for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope the soucemap is perfectly valid, it just says the line didn't change which is true. When combined it works well with the OXC sourcemap
On the babel plugin the sourcemap is not working great but that's nothing to do with 'AAAA;'.repeat(code.split('\n').length - 1) which I added only on the OXC plugin

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope the soucemap is perfectly valid, it just says the line didn't change which is true.

'AAAA;'.repeat(code.split('\n').length - 1) only maps the first character of each lines (which is the same behavior with hires: false). If I remember correctly, the position of console.log will be at the first character of the line rather than the position next to console.log. This is not that important as we have multiple places doing similar to it (it's better if we can avoid them though). But in addition to that, it causes troubles when combined with other sourcemaps.
https://x.com/bluwyoo/status/1674036158714769408 (I didn't find any examples, only found this post 😅)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we want is a range mapping (a proposal exists: https://github.com/tc39/ecma426/blob/main/proposals/range-mappings.md), but currently sourcemaps only supports character-by-character mapping.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks I see and I understand now why people wants a new source map spec 😅
That's sad that such a common usecase is not easy to do 😢

filter: { id: exactRegex(runtimePublicPath) },
handler(_id) {
return readFileSync(
join(_dirname, 'refresh-runtime.js'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note related to this PR because the issue also exist in the babel plugin but this is not working in dev.
I not sure yet what is the best way to handle this (I though about adding src/refresh-runtime.js to gitignore and having a copy step in the dev script, but not 100% convinced)

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'll take a look if I can fix this in a separate PR 👍

@sapphi-red sapphi-red changed the title feat: plugin-react-oxc feat: add plugin-react-oxc Apr 8, 2025
@sapphi-red sapphi-red merged commit 55dec04 into main Apr 8, 2025
11 checks passed
@sapphi-red sapphi-red deleted the feat/plugin-oxc branch April 8, 2025 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants