feat: add IIFE support for main-world content scripts#1102
feat: add IIFE support for main-world content scripts#1102Toumash wants to merge 6 commits intocrxjs:mainfrom
Conversation
🦋 Changeset detectedLatest commit: deea812 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
this is not quite what I'm looking for. ?iife imports code, but registerContentScript expects a string (file path). My usage is like this: import filePath from './content/iife?iife'
const script: chrome.scripting.RegisteredContentScript = {
id: 'abc',
matches: ['<all_urls>'],
js: [filePath],
runAt: 'document_start',
world: 'MAIN',
}
;(async () => {
const existing = await chrome.scripting.getRegisteredContentScripts({
ids: [script.id],
})
if (existing.length) {
await chrome.scripting.updateContentScripts([script])
} else {
await chrome.scripting.registerContentScripts([script])
}
})()You might still keep this PR, for iife in background scripts. And I'll use my work-arounds for now. |
|
Thanks for the clarification! I’ve updated the branch so |
Enable IIFE bundling for main-world content scripts and add output/e2e coverage for dynamic script injection.
Allow ?iife imports to resolve to content script file names for registerContentScripts use, and align fixtures with the new alias.
- Simplify test fixture to demonstrate the primary use case for IIFE imports - Update background.ts to use chrome.scripting.registerContentScripts - Fix IIFE path format (no leading slash) for registerContentScripts compatibility - Remove unused files (options.ts, content1.ts, content2.ts, redirect.html) - Handle document_start timing in main-world.ts (wait for body)
- Detect IIFE script changes in handleHotUpdate using raw file path since IIFE scripts are not in Vite's module graph - Trigger runtime reload after IIFE rebuild completes so Chrome picks up the updated file (Chrome caches content scripts registered via registerContentScripts) - Update E2E test to verify IIFE rebuild on file change: - Use src1/src2 fixture pattern for testing file updates - Verify that page reload after file change shows updated content
7575600 to
d1ec973
Compare
|
Ok, now the main world script should rebuild just fine |
d5e1c9a to
6a391c1
Compare
Vite 6+ tracks plugins internally using WeakMaps, causing errors when reusing plugins in a separate Rollup build. This rewrites bundleIife() to use viteBuild() with direct rollupOptions instead of raw Rollup. Key changes: - Use configFile: false to avoid loading user's config with incompatible settings - Use direct rollupOptions with format: 'iife' instead of lib mode - Copy resolve settings (alias, extensions, conditions) from dev server - Filter out manifest.json and .vite/ assets from output Works with Vite 3, 5, 6, and 7.
6a391c1 to
deea812
Compare
|
hmm i only wanted to have full tests for everything between vite versions |
71a1bee to
deea812
Compare
|
@salmin89 see the demo in the description. Unfortunately tests on windows fail probably because of the same reasons as other e2e were failing before but i dont remember correctly |
|
@copilot resolve the merge conflicts in this pull request |
Summary
Adds support for IIFE content scripts via the
?iifeimport query, enabling main-world script injection usingchrome.scripting.registerContentScriptswithworld: 'MAIN'.Features
?iifequery are bundled as IIFE (Immediately Invoked Function Expression) for injection into the page's main world*?iifeimports inclient.d.tsUsage
Technical Details
/forregisterContentScripts(e.g.,main-world.js)crx:runtime-reload(Chrome caches registered content scripts)Files Changed
packages/vite-plugin/src/node/fileWriter-rxjs.ts- IIFE bundling for dev modepackages/vite-plugin/src/node/plugin-contentScripts.ts- IIFE bundling for buildpackages/vite-plugin/src/node/plugin-contentScripts_dynamic.ts-?iifequery handlingpackages/vite-plugin/src/node/plugin-hmr.ts- IIFE rebuild detection and runtime reloadpackages/vite-plugin/client.d.ts- TypeScript module declarationTesting
Closes #1101
demo:
crxjs_demo_1101.mp4