Skip to content

Commit

Permalink
feat: add fragment support to vite-plugin-astro
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate Moore committed Oct 21, 2021
1 parent b64c0b5 commit 1948116
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/astro/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')
Expand Down
6 changes: 5 additions & 1 deletion packages/astro/src/vite-plugin-astro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;

Expand All @@ -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',
Expand Down

0 comments on commit 1948116

Please sign in to comment.