From 2c36d0a42733333d9e75fe003159b2ae4c1ddb11 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Thu, 21 Oct 2021 13:50:17 -0500 Subject: [PATCH] feat: add fragment support to vite-plugin-astro (#1600) --- packages/astro/src/core/config.ts | 9 +++++++++ packages/astro/src/vite-plugin-astro/index.ts | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/core/config.ts b/packages/astro/src/core/config.ts index 9657d36dc..9c830496b 100644 --- a/packages/astro/src/core/config.ts +++ b/packages/astro/src/core/config.ts @@ -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 new URL(addTrailingSlash(val), fileProtocolRoot)), + layouts: z + .string() + .default('./src/layouts') + .transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)), public: z .string() .default('./public') diff --git a/packages/astro/src/vite-plugin-astro/index.ts b/packages/astro/src/vite-plugin-astro/index.ts index f06e0ad39..5b5b46b5f 100644 --- a/packages/astro/src/vite-plugin-astro/index.ts +++ b/packages/astro/src/vite-plugin-astro/index.ts @@ -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 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',