summaryrefslogtreecommitdiff
path: root/packages/integrations/vue/src/index.ts
blob: ef35055877d7af056dc0a38aa5aa4c3857e6fd9d (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import type { Options as VueOptions } from '@vitejs/plugin-vue';
import type { Options as VueJsxOptions } from '@vitejs/plugin-vue-jsx';
import type { AstroIntegration, AstroIntegrationLogger, AstroRenderer } from 'astro';
import type { UserConfig, Plugin } from 'vite';

import { fileURLToPath } from 'node:url';
import vue from '@vitejs/plugin-vue';

interface Options extends VueOptions {
	jsx?: boolean | VueJsxOptions;
	appEntrypoint?: string;
}

interface ViteOptions extends Options {
	root: URL;
	logger: AstroIntegrationLogger;
}

function getRenderer(): AstroRenderer {
	return {
		name: '@astrojs/vue',
		clientEntrypoint: '@astrojs/vue/client.js',
		serverEntrypoint: '@astrojs/vue/server.js',
	};
}

function getJsxRenderer(): AstroRenderer {
	return {
		name: '@astrojs/vue (jsx)',
		clientEntrypoint: '@astrojs/vue/client.js',
		serverEntrypoint: '@astrojs/vue/server.js',
		jsxImportSource: 'vue',
		jsxTransformOptions: async () => {
			const jsxPlugin = (await import('@vue/babel-plugin-jsx')).default;
			return {
				plugins: [jsxPlugin],
			};
		},
	};
}

function virtualAppEntrypoint(options: ViteOptions) {
	const virtualModuleId = 'virtual:@astrojs/vue/app';
	const resolvedVirtualModuleId = '\0' + virtualModuleId;
	let getExports: (id: string) => Promise<string[]>;
	return {
		name: '@astrojs/vue/virtual-app',
		buildStart() {
			if (!getExports) {
				getExports = async (id: string) => {
					const info = await this.load.call(this, { id });
					return info.exports ?? [];
				};
			}
		},
		configureServer(server) {
			if (!getExports) {
				getExports = async (id: string) => {
					const mod = await server.ssrLoadModule(id);
					return Object.keys(mod) ?? [];
				};
			}
		},
		resolveId(id: string) {
			if (id == virtualModuleId) {
				return resolvedVirtualModuleId;
			}
		},
		async load(id: string) {
			const noop = `export const setup = (app) => app;`;
			if (id === resolvedVirtualModuleId) {
				if (options.appEntrypoint) {
					try {
						let resolved;
						if (options.appEntrypoint.startsWith('.')) {
							resolved = await this.resolve(
								fileURLToPath(new URL(options.appEntrypoint, options.root))
							);
						} else {
							resolved = await this.resolve(options.appEntrypoint, fileURLToPath(options.root));
						}
						if (!resolved) {
							// This error is handled below, the message isn't shown to the user
							throw new Error('Unable to resolve appEntrypoint');
						}
						const exports = await getExports(resolved.id);
						if (!exports.includes('default')) {
							options.logger.warn(
								`appEntrypoint \`${options.appEntrypoint}\` does not export a default function. Check out https://docs.astro.build/en/guides/integrations-guide/vue/#appentrypoint.`
							);
							return noop;
						}
						return `export { default as setup } from "${resolved.id}";`;
					} catch {
						options.logger.warn(
							`Unable to resolve appEntrypoint \`${options.appEntrypoint}\`. Does the file exist?`
						);
					}
				}
				return noop;
			}
		},
	} satisfies Plugin;
}

async function getViteConfiguration(options: ViteOptions): Promise<UserConfig> {
	const config: UserConfig = {
		optimizeDeps: {
			include: ['@astrojs/vue/client.js', 'vue'],
			exclude: ['@astrojs/vue/server.js', 'virtual:@astrojs/vue/app'],
		},
		plugins: [vue(options), virtualAppEntrypoint(options)],
		ssr: {
			external: ['@vue/server-renderer'],
			noExternal: ['vuetify', 'vueperslides', 'primevue'],
		},
	};

	if (options?.jsx) {
		const vueJsx = (await import('@vitejs/plugin-vue-jsx')).default;
		const jsxOptions = typeof options.jsx === 'object' ? options.jsx : undefined;
		config.plugins?.push(vueJsx(jsxOptions));
	}

	return config;
}

export default function (options?: Options): AstroIntegration {
	return {
		name: '@astrojs/vue',
		hooks: {
			'astro:config:setup': async ({ addRenderer, updateConfig, config, logger }) => {
				addRenderer(getRenderer());
				if (options?.jsx) {
					addRenderer(getJsxRenderer());
				}
				updateConfig({
					vite: await getViteConfiguration({ ...options, root: config.root, logger }),
				});
			},
		},
	};
}