vite-plugin-legacy-swc is a Vite plugin designed to provide robust legacy browser support for production builds by leveraging SWC for efficient JavaScript transpilation. As of its current stable version, 2.0.1, the plugin offers a significantly faster alternative to `@vitejs/plugin-legacy`, with benchmark results indicating a 58% reduction in CPU time and a 4% decrease in JS asset size. It operates by generating corresponding legacy chunks for each bundle chunk, transforming them into SystemJS modules using `@swc/core`, and automatically injecting necessary polyfills and the SystemJS runtime based on specified browser targets and actual code usage. The plugin intelligently injects `<script nomodule>` tags into the generated HTML, ensuring that legacy bundles and polyfills are conditionally loaded only in browsers that require them. The project has a consistent release cadence, with multiple updates throughout 2024 and 2025 addressing bug fixes and introducing new features.
npm install vite-plugin-legacy-swcVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure vite-plugin-legacy-swc in a Vite project to enable legacy browser support for production builds, specifying targets for both modern and legacy bundles.
Migrate all import statements to use ES Modules syntax (e.g., `import legacy from 'vite-plugin-legacy-swc'`).
Review existing deployments or configurations that rely on specific behavior related to file protocols in modern Android webviews.
Ensure `modernTargets` is explicitly configured to reflect desired modern browser support. If `build.target` is also set, ensure it's compatible or understand that `modernTargets` will take precedence for polyfill collection.
Carefully define `targets` to cover the range of legacy browsers needing transpilation, and `modernTargets` to represent the baseline for modern browsers that will receive un-transpiled code but may still need polyfills.
Update your import statements to use ES Modules syntax: `import legacy from 'vite-plugin-legacy-swc'` in your `vite.config.js`.