question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

vite react plugin bundles element tagnames as <tagname> in production mode

See original GitHub issue

Describe the bug

When bundling an app with React + Vite in production mode, Vite bundles all components with the tag name <tagname>. I would expect that the tag names be preserved when building for production.

The sample app has the following code:

React.createElement(tagName, {}, children)

It looks like tagName is being converted to "tagName" (the string) when building. If I change that tagName variable to helloworld, then the elements would show up as <helloworld> instead.

Reproduction

https://github.com/liamdebeasi/vite-production-demo

Steps to reproduce

  1. Run npm install.
  2. In the test-module directory, run npm pack.
  3. In the root directory, run npm install ./test-module/test-module-0.0.1.tgz.
  4. Run npm run dev.
  5. Open page and Dev Tools in the browser.
  6. Inspect the “My Badge” element. Observe that the element tag name is <app-badge>.
  7. Run npm run build.
  8. Open the built index.html file in a browser. If you have Python 3, you can do cd dist && python -m http.server and navigate localhost at the port specified.
  9. Observe that the badge element is output as <tagname>.

Other information:

  • This app uses the @vitejs/plugin-react plugin.
  • Removing the useRef import in test-module/index.esm.js seems to fix the issue, but I can’t quite figure out why.
  • This issue does not happen with React + Webpack.
  • Packing + installing test-module as a dependency is required to reproduce the issue. Importing from ./test-module instead of packing + installing does not reproduce the issue.

System Info

macOS 11.6.1
node 16.11.1
npm 8.0.0
vite 2.7.2

Used Package Manager

npm

Logs

vite:config bundled config file loaded in 131.34ms +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: '/Users/liamdebeasi/Ionic/vite-production-demo/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: {
  vite:config       input: '/Users/liamdebeasi/Ionic/vite-production-demo/index.html'
  vite:config     },
  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: '/Users/liamdebeasi/Ionic/vite-production-demo/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/liamdebeasi/Ionic/vite-production-demo',
  vite:config   base: '/',
  vite:config   publicDir: '/Users/liamdebeasi/Ionic/vite-production-demo/public',
  vite:config   cacheDir: '/Users/liamdebeasi/Ionic/vite-production-demo/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 } +6ms

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
elylucascommented, Dec 21, 2021

@liamdebeasi Using createElement named export from react instead of React.createElement also seems to fix the problem in the repo app. I tested this in Ionic v6 by hand modifying node_modules/@ionic/react/dist/index.esm.js and it got the app to render properly in a prod build.

0reactions
sapphi-redcommented, Apr 15, 2022

I was able to reproduce with vite v2.7.2 + plugin-react v1.1.3. But after updating to vite v2.7.2 + plugin-react v1.3.1, it worked well. Closing as it seeems to be fixed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Features | Vite
Vite will detect such bare module imports in all served source files and perform the following: Pre-bundle them to improve page loading speed...
Read more >
How can I suppress "The tag <some-tag> is unrecognized in ...
I found a potential fix for this issue - if you are using a plugin (and potentially in other circumstances) you can use...
Read more >
Tags and their synonyms - Stack Exchange Data Explorer
SourceTagName, ',') as synonyms, t.tagName, e.body as 'Excerpt', w.body as 'WikiBody' from tags t left join Posts e on t.ExcerptPostId = e.
Read more >
html-loader - webpack
Then add the plugin to your webpack config. For example: file.js import html from ". ... If the tag name is not specified...
Read more >
react-markdown - Bountysource
Vite and React-Markdown don't seem to play well when building in production mode. (vite build) Note: I'm using the react template of vite....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found