Skip to content

Commit

Permalink
chore: do not make middleware a virtual module
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed May 24, 2023
1 parent eabc987 commit ce809c5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
2 changes: 2 additions & 0 deletions packages/astro/src/core/build/plugins/README.md
Expand Up @@ -16,6 +16,8 @@ import { onRequest } from "@astro-middleware";
export { onRequest }
```

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

## `plugin-renderers`

This plugin is responsible to collect all the renderers inside an Astro application and emit them in a single file.
Expand Down
18 changes: 3 additions & 15 deletions packages/astro/src/core/build/plugins/plugin-middleware.ts
Expand Up @@ -20,26 +20,14 @@ export function vitePluginMiddleware(
}
},

resolveId(id) {
async resolveId(id) {
if (id === MIDDLEWARE_MODULE_ID && opts.settings.config.experimental.middleware) {
return RESOLVED_MIDDLEWARE_MODULE_ID;
}
},

async load(id) {
if (id === RESOLVED_MIDDLEWARE_MODULE_ID && opts.settings.config.experimental.middleware) {
const imports: string[] = [];
const exports: string[] = [];
let middlewareId = await this.resolve(
const middlewareId = await this.resolve(
`${opts.settings.config.srcDir.pathname}/${MIDDLEWARE_PATH_SEGMENT_NAME}`
);
if (middlewareId) {
imports.push(`import { onRequest } from "${middlewareId.id}"`);
exports.push(`export { onRequest }`);
return middlewareId.id;
}
const result = [imports.join('\n'), exports.join('\n')];

return result.join('\n');
}
},
};
Expand Down
14 changes: 7 additions & 7 deletions packages/astro/src/core/build/plugins/plugin-ssr.ts
Expand Up @@ -18,8 +18,8 @@ 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';

export const virtualModuleId = '@astrojs-ssr-virtual-entry';
const resolvedVirtualModuleId = '\0' + virtualModuleId;
export const SSR_VIRTUAL_MODULE_ID = '@astrojs-ssr-virtual-entry';
const RESOLVED_SSR_VIRTUAL_MODULE_ID = '\0' + SSR_VIRTUAL_MODULE_ID;
const manifestReplace = '@@ASTRO_MANIFEST_REPLACE@@';
const replaceExp = new RegExp(`['"](${manifestReplace})['"]`, 'g');

Expand All @@ -32,15 +32,15 @@ function vitePluginSSR(
name: '@astrojs/vite-plugin-astro-ssr',
enforce: 'post',
options(opts) {
return addRollupInput(opts, [virtualModuleId]);
return addRollupInput(opts, [SSR_VIRTUAL_MODULE_ID]);
},
resolveId(id) {
if (id === virtualModuleId) {
return resolvedVirtualModuleId;
if (id === SSR_VIRTUAL_MODULE_ID) {
return RESOLVED_SSR_VIRTUAL_MODULE_ID;
}
},
async load(id) {
if (id === resolvedVirtualModuleId) {
if (id === RESOLVED_SSR_VIRTUAL_MODULE_ID) {
const {
settings: { config },
allPages,
Expand Down Expand Up @@ -127,7 +127,7 @@ if(_start in adapter) {
if (chunk.type === 'asset') {
continue;
}
if (chunk.modules[resolvedVirtualModuleId]) {
if (chunk.modules[RESOLVED_SSR_VIRTUAL_MODULE_ID]) {
internals.ssrEntryChunk = chunk;
delete bundle[chunkName];
}
Expand Down
3 changes: 3 additions & 0 deletions packages/astro/src/core/build/static-build.ts
Expand Up @@ -32,6 +32,7 @@ import {
ASTRO_PAGE_EXTENSION_POST_PATTERN,
ASTRO_PAGE_RESOLVED_MODULE_ID,
} from './plugins/plugin-pages.js';
import { SSR_VIRTUAL_MODULE_ID } from './plugins/plugin-ssr.js';

export async function viteBuild(opts: StaticBuildOptions) {
const { allPages, settings } = opts;
Expand Down Expand Up @@ -184,6 +185,8 @@ async function ssrBuild(
.replaceAll('.', '_')
// this must be last
.replace(ASTRO_PAGE_EXTENSION_POST_PATTERN, '.')}.mjs`;
} else if (chunkInfo.facadeModuleId === SSR_VIRTUAL_MODULE_ID) {
return opts.buildConfig.serverEntry;
} else if (chunkInfo.facadeModuleId === RESOLVED_MIDDLEWARE_MODULE_ID) {
return 'middleware.mjs';
} else if (chunkInfo.facadeModuleId === RESOLVED_RENDERERS_MODULE_ID) {
Expand Down

0 comments on commit ce809c5

Please sign in to comment.