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

React element not rendered properly only on production builds #6162

Closed
7 tasks done
FezVrasta opened this issue Dec 17, 2021 · 2 comments
Closed
7 tasks done

React element not rendered properly only on production builds #6162

FezVrasta opened this issue Dec 17, 2021 · 2 comments

Comments

@FezVrasta
Copy link

FezVrasta commented Dec 17, 2021

Describe the bug

I'm having an issue with a React library (that I created), for some reason it works properly on dev mode, but it doesn't on production builds.

I noticed the bug only happens with the default import (that seems to import the Common JS build), but it doesn't happen with any other build target (UMD and ESM)

Reproduction

To reproduce the bug it's enough to bootstrap a new app with npx init vite using the React template.

Install the library with npm install react-resize-aware.

Then, edit the src/App.jsx file to be:

import { useState } from 'react';
import logo from './logo.svg';
import './App.css';
import useResizeAware from 'react-resize-aware';

function App() {
  const [listener] = useResizeAware();

  return (
    <div>
      {listener}
      Test
    </div>
  );
}

export default App;

If you run the app on dev mode and you inspect the resulting DOM, an iframe will be visible.

If you build the app and run it with vite preview, the iframe is replaced by a <t /> element.

System Info

System:
    OS: macOS 12.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 386.64 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
    Yarn: 1.22.15 - ~/.nvm/versions/node/v16.13.0/bin/yarn
    npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm
    Watchman: 2021.12.13.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 96.0.4664.110
    Safari: 15.2
  npmPackages:
    @vitejs/plugin-react: ^1.0.7 => 1.1.3 
    vite: ^2.7.2 => 2.7.3

Used Package Manager

npm

Logs

$ /private/tmp/vite/node_modules/.bin/vite build --debug
  vite:config bundled config file loaded in 42.54ms +0ms
  vite:config using resolved 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:watch-package-data',
  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   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: '/private/tmp/vite/dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     cssTarget: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: { input: '/private/tmp/vite/index.html' },
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  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   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: '/private/tmp/vite/vite.config.js',
  vite:config   configFileDependencies: [ 'vite.config.js' ],
  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: '/private/tmp/vite',
  vite:config   base: '/',
  vite:config   publicDir: '/private/tmp/vite/public',
  vite:config   cacheDir: '/private/tmp/vite/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isProduction: true,
  vite:config   server: { fs: { strict: true, allow: [Array], deny: [Array] } },
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: undefined,
  vite:config     proxy: undefined,
  vite:config     cors: undefined
  vite:config   },
  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   packageCache: Map(0) { set: [Function (anonymous)] },
  vite:config   createResolver: [Function: createResolver]
  vite:config } +2ms
vite v2.7.3 building for production...
✓ 35 modules transformed.
dist/assets/favicon.17e50649.svg   1.49 KiB
dist/assets/logo.ecc203fb.svg      2.61 KiB
dist/index.html                    0.52 KiB
dist/assets/index.cfbfdb95.js      0.91 KiB / gzip: 0.52 KiB
dist/assets/index.cd9c0392.css     0.75 KiB / gzip: 0.48 KiB
dist/assets/vendor.2b1a1f45.js     130.18 KiB / gzip: 42.06 KiB
✨  Done in 0.84s.
                 

Validations

@toSayNothing
Copy link
Contributor

i have test some cases , and found that u can add 'module' field in 'react-resize-aware' package.json, then whether dev or prod mode would be ok. like this:
image


the reason is: if u don't specified the 'module' field , whether in dev/prod mode, vite (esbuild/rollup) will resolve the 'react-resize-aware' package by the 'main' fileld, which is dist/index.js , and your package was pack by microbundle, in microbundle docs, the 'main' field is for CJS , and u should include the 'module' field according the docs https://github.com/developit/microbundle
image

@aleclarson
Copy link
Member

Don't minify your React libraries before publishing them. It breaks the reverse JSX compilation done by @vitejs/plugin-react. See #5441

You can pass jsxRuntime: "classic" to plugin-react to avoid the issue.

@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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants