summaryrefslogtreecommitdiff
path: root/packages/astro/src/vite-plugin-scripts/index.ts
blob: 20f4fdafec46e35dc38e221e8e58c7a231724f70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Plugin as VitePlugin } from 'vite';
import { AstroConfig, InjectedScriptStage } from '../@types/astro.js';

// NOTE: We can't use the virtual "\0" ID convention because we need to
// inject these as ESM imports into actual code, where they would not
// resolve correctly.
const SCRIPT_ID_PREFIX = `astro:scripts/`;
export const BEFORE_HYDRATION_SCRIPT_ID = `${SCRIPT_ID_PREFIX}${
	'before-hydration' as InjectedScriptStage
}.js`;
export const PAGE_SCRIPT_ID = `${SCRIPT_ID_PREFIX}${'page' as InjectedScriptStage}.js`;
export const PAGE_SSR_SCRIPT_ID = `${SCRIPT_ID_PREFIX}${'page-ssr' as InjectedScriptStage}.js`;

export default function astroScriptsPlugin({ config }: { config: AstroConfig }): VitePlugin {
	return {
		name: 'astro:scripts',
		async resolveId(id) {
			if (id.startsWith(SCRIPT_ID_PREFIX)) {
				return id;
			}
			return undefined;
		},

		async load(id) {
			if (id === BEFORE_HYDRATION_SCRIPT_ID) {
				return config._ctx.scripts
					.filter((s) => s.stage === 'before-hydration')
					.map((s) => s.content)
					.join('\n');
			}
			if (id === PAGE_SCRIPT_ID) {
				return config._ctx.scripts
					.filter((s) => s.stage === 'page')
					.map((s) => s.content)
					.join('\n');
			}
			if (id === PAGE_SSR_SCRIPT_ID) {
				return config._ctx.scripts
					.filter((s) => s.stage === 'page-ssr')
					.map((s) => s.content)
					.join('\n');
			}
			return null;
		},
		buildStart(options) {
			// We only want to inject this script if we are building
			// for the frontend AND some hydrated components exist in
			// the final build. We can detect this by looking for a
			// `astro/client/*` input, which signifies both conditions are met.
			const hasHydratedComponents =
				Array.isArray(options.input) &&
				options.input.some((input) => input.startsWith('astro/client'));
			const hasHydrationScripts = config._ctx.scripts.some((s) => s.stage === 'before-hydration');
			if (hasHydratedComponents && hasHydrationScripts) {
				this.emitFile({
					type: 'chunk',
					id: BEFORE_HYDRATION_SCRIPT_ID,
					name: BEFORE_HYDRATION_SCRIPT_ID,
				});
			}
		},
	};
}