{"record":{"id":"134b22b3698f1efd","repo":"vitejs/vite","slug":"vitejs-plugin-legacy-does-not-support-library-mod","errorCode":null,"errorMessage":"@vitejs/plugin-legacy does not support library mode.","messagePattern":"@vitejs/plugin-legacy does not support library mode\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/plugin-legacy/src/index.ts","lineNumber":462,"sourceCode":"          'es2015',\n        )\n      }\n    },\n  }\n\n  const legacyPostPlugin: Plugin = {\n    name: 'vite:legacy-post-process',\n    enforce: 'post',\n    apply: 'build',\n\n    renderStart(opts) {\n      // Empty the nested map for this output\n      outputToChunkFileNameToPolyfills.set(opts, null)\n    },\n\n    configResolved(_config) {\n      if (_config.build.lib) {\n        throw new Error('@vitejs/plugin-legacy does not support library mode.')\n      }\n      config = _config\n\n      const viteVersion = this.meta.viteVersion\n      supportsLegacyOxcMinification =\n        !!viteVersion &&\n        isVersionGte(viteVersion, legacyOxcMinificationSupportedVersion)\n\n      if (!supportsLegacyOxcMinification && config.build.minify === 'oxc') {\n        config.logger.warn(\n          colors.yellow(\n            `'oxc' minifier is not supported for legacy chunks by Vite version ${viteVersion}. ` +\n              `Please upgrade to Vite version ${legacyOxcMinificationSupportedVersion} or later.`,\n          ),\n        )\n      }\n\n      if (isDebug) {","sourceCodeStart":444,"sourceCodeEnd":480,"githubUrl":"https://github.com/vitejs/vite/blob/b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab/packages/plugin-legacy/src/index.ts#L444-L480","documentation":"plugin-legacy's design is built around HTML entry points: it injects `<script nomodule>`/SystemJS tags and polyfill chunks via `transformIndexHtml`, splitting a build into modern + legacy bundles. Library mode (`build.lib`) has no HTML entry and produces a single consumable package, so the legacy HTML injection pipeline does not apply. The plugin hard-rejects library mode in `configResolved` of the `vite:legacy-post-process` plugin.","triggerScenarios":"Having both `build.lib` set in vite.config AND `@vitejs/plugin-legacy` present in `plugins`, then running `vite build`. The throw fires during config resolution before any bundling starts.","commonSituations":"Reusing a single vite.config that was authored for an app (with `legacy()`) to also build a library entry. Adding `legacy()` to a monorepo package config that is in library mode. Forgetting that a shared config sets `build.lib`.","solutions":["Remove `legacy()` from the plugins array for any build that uses `build.lib`.","Split your config: use a function/config that adds `legacy()` only when building the app (HTML) target, and omits it for the library target.","Use an environment variable or mode check to conditionally include the legacy plugin: `plugins: [...(isLib ? [] : [legacy()])]`."],"exampleFix":"// before\nexport default defineConfig({\n  build: { lib: { entry: 'src/index.ts' } },\n  plugins: [legacy()],\n})\n// after\nexport default defineConfig(({ mode }) => ({\n  build: { lib: { entry: 'src/index.ts' } },\n  plugins: [/* legacy() removed - not supported in lib mode */],\n}))","handlingStrategy":"validation","validationCode":"import type { InlineConfig } from 'vite'\nfunction assertNoLegacyInLibMode(cfg: InlineConfig) {\n  const hasLib = !!(cfg.build && (cfg.build as any).lib)\n  const hasLegacy = (cfg.plugins || []).flat().some((p: any) => p && p.name && /legacy/i.test(p.name))\n  if (hasLib && hasLegacy) {\n    throw new Error('@vitejs/plugin-legacy cannot be used with build.lib - remove the plugin for lib builds')\n  }\n}","typeGuard":"function isAppBuild(cfg: { build?: { lib?: unknown } }): boolean {\n  return !cfg?.build?.lib\n}","tryCatchPattern":null,"preventionTips":["Keep separate configs for app vs library builds.","Conditionally add legacy(): plugins: [...(mode === 'lib' ? [] : [legacy()])]."],"tags":["plugin-legacy","library-mode","build","config-validation"],"backgroundTag":null,"analyzedSha":"b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab","analyzedAt":"2026-08-11T11:49:19.515Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}