|
1 | | -import type { EnvironmentOptions, Plugin as VitePlugin } from 'vite'; |
| 1 | +import type { EnvironmentOptions, Plugin as VitePlugin, Rollup } from 'vite'; |
2 | 2 | import type { BuildInternals } from '../internal.js'; |
3 | 3 | import type { StaticBuildOptions } from '../types.js'; |
4 | 4 | import { normalizeEntryId } from './plugin-component-entry.js'; |
5 | 5 | import { ASTRO_VITE_ENVIRONMENT_NAMES } from '../../constants.js'; |
6 | 6 |
|
| 7 | +function getRollupInputAsSet(rollupInput: Rollup.InputOption | undefined): Set<string> { |
| 8 | + if (Array.isArray(rollupInput)) { |
| 9 | + return new Set(rollupInput); |
| 10 | + } else if (typeof rollupInput === 'string') { |
| 11 | + return new Set([rollupInput]); |
| 12 | + } else if (rollupInput && typeof rollupInput === 'object') { |
| 13 | + return new Set(Object.values(rollupInput) as string[]); |
| 14 | + } else { |
| 15 | + return new Set(); |
| 16 | + } |
| 17 | +} |
| 18 | + |
7 | 19 | export function pluginInternals( |
8 | 20 | options: StaticBuildOptions, |
9 | 21 | internals: BuildInternals, |
10 | 22 | ): VitePlugin { |
11 | | - let input: Set<string>; |
12 | | - |
13 | 23 | return { |
14 | 24 | name: '@astro/plugin-build-internals', |
15 | 25 |
|
@@ -42,21 +52,15 @@ export function pluginInternals( |
42 | 52 | } |
43 | 53 | }, |
44 | 54 |
|
45 | | - configResolved(config) { |
46 | | - // Get input from rollupOptions |
47 | | - const rollupInput = config.build?.rollupOptions?.input; |
48 | | - if (Array.isArray(rollupInput)) { |
49 | | - input = new Set(rollupInput); |
50 | | - } else if (typeof rollupInput === 'string') { |
51 | | - input = new Set([rollupInput]); |
52 | | - } else if (rollupInput && typeof rollupInput === 'object') { |
53 | | - input = new Set(Object.values(rollupInput) as string[]); |
54 | | - } else { |
55 | | - input = new Set(); |
56 | | - } |
57 | | - }, |
58 | | - |
59 | 55 | async generateBundle(_options, bundle) { |
| 56 | + // Read the rollup input directly from the current environment's config rather than |
| 57 | + // relying on a closure variable from configResolved. With Vite's per-environment config |
| 58 | + // resolution, a shared closure variable would be overwritten by the last environment's |
| 59 | + // configResolved call, causing inputs from one environment (e.g. SSR) to leak into |
| 60 | + // another (e.g. client). This caused server-only modules like @astrojs/node/server.js |
| 61 | + // to be resolved in the client environment, triggering spurious "externalized for |
| 62 | + // browser compatibility" warnings. |
| 63 | + const input = getRollupInputAsSet(this.environment?.config.build.rollupOptions.input); |
60 | 64 | const promises = []; |
61 | 65 | const mapping = new Map<string, Set<string>>(); |
62 | 66 | const allInput = new Set([...input, ...internals.clientInput]); |
|
0 commit comments