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 b94d215
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 36 deletions.
11 changes: 3 additions & 8 deletions packages/astro/src/core/build/plugins/README.md
Expand Up @@ -9,13 +9,6 @@ This plugin is responsible to retrieve the `src/middleware.{ts.js}` file and emi

The final file is emitted only if the user has the middleware file. The final name of the file is `middleware.mjs`.

The file emitted has this content, more or less:

```js
import { onRequest } from "@astro-middleware";
export { onRequest }
```

This is **not** a virtual module. The plugin will try to resolve the physical file.

## `plugin-renderers`
Expand Down Expand Up @@ -57,12 +50,14 @@ The mapping is as follows:
src/pages/index.astro => @astro-page:src/pages/index@_@astro
```

We replace the dot that belongs to the extension with an arbitrary string.
1. We add a fixed prefix, which is used as virtual module naming convention;
2. We replace the dot that belongs extension with an arbitrary string.

This kind of patterns will then allow us to retrieve the path physical path of the
file back from that string. This is important for the [code generation](#plugin-pages-code-generation)



### `plugin pages` code generation

When generating the code of the page, we will import and export the following modules:
Expand Down
31 changes: 19 additions & 12 deletions packages/astro/src/core/build/plugins/plugin-pages.ts
Expand Up @@ -13,6 +13,22 @@ 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 = '@_@';

/**
* 1. We add a fixed prefix, which is used as virtual module naming convention;
* 2. We replace the dot that belongs extension with an arbitrary string.
*
* @param path
*/
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,16 +38,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)
)}`;

inputs.add(virtualModuleName);
inputs.add(getVirtualModulePageNameFromPath(path));
}

return addRollupInput(options, Array.from(inputs));
Expand All @@ -48,8 +55,8 @@ function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): V
if (id.startsWith(ASTRO_PAGE_RESOLVED_MODULE_ID)) {
const imports: string[] = [];
const exports: string[] = [];
// split by ":", the second element is the page name, which will start with "src/..."
const [, pageName] = id.split(':');
// we remove the module name prefix from id, this will result into a string that will start with "src/..."
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
4 changes: 0 additions & 4 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 All @@ -33,7 +32,6 @@ import {
ASTRO_PAGE_RESOLVED_MODULE_ID,
} from './plugins/plugin-pages.js';
import { SSR_VIRTUAL_MODULE_ID } from './plugins/plugin-ssr.js';
import type { PreRenderedChunk } from 'rollup';

export async function viteBuild(opts: StaticBuildOptions) {
const { allPages, settings } = opts;
Expand Down Expand Up @@ -181,8 +179,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 b94d215

Please sign in to comment.