import path from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import type { Plugin, ResolvedConfig } from 'vite'; import type * as z from 'zod/v4'; import type { AstroPluginCli } from './internal/cli.js'; import { type AstroPluginOptions, AstroPluginOptionsSchema } from './options.js'; import type { AstroRenderer, ClientDirectiveConfig } from '@astrojs/runtime/types'; import compilePlugins, { type AstroCompileConfig } from './internal/vite-plugin-astro/index.js'; import clientDirectivesPlugin from './vite-plugin-client-directives/index.js'; import componentAssetsPlugin from './vite-plugin-component-assets/index.js'; import fileUrlPlugin from './vite-plugin-fileurl/index.js'; import headPlugin from './internal/vite-plugin-head/index.js'; import hmrReloadPlugin from './internal/vite-plugin-hmr-reload/index.js'; import renderSetupPlugin from './vite-plugin-render-setup/index.js'; import renderersPlugin from './internal/vite-plugin-renderers/index.js'; import scriptsPlugin from './internal/vite-plugin-scripts/index.js'; /** * Project configuration owned by `astro()` and shared with every plugin built on it. */ export interface ResolvedSharedConfig { /** Absolute project root, with a trailing slash. */ root: URL; /** Absolute public directory, with a trailing slash. */ publicDir: URL; /** Vite's resolved `base`. */ base: string; site: string | undefined; compressHTML: boolean | 'jsx'; trailingSlash: 'always' | 'never' | 'ignore'; } /** * The API exposed by `astro()` as `plugin.api.astro`. Read it with `getAstroApi()`. */ export interface AstroPluginApi { /** * The resolved shared configuration. * * `astro()` resolves it in a `configResolved` hook with `order: 'pre'`, so it is available in * every other plugin's `configResolved` hook (without `order: 'pre'`) and later. * * @throws {Error} When read before it has been resolved. */ readonly config: ResolvedSharedConfig; /** Renderers passed in `AstroPluginOptions.renderers`. */ readonly renderers: readonly AstroRenderer[]; /** Custom directives passed in `AstroPluginOptions.clientDirectives`. */ readonly clientDirectives: readonly ClientDirectiveConfig[]; } const PLUGIN_NAME = '@astrojs/vite-plugin-astro'; const RUNTIME_NAME = '@astrojs/runtime'; /** * Compiles `.astro` components and registers what `renderToString()` from `@astrojs/runtime` needs to * render them: renderers, client directives, and each component tree's stylesheets and island URLs. * * @param cli Set by the `astro` CLI only. */ export default function astro(options: AstroPluginOptions = {}, cli?: AstroPluginCli): Plugin[] { const parsed = AstroPluginOptionsSchema.parse( options satisfies z.input, ); let config: ResolvedSharedConfig | undefined; // Filled in once Vite's config is resolved; the compiler reads it on use. const compileConfig = {} as AstroCompileConfig; // The objects passed in, not the parsed copies: renderers of integrations may carry more fields, // which reach the renderers module. const renderers = options.renderers ?? []; const api: AstroPluginApi = { get config() { if (!config) { throw new Error( `The ${PLUGIN_NAME} config was read before Vite resolved its config. Read \`api.astro.config\` in a \`configResolved\` hook or later.`, ); } return config; }, renderers, clientDirectives: parsed.clientDirectives, }; const plugins: Plugin[] = [ { name: PLUGIN_NAME, api: { astro: api }, configResolved: { order: 'pre', handler(viteConfig) { config = resolveSharedConfig(parsed, viteConfig); Object.assign(compileConfig, { root: config.root, publicDir: config.publicDir, base: config.base, site: config.site, compressHTML: config.compressHTML, trailingSlash: config.trailingSlash, scopedStyleStrategy: 'attribute', devToolbar: { enabled: false }, build: { format: 'directory' }, } satisfies AstroCompileConfig); }, }, configEnvironment: { // After other plugins, to drop the externals they add. order: 'post', handler(name, environmentOptions) { const consumer = environmentOptions.consumer ?? (name === 'client' ? 'client' : 'server'); if (consumer !== 'server') return; // Vite processes the runtime rather than externalizing it, so that compiled components, // `virtual:astro:render-setup` and `renderToString()` share one copy of it (resolved by // the hook below), and a build bundles it. This package is processed too, so that its // re-exports of the runtime resolve to that copy. // An explicit `external` wins over `noExternal`: in dev, vitefu-based plugins such as // `@sveltejs/vite-plugin-svelte` externalize the dependencies of packages that depend on // their framework, among them the runtime as a dependency of a renderer package. const external = environmentOptions.resolve?.external; if (Array.isArray(external)) { environmentOptions.resolve!.external = external.filter( (id) => id !== PLUGIN_NAME && id !== RUNTIME_NAME, ); } return { resolve: { noExternal: [PLUGIN_NAME, RUNTIME_NAME] } }; }, }, resolveId: { filter: { id: /^@astrojs\/runtime(?:\/|$)/ }, handler(id, _importer, resolveOptions) { // Every importer gets this package's copy of the runtime, which holds the render config // that `virtual:astro:render-setup` registers. Compiled components import it too, and a // project that does not depend on `@astrojs/runtime` cannot resolve it itself. return this.resolve(id, fileURLToPath(import.meta.url), { ...resolveOptions, skipSelf: true, }); }, }, }, ...compilePlugins({ config: cli?.compileConfig ?? compileConfig, // Vite's logger has no debug level. logger: cli?.logger ?? { debug() {} }, toolbarEnabled: cli?.toolbarEnabled ?? (() => false), internalURL: cli?.internalURL, transitionsAnimationURL: cli?.transitionsAnimationURL, }), ...renderersPlugin({ renderers, root: parsed.root, skipRenderers: cli?.skipRenderers }), // The modules of the render API. The `astro` CLI renders pages itself. ...(cli ? [] : [ clientDirectivesPlugin({ clientDirectives: parsed.clientDirectives, getRoot: () => api.config.root, }), componentAssetsPlugin({ renderers, getRenderConfig: () => ({ compressHTML: api.config.compressHTML, site: api.config.site, }), }), renderSetupPlugin({ renderers }), ]), scriptsPlugin({ scripts: cli?.scripts ?? [] }), headPlugin({ environments: cli?.headEnvironments }), fileUrlPlugin(), hmrReloadPlugin({ getDevCssModuleId: cli?.getDevCssModuleId }), ]; // Vite resolves the config again for each build environment, which re-runs a config file and // creates new plugin instances. The client build reads state collected by the server build // (compile metadata, component assets), so every environment must use the same instances. for (const plugin of plugins) { plugin.sharedDuringBuild = true; } return plugins; } /** * Returns the API of the `astro()` plugin registered in `config`. * * @throws {Error} When `astro()` is not in Vite's `plugins`. */ export function getAstroApi(config: ResolvedConfig): AstroPluginApi { const api = config.plugins.find((plugin) => plugin.name === PLUGIN_NAME)?.api?.astro; if (!api) { throw new Error( `\`astro()\` from ${PLUGIN_NAME} is missing from the Vite \`plugins\` array. Plugins that read \`api.astro\` require it.`, ); } return api; } function resolveSharedConfig( options: z.output, viteConfig: ResolvedConfig, ): ResolvedSharedConfig { const root = path.resolve(viteConfig.root, options.root ?? '.'); return { root: resolveDirAsUrl(root, '.'), publicDir: resolveDirAsUrl(root, options.publicDir), base: viteConfig.base, site: options.site, compressHTML: options.compressHTML, trailingSlash: options.trailingSlash, }; } function resolveDirAsUrl(root: string, dir: string): URL { let resolvedDir = path.resolve(root, dir); if (!resolvedDir.endsWith(path.sep)) { resolvedDir += path.sep; } return pathToFileURL(resolvedDir); }