Skip to content

Feat/1101 iife content scripts#1158

Open
salmin89 wants to merge 4 commits intocrxjs:mainfrom
salmin89:feat/1101--iife-content-scripts
Open

Feat/1101 iife content scripts#1158
salmin89 wants to merge 4 commits intocrxjs:mainfrom
salmin89:feat/1101--iife-content-scripts

Conversation

@salmin89
Copy link
Copy Markdown
Contributor

@salmin89 salmin89 commented Apr 24, 2026

Hey guys!

Full disclosure:

I used Agents to heavily assist with this PR. This feature is something we've needed at my company for a while, but I never had enough time to learn CrxJS enough to be able to it without help from AI. My strategy was to write tests to make sure we didn't break any existing functionality.

This feature would solve a number of open issues (some of which would need to accept the iife lifestyle)
#1102
#1101
#1152
#1119

The following below is what the AI generated to describe this PR.


feat: Add IIFE content script bundling

Summary

This PR adds support for bundling content scripts as self-contained IIFE (Immediately Invoked Function Expression) files. Content scripts named with the .iife.ts extension are automatically detected and built as single files with all dependencies inlined.

Motivation

When injecting content scripts into the MAIN world using chrome.scripting.executeScript or chrome.scripting.registerContentScripts, ES modules are not supported. This requires content scripts to be bundled as IIFE files with no external imports.

Previously, developers had to manually configure separate build steps or use workarounds. This PR adds first-class support for IIFE content scripts with zero configuration.

Usage

1. Manifest-declared content scripts

Name your content script with .iife.ts extension:

src/content/injected.iife.ts

import { someHelper } from './utils'

console.log('Running in MAIN world!', someHelper())

manifest.config.ts

export default defineManifest({
  content_scripts: [
    {
      js: ['src/content/injected.iife.ts'],
      matches: ['https://*/*'],
      world: 'MAIN',
    },
  ],
})

2. Dynamic content scripts (chrome.scripting API)

src/background.ts

import scriptPath from './content/injected.iife.ts?script'

chrome.action.onClicked.addListener((tab) => {
  chrome.scripting.executeScript({
    target: { tabId: tab.id },
    world: 'MAIN',
    files: [scriptPath],
  })
})

Output

Regular content script (main.ts):

  • Uses loader pattern with code-splitting
  • Output: assets/main.ts-loader-xxx.js + chunks

IIFE content script (main.iife.ts):

  • Single self-contained file with all imports inlined
  • Output: src/content/main.iife.js
// Built output of .iife.ts file
(function(){"use strict";function helper(){return"inlined"}console.log("Running!",helper())})();

Benefits

  • Zero configuration - Just name your file .iife.ts
  • All imports inlined - No external dependencies or dynamic imports
  • MAIN world compatible - Works with chrome.scripting.executeScript
  • Works with ?script imports - Auto-detected for dynamic content scripts

Trade-offs

  • No code sharing between IIFE content scripts (each is fully self-contained)
  • Separate build step per IIFE script (slightly longer build time)

Testing

  • Added unit tests for isIifeContentScript filename detection (10 tests)
  • Added E2E tests for build output verification
  • Added E2E tests for dev server compatibility

Add support for bundling content scripts as self-contained IIFE files.
Content scripts named with .iife.ts extension are automatically detected
and built as single files with all dependencies inlined.

This is useful for:
- MAIN world content scripts (chrome.scripting.executeScript)
- Avoiding loader overhead
- Single-file distribution

The IIFE plugin uses Vite's library mode to build each .iife.ts file
separately, producing a single bundled output per content script.
- Update manifest plugin to skip emitting .iife.ts files (handled by IIFE plugin)
- Update content scripts plugin to skip IIFE type processing
- Update dynamic content scripts plugin to auto-detect .iife.ts files
- Add enforce: 'post' to dynamic scripts build hook for correct ordering
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 24, 2026

🦋 Changeset detected

Latest commit: 4f5d2ec

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@crxjs/vite-plugin Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@salmin89 salmin89 force-pushed the feat/1101--iife-content-scripts branch 2 times, most recently from c9aa0b8 to 319d4ab Compare April 24, 2026 01:54
- Add unit tests for isIifeContentScript filename detection
- Add E2E build test verifying IIFE output format and inlined imports
- Add E2E serve test verifying .iife.ts files work in dev mode
- Test both manifest-declared and dynamic (?script) content scripts
@salmin89 salmin89 force-pushed the feat/1101--iife-content-scripts branch from 319d4ab to 0f729b4 Compare April 24, 2026 01:58
@Toumash
Copy link
Copy Markdown
Member

Toumash commented Apr 24, 2026

hm whats the difference #1101 / #1102 other than .iife.js vs ?iife? @salmin89

@salmin89
Copy link
Copy Markdown
Contributor Author

I'd love to see what @jacksteamdev thinks about this solution

@salmin89
Copy link
Copy Markdown
Contributor Author

salmin89 commented Apr 24, 2026

hm whats the difference #1101 / #1102 other than .iife.js vs ?iife? @salmin89

Initially when I tested 1102 it didn't do what I needed it to do. I see that you ended up updating the PR and got it working (I missed the update).

I would say that this PR does the same thing as #1102 does, but also adds support for IIFE in the manifest (by naming .iife).


I just added a comment to your PR while testing it out:
#1102 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants