Skip to content

ci: test plugin across Vite 3, 6, 7, and 8-beta versions with IIFE support#1105

Open
Toumash wants to merge 15 commits into
crxjs:mainfrom
Toumash:ci/vite-version-matrix
Open

ci: test plugin across Vite 3, 6, 7, and 8-beta versions with IIFE support#1105
Toumash wants to merge 15 commits into
crxjs:mainfrom
Toumash:ci/vite-version-matrix

Conversation

@Toumash
Copy link
Copy Markdown
Member

@Toumash Toumash commented Jan 17, 2026

Summary

  • Add CI matrix to test the vite-plugin across multiple Vite versions (3, 6, 7, 8-beta)
  • Add IIFE content script support for main-world scripts using ?iife query parameter
  • Fix Vite 6+ compatibility issues in IIFE bundling and manifest plugin

Changes

CI Improvements

  • Test matrix runs Unit/Output and E2E tests across Vite 3, 6, 7, and 8.0.0-beta.7
  • Runtime Vite version logging for verification
  • Both Ubuntu and Windows runners

IIFE Content Scripts Feature

  • New ?iife query parameter for content scripts that need main-world execution
  • Uses chrome.scripting.registerContentScripts() with world: "MAIN"
  • Bundles scripts as self-executing IIFE format
  • Supports HMR rebuilds during development

Vite 6+ Compatibility Fixes

  • Use Vite build API instead of raw Rollup for IIFE bundling (fixes WeakMap key errors)
  • Add names, originalFileNames, needsCodeReference properties to emitted assets (fixes manifest plugin crashes)
  • Update type definitions for Rollup 4+ compatibility

Testing

All tests pass locally on Vite 3, 6, 7, and 8-beta. CI runs the full test suite across all versions.

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
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.
Merged vite-compat tests into the main test suite under tests/compat/
and updated the CI workflow to run all unit, output, and e2e tests
with a Vite version matrix.

This ensures the plugin is properly tested against all supported Vite
versions as declared in peerDependencies.

Changes:
- Move tests/vite-compat/* to packages/vite-plugin/tests/compat/
- Add vite-version matrix to unit-output and e2e jobs
- Remove separate compat job (now redundant)
- Update test imports to use 'src' alias
- Add explicit name property to CrxPlugin interface for Vite 7+
- Use intersection type for ResolvedConfigWithHMRToken
- Add isCrxPlugin type guard for cross-version plugin filtering
- Improve IIFE bundling with proper Vite build API usage
- Add test:run:compat script for version-specific testing
- Update CI workflow to test with matrix of Vite versions
Pass entryFileNames, chunkFileNames, and assetFileNames from the parent
config to viteBuild() so IIFE-bundled scripts follow the same naming
convention as the rest of the build output.
The Vite 6+ manifest plugin expects assets to have 'names' (plural array),
'originalFileNames', and 'needsCodeReference' properties. Assets created
via emitAssetToBundle() were missing these, causing the manifest plugin
to crash with 'Cannot read properties of undefined (reading length)'.

This adds the required properties to emitAssetToBundle() output, making
IIFE content script bundling work correctly with Vite 6, 7, and 8-beta.
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jan 17, 2026

🦋 Changeset detected

Latest commit: 569ab1d

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

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

💥 An error occurred when fetching the changed packages and changesets in this PR
Some errors occurred when validating the changesets config:
The package or glob expression "@crxjs/vite-compat-test" is specified in the `ignore` option but it is not found in the project. You may have misspelled the package name or provided an invalid glob expression. Note that glob expressions must be defined according to https://www.npmjs.com/package/micromatch.

Fix 'Unknown input options: platform' error by only passing valid Rollup
input options instead of spreading all rollupOptions from Vite config.
@Toumash Toumash force-pushed the ci/vite-version-matrix branch from cd06766 to 569ab1d Compare January 17, 2026 18:16
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

Thanks for contributing to CRXJS! This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs within 7 days.

@github-actions github-actions Bot added the Stale label May 7, 2026
@Toumash Toumash removed the Stale label May 8, 2026
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