| 147 | new WeakSet<Rollup.NormalizedOutputOptions>() |
| 148 | |
| 149 | function viteLegacyPlugin(options: Options = {}): Plugin[] { |
| 150 | let config: ResolvedConfig |
| 151 | let targets: Options['targets'] |
| 152 | const modernTargets: Options['modernTargets'] = |
| 153 | options.modernTargets || modernTargetsBabel |
| 154 | |
| 155 | const genLegacy = options.renderLegacyChunks !== false |
| 156 | const genModern = options.renderModernChunks !== false |
| 157 | if (!genLegacy && !genModern) { |
| 158 | throw new Error( |
| 159 | '`renderLegacyChunks` and `renderModernChunks` cannot be both false', |
| 160 | ) |
| 161 | } |
| 162 | |
| 163 | const debugFlags = (process.env.DEBUG || '').split(',') |
| 164 | const isDebug = |
| 165 | debugFlags.includes('vite:*') || debugFlags.includes('vite:legacy') |
| 166 | |
| 167 | const assumptions = options.assumptions || {} |
| 168 | |
| 169 | const facadeToLegacyChunkMap = new Map() |
| 170 | const facadeToLegacyImportMap = new Map<string | null, Rollup.OutputAsset>() |
| 171 | const facadeToLegacyPolyfillMap = new Map() |
| 172 | const facadeToModernPolyfillMap = new Map() |
| 173 | const modernPolyfills = new Set<string>() |
| 174 | const legacyPolyfills = new Set<string>() |
| 175 | // When discovering polyfills in `renderChunk`, the hook may be non-deterministic, so we group the |
| 176 | // modern and legacy polyfills in a sorted chunks map for each rendered outputs before merging them. |
| 177 | const outputToChunkFileNameToPolyfills = new WeakMap< |
| 178 | Rollup.NormalizedOutputOptions, |
| 179 | Map<string, { modern: Set<string>; legacy: Set<string> }> | null |
| 180 | >() |
| 181 | |
| 182 | if (Array.isArray(options.modernPolyfills) && genModern) { |
| 183 | options.modernPolyfills.forEach((i) => { |
| 184 | modernPolyfills.add( |
| 185 | i.includes('/') ? `core-js/${i}` : `core-js/modules/${i}.js`, |
| 186 | ) |
| 187 | }) |
| 188 | } |
| 189 | if (Array.isArray(options.additionalModernPolyfills)) { |
| 190 | options.additionalModernPolyfills.forEach((i) => { |
| 191 | modernPolyfills.add(i) |
| 192 | }) |
| 193 | } |
| 194 | if (Array.isArray(options.polyfills)) { |
| 195 | options.polyfills.forEach((i) => { |
| 196 | if (i.startsWith(`regenerator`)) { |
| 197 | legacyPolyfills.add(`regenerator-runtime/runtime.js`) |
| 198 | } else { |
| 199 | legacyPolyfills.add( |
| 200 | i.includes('/') ? `core-js/${i}` : `core-js/modules/${i}.js`, |
| 201 | ) |
| 202 | } |
| 203 | }) |
| 204 | } |
| 205 | if (Array.isArray(options.additionalLegacyPolyfills)) { |
| 206 | options.additionalLegacyPolyfills.forEach((i) => { |