2022-09-29 03:25:45 +00:00
|
|
|
import type { Options as VueOptions } from '@vitejs/plugin-vue';
|
|
|
|
import type { Options as VueJsxOptions } from '@vitejs/plugin-vue-jsx';
|
2022-09-06 15:12:47 +00:00
|
|
|
import vue from '@vitejs/plugin-vue';
|
2022-06-06 16:49:53 +00:00
|
|
|
import type { AstroIntegration, AstroRenderer } from 'astro';
|
2022-09-06 15:11:01 +00:00
|
|
|
import type { UserConfig } from 'vite';
|
2022-03-18 22:35:45 +00:00
|
|
|
|
2022-09-29 03:25:45 +00:00
|
|
|
interface Options extends VueOptions {
|
|
|
|
jsx?: boolean | VueJsxOptions;
|
|
|
|
}
|
|
|
|
|
2022-03-18 22:35:45 +00:00
|
|
|
function getRenderer(): AstroRenderer {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/vue',
|
|
|
|
clientEntrypoint: '@astrojs/vue/client.js',
|
|
|
|
serverEntrypoint: '@astrojs/vue/server.js',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-29 03:25:45 +00:00
|
|
|
function getJsxRenderer(): AstroRenderer {
|
2022-03-18 22:35:45 +00:00
|
|
|
return {
|
2022-09-29 03:25:45 +00:00
|
|
|
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],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getViteConfiguration(options?: Options): Promise<UserConfig> {
|
|
|
|
const config: UserConfig = {
|
2022-03-18 22:35:45 +00:00
|
|
|
optimizeDeps: {
|
|
|
|
include: ['@astrojs/vue/client.js', 'vue'],
|
|
|
|
exclude: ['@astrojs/vue/server.js'],
|
|
|
|
},
|
2022-04-19 16:31:32 +00:00
|
|
|
plugins: [vue(options)],
|
2022-03-18 22:35:45 +00:00
|
|
|
ssr: {
|
|
|
|
external: ['@vue/server-renderer'],
|
2022-09-06 15:12:47 +00:00
|
|
|
noExternal: ['vueperslides'],
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
|
|
|
};
|
2022-09-29 03:25:45 +00:00
|
|
|
|
|
|
|
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;
|
2022-03-18 22:35:45 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 16:31:32 +00:00
|
|
|
export default function (options?: Options): AstroIntegration {
|
2022-03-18 22:35:45 +00:00
|
|
|
return {
|
|
|
|
name: '@astrojs/vue',
|
|
|
|
hooks: {
|
2022-09-29 03:25:45 +00:00
|
|
|
'astro:config:setup': async ({ addRenderer, updateConfig }) => {
|
2022-03-18 22:35:45 +00:00
|
|
|
addRenderer(getRenderer());
|
2022-09-29 03:25:45 +00:00
|
|
|
if (options?.jsx) {
|
|
|
|
addRenderer(getJsxRenderer());
|
|
|
|
}
|
|
|
|
updateConfig({ vite: await getViteConfiguration(options) });
|
2022-03-18 22:35:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|