diff --git a/packages/astro/src/core/build/plugins/plugin-ssr.ts b/packages/astro/src/core/build/plugins/plugin-ssr.ts index 4846d287b7..f7bc2e4076 100644 --- a/packages/astro/src/core/build/plugins/plugin-ssr.ts +++ b/packages/astro/src/core/build/plugins/plugin-ssr.ts @@ -13,7 +13,7 @@ import { SSR_MANIFEST_VIRTUAL_MODULE_ID } from './plugin-manifest.js'; import { MIDDLEWARE_MODULE_ID } from './plugin-middleware.js'; import { ASTRO_PAGE_MODULE_ID } from './plugin-pages.js'; import { RENDERERS_MODULE_ID } from './plugin-renderers.js'; -import { getPathFromVirtualModulePageName, getVirtualModulePageName, virtualModuleNameFromResolvedId } from './util.js'; +import { getRouteAndComponentFromVirtualModulePageName, getVirtualModulePageName, getPageKeyFromVirtualModulePageName } from './util.js'; export const SSR_VIRTUAL_MODULE_ID = '@astrojs-ssr-virtual-entry'; export const RESOLVED_SSR_VIRTUAL_MODULE_ID = '\0' + SSR_VIRTUAL_MODULE_ID; @@ -172,7 +172,9 @@ function vitePluginSSRSplit( const imports: string[] = []; const contents: string[] = []; const exports: string[] = []; - const virtualModuleName = virtualModuleNameFromResolvedId(ASTRO_PAGE_MODULE_ID, RESOLVED_SPLIT_MODULE_ID, id); + const pageKey = getPageKeyFromVirtualModulePageName(RESOLVED_SPLIT_MODULE_ID, id); + const pageData = internals.pagesByKeys.get(pageKey); + const virtualModuleName = getVirtualModulePageName(ASTRO_PAGE_MODULE_ID, pageData!.component, pageData!.route.route); let module = await this.resolve(virtualModuleName); if (module) { imports.push(`import * as pageModule from "${virtualModuleName}";`); @@ -294,7 +296,7 @@ function storeEntryPoint( internals: BuildInternals, fileName: string ) { - const componentPath = getPathFromVirtualModulePageName(RESOLVED_SPLIT_MODULE_ID, moduleKey); + const [_route, componentPath] = getRouteAndComponentFromVirtualModulePageName(RESOLVED_SPLIT_MODULE_ID, moduleKey); for (const pageData of Object.values(options.allPages)) { if (componentPath == pageData.component) { const publicPath = fileURLToPath(options.settings.config.build.server); diff --git a/packages/astro/src/core/build/plugins/util.ts b/packages/astro/src/core/build/plugins/util.ts index 9dd34f0d70..7d6cb025da 100644 --- a/packages/astro/src/core/build/plugins/util.ts +++ b/packages/astro/src/core/build/plugins/util.ts @@ -57,7 +57,7 @@ export function makePageDataKey(route: string, componentPath: string) { /** * Prevents Rollup from triggering other plugins in the process by masking the extension (hence the virtual file). - * + * Inverse function of getRouteAndComponentFromVirtualModulePageName() below. * @param virtualModulePrefix The prefix used to create the virtual module * @param path Page component path * @param route Route of the page @@ -72,43 +72,30 @@ export function getVirtualModulePageName(virtualModulePrefix: string, path: stri ); } -/** - * In SSR plugins, we need to use the non-resolved virtualModuleName in order to resolve correctly the virtual module. - * @param virtualModulePrefix The prefix used to create the virtual module - * @param resolvedModulePrefix The prefix of the resolved virtual module - * @param resolvedId The resolved virtual module id - * @returns - */ -export function virtualModuleNameFromResolvedId(virtualModulePrefix: string, resolvedModulePrefix: string, resolvedId: string) { - const extension = extname(resolvedId); - const clean_path = resolvedId.slice(resolvedModulePrefix.length); - return ( - virtualModulePrefix + - (extension.startsWith('.') - ? clean_path.slice(0, -extension.length) + extension.replace('.', ASTRO_PAGE_EXTENSION_POST_PATTERN) - : clean_path - ) - ); -} - /** * From the VirtualModulePageName, get the original pageData key. + * Useful to retrieve the pageData from internals.pagesByKeys.get() * @param virtualModulePrefix The prefix used to create the virtual module * @param id Virtual module name */ export function getPageKeyFromVirtualModulePageName(virtualModulePrefix: string, id: string) { + const [route, path] = getRouteAndComponentFromVirtualModulePageName(virtualModulePrefix, id); + return makePageDataKey(route, path); +} + +/** + * From the VirtualModulePageName, get the route and the component path. + * Inverse function of getVirtualModulePageName() above. + * @param virtualModulePrefix The prefix at the beginning of the virtual module + * @param id Virtual module name + * @returns [route, componentPath] as [string, string] + */ +export function getRouteAndComponentFromVirtualModulePageName(virtualModulePrefix: string, id: string) { const [route, path] = id .slice(virtualModulePrefix.length) .split(ASTRO_PAGE_KEY_SEPARATOR); - const componentPath = path.replace(ASTRO_PAGE_EXTENSION_POST_PATTERN, '.'); - return makePageDataKey(route, componentPath); -} - -// TODO: Should this be removed? Or refactored in generate.ts ? -export function getPathFromVirtualModulePageName(virtualModulePrefix: string, id: string) { - const pageName = id.slice(virtualModulePrefix.length); - return pageName.replace(ASTRO_PAGE_EXTENSION_POST_PATTERN, '.'); + return [route, path.replace(ASTRO_PAGE_EXTENSION_POST_PATTERN, '.')]; } export function shouldInlineAsset(