bitovi / react-to-web-component

Convert react components to native Web Components. Works with Preact too!
https://www.bitovi.com/open-source/react-to-web-component
MIT License
644 stars 42 forks source link

How to exclude React packages from production build? #178

Open Irinova opened 1 month ago

Irinova commented 1 month ago

I need only native web-components without react libs in build file. Is it possible to do that? My Vite config:

import { defineConfig } from 'vite'

export default defineConfig({
  build: {
    // generate .vite/manifest.json in outDir
    manifest: true,
    rollupOptions: {
      // overwrite default .html entry
      input: '/src/index.jsx',
    },
  },
})

index.jsx:


import ReactDOM from "react-dom/client"
import r2wc from "react-to-webcomponent"
import 'vite/modulepreload-polyfill'

const Greeting = () => {
 return <h1>Hello, World!</h1>
}

const WebGreeting = r2wc(Greeting, React, ReactDOM)

customElements.define("web-greeting", WebGreeting)
leweyse commented 3 weeks ago

Rollup has the option external to exclude files or dependencies from the bundle: https://rollupjs.org/configuration-options/#external

You can use something like this:

    rollupOptions: {
      external: ['react'],
      output: {
        globals: {
          react: 'React',
        },
      },
    },