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

[plugin-react] Reverse compilation of "React.createElement" produce incorrect results #5441

Closed
7 tasks done
zengguirong opened this issue Oct 27, 2021 · 4 comments
Closed
7 tasks done

Comments

@zengguirong
Copy link

zengguirong commented Oct 27, 2021

Describe the bug

According to the source code,
the React plugin will compile the code in node_modules that contains React.createElement(Such as React.createElement(AnyComponent)) to JSX tag(Such as <AnyComponent>) when the plugin option jsxRuntime was set to automatic(It is also the default value).
Then the JSX tag above will be compile again to the jsx function call(Such as /* @__PURE__ */ jsx(AnyComponent)) through Babel.

However, in some third-party libraries, the component naming does not follow the "Pascal Case" specification(Such as finalComponent at this link). In this case, the JSX component name will be treated as a native HTML element by Babel, that is, compiled into a string(jsx('finalComponent') instead of jsx(finalComponent)).

Therefore, I hope Vite provide configuration options to skip this behavior in my
specified libraries, or change the component name to "Pascal Case" before passing it to Babel.

Reproduction

Repo: https://github.com/zengguirong/vite-react-report
Library source: https://github.com/alibaba/formily/blob/v2.0.0-rc.17/packages/react/src/components/ReactiveField.tsx#L48
Vite build file: https://github.com/zengguirong/vite-react-report/blob/main/dist/index.js#L9212

System Info

System:
    OS: macOS 11.5.2
    CPU: (12) x64 Intel(R) Core(TM) i7-8700B CPU @ 3.20GHz
    Memory: 2.50 GB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.18.1 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 6.14.15 - /usr/local/bin/npm
  Browsers:
    Chrome: 95.0.4638.54
    Edge: 94.0.992.50
    Firefox: 92.0
    Safari: 14.1.2
  npmPackages:
    @vitejs/plugin-react: ^1.0.0 => 1.0.6 
    vite: 2.6.12 => 2.6.12

Used Package Manager

yarn

Logs

$ tsc && vite build --debug
  vite:config bundled config file loaded in 89.61ms +0ms
  vite:config using resolved config: {
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: false,
  vite:config     cssTarget: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: { external: [Array] },
  vite:config     minify: false,
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: {
  vite:config       entry: './src/main.tsx',
  vite:config       formats: [Array],
  vite:config       fileName: [Function: fileName]
  vite:config     },
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] }
  vite:config   },
  vite:config   plugins: [
  vite:config     'alias',
  vite:config     'vite:react-babel',
  vite:config     'vite:react-refresh',
  vite:config     'vite:react-jsx',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-script-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   resolve: { dedupe: [ 'react', 'react-dom' ], alias: [ [Object], [Object] ] },
  vite:config   optimizeDeps: {
  vite:config     include: [ 'react/jsx-dev-runtime' ],
  vite:config     esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
  vite:config   },
  vite:config   configFile: '/Users/zengguirong/code/vite-react-report/vite.config.ts',
  vite:config   configFileDependencies: [ 'vite.config.ts' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   root: '/Users/zengguirong/code/vite-react-report',
  vite:config   base: '/',
  vite:config   publicDir: '/Users/zengguirong/code/vite-react-report/public',
  vite:config   cacheDir: '/Users/zengguirong/code/vite-react-report/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isProduction: true,
  vite:config   server: { fs: { strict: undefined, allow: [Array] } },
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   createResolver: [Function: createResolver]
  vite:config } +4ms
vite v2.6.12 building for production...

Validations

@zengguirong zengguirong changed the title [plugin-react] Reverse Compilation of "React.createElement" produce incorrect results [plugin-react] Reverse compilation of "React.createElement" produce incorrect results Oct 28, 2021
@lblblong
Copy link

i have encountered the same problem when using Formily, and the input components cannot be displayed after build

@StarHosea
Copy link

same problem

@aleclarson
Copy link
Member

Fixed in #6110

Patch release coming soon

@aleclarson
Copy link
Member

@vitejs/plugin-react@1.1.4 should fix this

LMK if it doesn't

@github-actions github-actions bot locked and limited conversation to collaborators Jan 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants