0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

remove broken & useless virtualModuleNameFromResolvedId

This commit is contained in:
Goulven Clec'h 2024-04-26 18:10:20 +02:00
parent cdbdcae311
commit 9b02b89920
2 changed files with 20 additions and 31 deletions

View file

@ -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);

View file

@ -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(