Skip to content

Commit

Permalink
chore: code suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed May 25, 2023
1 parent 37418e1 commit 86eb4b2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 24 deletions.
21 changes: 12 additions & 9 deletions packages/astro/src/core/build/plugins/plugin-pages.ts
Expand Up @@ -13,6 +13,16 @@ export const ASTRO_PAGE_RESOLVED_MODULE_ID = '\0@astro-page:';
// This is an arbitrary string that we are going to replace the dot of the extension
export const ASTRO_PAGE_EXTENSION_POST_PATTERN = '@_@';

export function getVirtualModulePageNameFromPath(path: string) {
// we mask the extension, so this virtual file
// so rollup won't trigger other plugins in the process
const extension = extname(path);
return `${ASTRO_PAGE_MODULE_ID}${path.replace(
extension,
extension.replace('.', ASTRO_PAGE_EXTENSION_POST_PATTERN)
)}`;
}

function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): VitePlugin {
return {
name: '@astro/plugin-build-pages',
Expand All @@ -22,14 +32,7 @@ function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): V
const inputs: Set<string> = new Set();

for (const path of Object.keys(opts.allPages)) {
const extension = extname(path);

// we mask the extension, so this virtual file
// so rollup won't trigger other plugins in the process
const virtualModuleName = `${ASTRO_PAGE_MODULE_ID}${path.replace(
extension,
extension.replace('.', ASTRO_PAGE_EXTENSION_POST_PATTERN)
)}`;
const virtualModuleName = getVirtualModulePageNameFromPath(path);

inputs.add(virtualModuleName);
}
Expand All @@ -49,7 +52,7 @@ function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): V
const imports: string[] = [];
const exports: string[] = [];
// split by ":", the second element is the page name, which will start with "src/..."
const [, pageName] = id.split(':');
const pageName = id.slice(ASTRO_PAGE_RESOLVED_MODULE_ID.length);
// We replaced the `.` of the extension with ASTRO_PAGE_EXTENSION_POST_PATTERN, let's replace it back
const pageData = internals.pagesByComponent.get(
`${pageName.replace(ASTRO_PAGE_EXTENSION_POST_PATTERN, '.')}`
Expand Down
14 changes: 4 additions & 10 deletions packages/astro/src/core/build/plugins/plugin-ssr.ts
Expand Up @@ -14,9 +14,8 @@ import { addRollupInput } from '../add-rollup-input.js';
import { getOutFile, getOutFolder } from '../common.js';
import { cssOrder, mergeInlineCss, type BuildInternals } from '../internal.js';
import { MIDDLEWARE_MODULE_ID } from './plugin-middleware.js';
import { extname } from 'node:path';
import { RENDERERS_MODULE_ID } from './plugin-renderers.js';
import { ASTRO_PAGE_EXTENSION_POST_PATTERN, ASTRO_PAGE_MODULE_ID } from './plugin-pages.js';
import { getVirtualModulePageNameFromPath } from './plugin-pages.js';

export const SSR_VIRTUAL_MODULE_ID = '@astrojs-ssr-virtual-entry';
const RESOLVED_SSR_VIRTUAL_MODULE_ID = '\0' + SSR_VIRTUAL_MODULE_ID;
Expand Down Expand Up @@ -57,16 +56,12 @@ function vitePluginSSR(
const pageMap: string[] = [];

for (const path of Object.keys(allPages)) {
const extension = extname(path);
const virtualModuleName = `${ASTRO_PAGE_MODULE_ID}${path.replace(
extension,
extension.replace('.', ASTRO_PAGE_EXTENSION_POST_PATTERN)
)}`;
const virtualModuleName = getVirtualModulePageNameFromPath(path);
let module = await this.resolve(virtualModuleName);
if (module) {
const variable = `_page${i}`;
// we need to use the non-resolved ID in order to resolve correctly the virtual module
imports.push(`const ${variable} = () => import("${virtualModuleName}")`);
imports.push(`const ${variable} = () => import("${virtualModuleName}");`);

const pageData = internals.pagesByComponent.get(path);
if (pageData) {
Expand Down Expand Up @@ -110,8 +105,7 @@ const _start = 'start';
if(_start in adapter) {
adapter[_start](_manifest, _args);
}`;
const result = [imports.join('\n'), contents.join('\n'), content, exports.join('\n')];
return result.join('\n');
return `${imports.join('\n')}${contents.join('\n')}${content}${exports.join('\n')}`;
}
return void 0;
},
Expand Down
1 change: 0 additions & 1 deletion packages/astro/src/core/build/plugins/util.ts
@@ -1,5 +1,4 @@
import type { Plugin as VitePlugin } from 'vite';
import type { BuildInternals } from '../internal';

// eslint-disable-next-line @typescript-eslint/ban-types
type OutputOptionsHook = Extract<VitePlugin['outputOptions'], Function>;
Expand Down
3 changes: 0 additions & 3 deletions packages/astro/src/core/build/static-build.ts
Expand Up @@ -24,7 +24,6 @@ import { generatePages } from './generate.js';
import { trackPageData } from './internal.js';
import { createPluginContainer, type AstroBuildPluginContainer } from './plugin.js';
import { registerAllPlugins } from './plugins/index.js';
import { RESOLVED_MIDDLEWARE_MODULE_ID } from './plugins/plugin-middleware.js';
import { RESOLVED_RENDERERS_MODULE_ID } from './plugins/plugin-renderers.js';
import type { PageBuildData, StaticBuildOptions } from './types';
import { getTimeStat } from './util.js';
Expand Down Expand Up @@ -181,8 +180,6 @@ async function ssrBuild(
return makeAstroPageEntryPointFileName(chunkInfo.facadeModuleId);
} else if (chunkInfo.facadeModuleId === SSR_VIRTUAL_MODULE_ID) {
return opts.settings.config.build.serverEntry;
} else if (chunkInfo.facadeModuleId === RESOLVED_MIDDLEWARE_MODULE_ID) {
return 'middleware.mjs';
} else if (chunkInfo.facadeModuleId === RESOLVED_RENDERERS_MODULE_ID) {
return 'renderers.mjs';
} else {
Expand Down
1 change: 0 additions & 1 deletion packages/integrations/mdx/src/index.ts
Expand Up @@ -12,7 +12,6 @@ import { VFile } from 'vfile';
import type { Plugin as VitePlugin } from 'vite';
import { getRehypePlugins, getRemarkPlugins, recmaInjectImportMetaEnvPlugin } from './plugins.js';
import { getFileInfo, ignoreStringPlugins, parseFrontmatter } from './utils.js';
import { extname } from 'node:path';

export type MdxOptions = Omit<typeof markdownConfigDefaults, 'remarkPlugins' | 'rehypePlugins'> & {
extendMarkdownConfig: boolean;
Expand Down

0 comments on commit 86eb4b2

Please sign in to comment.