{"id":22595,"library":"vite-plugin-browserslist-useragent","title":"vite-plugin-browserslist-useragent","description":"A Vite plugin that wraps browserslist-useragent-regexp to compile browserslist queries into RegExp patterns for testing browser user agents. Current stable version is 0.7.0, updated regularly. It automatically generates a virtual module (virtual:supported-browsers) that exposes the compiled regexes, eliminating the need to run CLI commands manually. Key differentiator: seamless integration with Vite's build pipeline, with support for Vite 3 through 8. Ships TypeScript definitions.","status":"active","version":"0.7.0","language":"javascript","source_language":"en","source_url":"https://github.com/kingyue737/vite-plugin-browserslist-useragent","tags":["javascript","browserslist","regexp","vite-plugin","useragent","typescript"],"install":[{"cmd":"npm install vite-plugin-browserslist-useragent","lang":"bash","label":"npm"},{"cmd":"yarn add vite-plugin-browserslist-useragent","lang":"bash","label":"yarn"},{"cmd":"pnpm add vite-plugin-browserslist-useragent","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency required for plugin to work","package":"vite","optional":false}],"imports":[{"note":"Default export; named import will fail. TypeScript: use default import.","wrong":"import { SupportedBrowsers } from 'vite-plugin-browserslist-useragent'","symbol":"SupportedBrowsers","correct":"import SupportedBrowsers from 'vite-plugin-browserslist-useragent'"},{"note":"Exposed via virtual module, not from the plugin package itself.","wrong":"import { browsersRegex } from 'vite-plugin-browserslist-useragent'","symbol":"browsersRegex","correct":"import { browsersRegex } from 'virtual:supported-browsers'"},{"note":"Also exposed via virtual module.","wrong":"import { browsersRegexes } from 'vite-plugin-browserslist-useragent'","symbol":"browsersRegexes","correct":"import { browsersRegexes } from 'virtual:supported-browsers'"},{"note":"CJS: default export via module.exports, not named export.","wrong":"const { SupportedBrowsers } = require('vite-plugin-browserslist-useragent')","symbol":"SupportedBrowsers","correct":"const SupportedBrowsers = require('vite-plugin-browserslist-useragent')"}],"quickstart":{"code":"// vite.config.ts\nimport { defineConfig } from 'vite';\nimport SupportedBrowsers from 'vite-plugin-browserslist-useragent';\n\nexport default defineConfig({\n  plugins: [SupportedBrowsers()],\n});\n\n// In your app code (e.g., main.ts)\nimport { browsersRegex } from 'virtual:supported-browsers';\n\nconst isSupported = browsersRegex.test(navigator.userAgent);\nconsole.log('Browser supported:', isSupported);","lang":"typescript","description":"Shows how to install the plugin, add it to Vite config, and use the virtual module to test browser support via userAgent regex."},"warnings":[{"fix":"Add a module declaration file: declare module 'virtual:supported-browsers' { export const browsersRegex: RegExp; export const browsersRegexes: Array<{family: string; regexString: string; requestVersionsStrings: string[]}>; }","message":"The virtual module 'virtual:supported-browsers' is auto-generated and not a real file. TypeScript may complain about missing module declaration.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Refer to the browserslist-useragent-regexp documentation for valid options. Example: SupportedBrowsers({ browsers: 'last 2 versions', env: 'production' })","message":"Plugin options must be passed as object; omitting options uses defaults. Some options require specific browserslist-useragent-regexp version compatibility.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Create a .browserslistrc file in your project root with your desired browser queries (e.g., 'last 2 versions', 'not dead').","message":"The plugin depends on a browserslist configuration file (.browserslistrc) or a 'browserslist' key in package.json. Without it, no regexes may be generated.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Upgrade to Vite 3+ or stay on an older version of this plugin (<0.7.0) if using Vite 2.","message":"Version 0.7.0 dropped support for Vite 2. Peer dependency now requires Vite ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0.","severity":"breaking","affected_versions":">=0.7.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Create a .d.ts file (e.g., src/vite-env.d.ts) with: declare module 'virtual:supported-browsers' { export const browsersRegex: RegExp; export const browsersRegexes: any[]; }","cause":"TypeScript cannot resolve the virtual module without a declaration file.","error":"Error: Cannot find module 'virtual:supported-browsers' or its corresponding type declarations."},{"fix":"Use import SupportedBrowsers from 'vite-plugin-browserslist-useragent' for the plugin, and import { browsersRegex } from 'virtual:supported-browsers' for the regex.","cause":"Attempting named import from the plugin package instead of the virtual module.","error":"TypeError: Cannot destructure property 'browsersRegex' of '...' as it is undefined."},{"fix":"Create a .browserslistrc file in the project root with your target browsers, e.g.:\nlast 2 versions\nnot dead","cause":"The plugin requires a browserslist configuration to generate regexes.","error":"Error: Browserslist config not found. Create a .browserslistrc file or set browserslist in package.json."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}