Skip to content

[plugin-react] Plugin does not convert React.createElement to JSX Runtime when name is an expression  #15

@GlacialBoy

Description

@GlacialBoy

Describe the bug

I'm bundling a library from one repo (ES module) with Vite and importing it in another repo handled by Vite.
We have several external dependencies in this project.

In the bundled library code there are pieces of code where the React.createElement have component name in variables with lower case name and expressions, like:

React.createElement(render4.component, __spreadProps(__spreadValues({}, render4.props))
React.createElement(this.props.pointer, this.props)
React.createElement(_default$1, {})

When I used the library executing vite build those expressions are not converted to _jsx, launching an error in production, "React is undefined"

After an investigation I noticed the 3 cases above are not recognised as valid “React.createElement” snippet because the check at this line fails:

https://github.com/vitejs/vite/blob/274c10eb9e9451620981da808e143bc50b8c2a38/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts#L157

When this check fails the snippet is not replaced in the original code.

Another related issue is that when we have some React aliases and the aliases are used to execute createElement (for example rollup define “React__default” when in the same file there are multiple name conflicts ) the lines: 


https://github.com/vitejs/vite/blob/274c10eb9e9451620981da808e143bc50b8c2a38/packages/plugin-react/src/jsx-runtime/restore-jsx.ts#L33-L36

Is going to replace the alias with React, 
If then the check of a valid React.createElement fails (issue above) then the plugin is not going to replace back the alias, adding in the chunk code a reference to a variable (React) that doesn’t exists.



So the issues:

  • There are limitations in the way that the Plugin can recognise the React.createElement snippet and that can be problematic in project with many 3rd party dependencies
  • When React has aliases and they are used with “createElement” if the plugin does not recognise the “React.createElement” snippet then the aliases are not replaced back


Thanks for your great work! 🎉

Reproduction

https://github.com/GlacialBoy/vite-createElement-error

System Info

System:
    OS: macOS 11.3.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 638.48 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.2 - ~/.nvm/versions/node/v16.13.2/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.2/bin/npm
    Watchman: 2021.10.11.00 - /usr/local/bin/watchman
  Browsers:
    Chrome: 100.0.4896.127
    Firefox: 99.0.1
    Safari: 14.1
  npmPackages:
    @vitejs/plugin-react: ^1.3.0 => 1.3.1 
    vite: ^2.9.6 => 2.9.6

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions