Skip to content

Support IIFE bundles for main world content scripts #1101

@Toumash

Description

@Toumash

Problem

Need to inject code into the main world using chrome.scripting.registerContentScripts, but ES modules are not allowed. The current ?script loader uses chrome.runtime.getURL, which cannot run in the main world. Using ?iife fails with Error: IIFE content scripts are not implemented.

Impact

Developers cannot generate IIFE bundles for main-world content scripts via crxjs today.

Current workaround

Create a separate Vite config that builds an IIFE bundle and run another watch build. Example:

export default defineConfig({
  build: {
    emptyOutDir: false,
    rollupOptions: {
      input: path.resolve(__dirname, './content-main.ts'),
      output: {
        format: 'iife',
        dir: path.resolve(__dirname, './dist/assets'),
        entryFileNames: 'content-main.js',
      },
    },
  },
})

Then run an additional build --watch --config for the IIFE bundle. HMR is not available for this IIFE output.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions