feat: add fragment support to vite-plugin-astro (#1600)

This commit is contained in:
Nate Moore 2021-10-21 13:50:17 -05:00 committed by Drew Powers
parent dfe23864e0
commit 2c36d0a427
2 changed files with 14 additions and 1 deletions

View file

@ -22,6 +22,11 @@ export const AstroConfigSchema = z.object({
.optional()
.default('./src/pages')
.transform((val) => new URL(val)),
layouts: z
.string()
.optional()
.default('./src/layouts')
.transform((val) => new URL(val)),
public: z
.string()
.optional()
@ -87,6 +92,10 @@ export async function validateConfig(userConfig: any, root: string): Promise<Ast
.string()
.default('./src/pages')
.transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
layouts: z
.string()
.default('./src/layouts')
.transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
public: z
.string()
.default('./public')

View file

@ -4,6 +4,7 @@ import type { AstroConfig } from '../@types/astro-core';
import esbuild from 'esbuild';
import fs from 'fs';
import { fileURLToPath } from 'url';
import { transform } from '@astrojs/compiler';
import { decode } from 'sourcemap-codec';
import { AstroDevServer } from '../core/dev/index.js';
@ -23,7 +24,9 @@ export default function astro({ config, devServer }: AstroPluginOptions): Plugin
if (!id.endsWith('.astro')) {
return null;
}
// const isPage = id.startsWith(fileURLToPath(config.pages));
// pages and layouts should be transformed as full documents (implicit <head> <body> etc)
// everything else is treated as a fragment
const isPage = id.startsWith(fileURLToPath(config.pages)) || id.startsWith(fileURLToPath(config.layouts));
let source = await fs.promises.readFile(id, 'utf8');
let tsResult: TransformResult | undefined;
@ -32,6 +35,7 @@ export default function astro({ config, devServer }: AstroPluginOptions): Plugin
// use `sourcemap: "both"` so that sourcemap is included in the code
// result passed to esbuild, but also available in the catch handler.
tsResult = await transform(source, {
as: isPage ? "document" : "fragment",
site: config.buildOptions.site,
sourcefile: id,
sourcemap: 'both',