0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-04-07 23:41:43 -05:00

Fix build

This commit is contained in:
Matthew Phillips 2024-06-21 14:25:50 -04:00
parent b5ceb65ac2
commit 1da675c1fd
8 changed files with 45 additions and 55 deletions

View file

@ -29,6 +29,7 @@ import {
getEntryType,
getExtGlob,
} from './utils.js';
import { createDefaultAstroMetadata } from '../vite-plugin-astro/metadata.js';
interface AstroContentVirtualModPluginParams {
settings: AstroSettings;
@ -76,17 +77,12 @@ export function astroContentVirtualModPlugin({
isClient,
});
const astro = createDefaultAstroMetadata();
astro.propagation = 'in-tree';
return {
code,
meta: {
astro: {
hydratedComponents: [],
clientOnlyComponents: [],
scripts: [],
containsHead: false,
propagation: 'in-tree',
pageOptions: {},
},
astro,
} satisfies AstroPluginMetadata,
};
}

View file

@ -17,7 +17,7 @@ import {
createModuleScriptsSet,
createStylesheetElementSet,
} from '../render/ssr-element.js';
import { default404Page } from '../routing/astro-designed-error-pages.js';
import { createDefaultRoutes } from '../routing/default.js';
import { findRouteToRewrite } from '../routing/rewrite.js';
import { isServerLikeOutput } from '../util.js';
import { getOutDirWithinCwd } from './common.js';
@ -54,7 +54,8 @@ export class BuildPipeline extends Pipeline {
readonly manifest: SSRManifest,
readonly options: StaticBuildOptions,
readonly config = options.settings.config,
readonly settings = options.settings
readonly settings = options.settings,
readonly defaultRoutes = createDefaultRoutes(manifest, config.root)
) {
const resolveCache = new Map<string, string>();
async function resolve(specifier: string) {
@ -269,14 +270,18 @@ export class BuildPipeline extends Pipeline {
// SAFETY: checked before
const entry = this.#componentsInterner.get(routeData)!;
return await entry.page();
} else if (routeData.component === DEFAULT_404_COMPONENT) {
return { default: default404Page };
} else {
// SAFETY: the pipeline calls `retrieveRoutesToGenerate`, which is in charge to fill the cache.
const filePath = this.#routesByFilePath.get(routeData)!;
const module = await this.retrieveSsrEntry(routeData, filePath);
return module.page();
}
for(const route of this.defaultRoutes) {
if(route.component === routeData.component) {
return route.instance;
}
}
// SAFETY: the pipeline calls `retrieveRoutesToGenerate`, which is in charge to fill the cache.
const filePath = this.#routesByFilePath.get(routeData)!;
const module = await this.retrieveSsrEntry(routeData, filePath);
return module.page();
}
async tryRewrite(

View file

@ -13,6 +13,7 @@ type DefaultRouteParams = {
instance: ComponentInstance;
matchesComponent(filePath: URL): boolean;
route: string;
component: string;
}
export function createDefaultRoutes(manifest: SSRManifest, root: URL): DefaultRouteParams[] {
@ -21,11 +22,13 @@ export function createDefaultRoutes(manifest: SSRManifest, root: URL): DefaultRo
instance: default404Instance,
matchesComponent: (filePath) => filePath.href === new URL(DEFAULT_404_COMPONENT, root).href,
route: DEFAULT_404_ROUTE.route,
component: DEFAULT_404_COMPONENT,
},
{
instance: createServerIslandEndpoint(manifest),
matchesComponent: (filePath) => filePath.href === new URL(SERVER_ISLAND_COMPONENT, root).href,
route: SERVER_ISLAND_ROUTE,
component: SERVER_ISLAND_COMPONENT,
}
];
}

View file

@ -4,6 +4,7 @@ import { AstroError } from '../core/errors/errors.js';
import { AstroErrorData } from '../core/errors/index.js';
import { resolvePath } from '../core/util.js';
import type { PluginMetadata } from '../vite-plugin-astro/types.js';
import { createDefaultAstroMetadata } from '../vite-plugin-astro/metadata.js';
const ClientOnlyPlaceholder = 'astro-client-only';
@ -143,14 +144,7 @@ export default function astroJSX(): PluginObj {
Program: {
enter(path, state) {
if (!(state.file.metadata as PluginMetadata).astro) {
(state.file.metadata as PluginMetadata).astro = {
clientOnlyComponents: [],
hydratedComponents: [],
scripts: [],
containsHead: false,
propagation: 'none',
pageOptions: {},
};
(state.file.metadata as PluginMetadata).astro = createDefaultAstroMetadata();
}
path.node.body.splice(
0,
@ -227,6 +221,7 @@ export default function astroJSX(): PluginObj {
if (isClientOnly) {
(state.file.metadata as PluginMetadata).astro.clientOnlyComponents.push({
exportName: meta.name,
localName: '',
specifier: tagName,
resolvedPath,
});
@ -236,6 +231,7 @@ export default function astroJSX(): PluginObj {
} else {
(state.file.metadata as PluginMetadata).astro.hydratedComponents.push({
exportName: '*',
localName: '',
specifier: tagName,
resolvedPath,
});
@ -300,6 +296,7 @@ export default function astroJSX(): PluginObj {
if (isClientOnly) {
(state.file.metadata as PluginMetadata).astro.clientOnlyComponents.push({
exportName: meta.name,
localName: '',
specifier: meta.name,
resolvedPath,
});
@ -309,6 +306,7 @@ export default function astroJSX(): PluginObj {
} else {
(state.file.metadata as PluginMetadata).astro.hydratedComponents.push({
exportName: meta.name,
localName: '',
specifier: meta.name,
resolvedPath,
});

View file

@ -14,20 +14,14 @@ import type { PluginMetadata } from '../vite-plugin-astro/types.js';
// This import includes ambient types for hast to include mdx nodes
import type {} from 'mdast-util-mdx';
import { createDefaultAstroMetadata } from '../vite-plugin-astro/metadata.js';
const ClientOnlyPlaceholder = 'astro-client-only';
export const rehypeAnalyzeAstroMetadata: RehypePlugin = () => {
return (tree, file) => {
// Initial metadata for this MDX file, it will be mutated as we traverse the tree
const metadata: PluginMetadata['astro'] = {
clientOnlyComponents: [],
hydratedComponents: [],
scripts: [],
containsHead: false,
propagation: 'none',
pageOptions: {},
};
const metadata = createDefaultAstroMetadata();
// Parse imports in this file. This is used to match components with their import source
const imports = parseImports(tree.children);
@ -71,6 +65,7 @@ export const rehypeAnalyzeAstroMetadata: RehypePlugin = () => {
// Add this component to the metadata
metadata.clientOnlyComponents.push({
exportName: matchedImport.name,
localName: '',
specifier: tagName,
resolvedPath,
});
@ -80,6 +75,7 @@ export const rehypeAnalyzeAstroMetadata: RehypePlugin = () => {
// Add this component to the metadata
metadata.hydratedComponents.push({
exportName: '*',
localName: '',
specifier: tagName,
resolvedPath,
});

View file

@ -105,19 +105,6 @@ export async function matchRoute(
const custom404 = getCustom404Route(manifestData);
if (custom404 && custom404.component === DEFAULT_404_COMPONENT) {
const component: ComponentInstance = {
default: default404Page,
};
return {
route: custom404,
filePath: new URL(`file://${custom404.component}`),
resolvedPathname: pathname,
preloadedComponent: component,
mod: component,
};
}
if (custom404) {
const filePath = new URL(`./${custom404.component}`, config.root);
const preloadedComponent = await pipeline.preload(custom404, filePath);

View file

@ -7,3 +7,15 @@ export function getAstroMetadata(modInfo: ModuleInfo): PluginMetadata['astro'] |
}
return undefined;
}
export function createDefaultAstroMetadata(): PluginMetadata['astro'] {
return {
hydratedComponents: [],
clientOnlyComponents: [],
serverComponents: [],
scripts: [],
propagation: 'none',
containsHead: false,
pageOptions: {},
};
}

View file

@ -13,9 +13,9 @@ import { AstroError, AstroErrorData } from '../core/errors/index.js';
import type { Logger } from '../core/logger/core.js';
import { isMarkdownFile } from '../core/util.js';
import { shorthash } from '../runtime/server/shorthash.js';
import type { PluginMetadata } from '../vite-plugin-astro/types.js';
import { getFileInfo } from '../vite-plugin-utils/index.js';
import { type MarkdownImagePath, getMarkdownCodeForImages } from './images.js';
import { createDefaultAstroMetadata } from '../vite-plugin-astro/metadata.js';
interface AstroPluginOptions {
settings: AstroSettings;
@ -159,14 +159,7 @@ export default function markdown({ settings, logger }: AstroPluginOptions): Plug
return {
code,
meta: {
astro: {
hydratedComponents: [],
clientOnlyComponents: [],
scripts: [],
propagation: 'none',
containsHead: false,
pageOptions: {},
} as PluginMetadata['astro'],
astro: createDefaultAstroMetadata(),
vite: {
lang: 'ts',
},