最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - Chrome Extension Code Splitting with React.lazy + Vite - Chunk Loading Fails Despite Background Script Exclusion Body:

programmeradmin2浏览0评论

I’m building a Chrome extension using React 18 and Vite 5, trying to implement code splitting for optimization. Despite excluding background scripts from splitting, I’m getting chunk loading errors. Problem Context: • Using React.lazy for component-level code splitting • Configured Vite to exclude background.js from splitting • Components load fine in regular React app, but fail in extension

vite.config.ts :

export default defineConfig({
  build: {
    rollupOptions: {
      input: {
        main: path.resolve(__dirname, 'index.html'),
        background: path.resolve(__dirname, 'src/background.ts')
      },
      output: {
        entryFileNames: '[name].js',
        chunkFileNames: '[name].js',
        assetFileNames: '[name].[ext]'
        manualChunks: (id) => {
          if (id.includes('background.js')) return undefined;
          return 'vendor';
        }
      }
    }
  }
});

popup component looks like this :

const LazyComponent = React.lazy(() => import('./PopupSection'));

function Popup() {
  return (
    <Suspense fallback={<div>Loading...</div>}>
      <LazyComponent />
    </Suspense>
  );
}

What I’ve Tried so far is to : 1. Excluded background script from manualChunks 2. Verified manifest includes all chunk paths in web_accessible_resources 3. Tested with dynamic imports without React.lazy - same issue 4. Regular React app works with same config, extension fails Question: Why does code splitting fail in the Chrome extension environment despite excluding background scripts from splitting? How can I properly implement component-level lazy loading while maintaining extension functionality? Are there special considerations for Vite’s build process in extensions?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论