feat: add fragment support to vite-plugin-astro (#1600)
This commit is contained in:
parent
dfe23864e0
commit
2c36d0a427
2 changed files with 14 additions and 1 deletions
|
@ -22,6 +22,11 @@ export const AstroConfigSchema = z.object({
|
||||||
.optional()
|
.optional()
|
||||||
.default('./src/pages')
|
.default('./src/pages')
|
||||||
.transform((val) => new URL(val)),
|
.transform((val) => new URL(val)),
|
||||||
|
layouts: z
|
||||||
|
.string()
|
||||||
|
.optional()
|
||||||
|
.default('./src/layouts')
|
||||||
|
.transform((val) => new URL(val)),
|
||||||
public: z
|
public: z
|
||||||
.string()
|
.string()
|
||||||
.optional()
|
.optional()
|
||||||
|
@ -87,6 +92,10 @@ export async function validateConfig(userConfig: any, root: string): Promise<Ast
|
||||||
.string()
|
.string()
|
||||||
.default('./src/pages')
|
.default('./src/pages')
|
||||||
.transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
|
.transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
|
||||||
|
layouts: z
|
||||||
|
.string()
|
||||||
|
.default('./src/layouts')
|
||||||
|
.transform((val) => new URL(addTrailingSlash(val), fileProtocolRoot)),
|
||||||
public: z
|
public: z
|
||||||
.string()
|
.string()
|
||||||
.default('./public')
|
.default('./public')
|
||||||
|
|
|
@ -4,6 +4,7 @@ import type { AstroConfig } from '../@types/astro-core';
|
||||||
|
|
||||||
import esbuild from 'esbuild';
|
import esbuild from 'esbuild';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
import { transform } from '@astrojs/compiler';
|
import { transform } from '@astrojs/compiler';
|
||||||
import { decode } from 'sourcemap-codec';
|
import { decode } from 'sourcemap-codec';
|
||||||
import { AstroDevServer } from '../core/dev/index.js';
|
import { AstroDevServer } from '../core/dev/index.js';
|
||||||
|
@ -23,7 +24,9 @@ export default function astro({ config, devServer }: AstroPluginOptions): Plugin
|
||||||
if (!id.endsWith('.astro')) {
|
if (!id.endsWith('.astro')) {
|
||||||
return null;
|
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 source = await fs.promises.readFile(id, 'utf8');
|
||||||
let tsResult: TransformResult | undefined;
|
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
|
// use `sourcemap: "both"` so that sourcemap is included in the code
|
||||||
// result passed to esbuild, but also available in the catch handler.
|
// result passed to esbuild, but also available in the catch handler.
|
||||||
tsResult = await transform(source, {
|
tsResult = await transform(source, {
|
||||||
|
as: isPage ? "document" : "fragment",
|
||||||
site: config.buildOptions.site,
|
site: config.buildOptions.site,
|
||||||
sourcefile: id,
|
sourcefile: id,
|
||||||
sourcemap: 'both',
|
sourcemap: 'both',
|
||||||
|
|
Loading…
Reference in a new issue