Skip to content

Commit

Permalink
chore: check if physical file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed May 23, 2023
1 parent 4217953 commit 4252020
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
20 changes: 14 additions & 6 deletions packages/astro/src/core/build/plugins/plugin-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,32 @@ import { addRollupInput } from '../add-rollup-input.js';
import type { BuildInternals } from '../internal.js';
import type { AstroBuildPlugin } from '../plugin';
import type { StaticBuildOptions } from '../types';
import { existsSync } from 'node:fs';

export const MIDDLEWARE_MODULE_ID = '@astro-middleware';
export const RESOLVED_MIDDLEWARE_MODULE_ID = '\0@astro-middleware';

let inputs: Set<string> = new Set();

function middlewareFileExists(opts: StaticBuildOptions): boolean {
const middlewareFile = `${opts.settings.config.srcDir.pathname}/${MIDDLEWARE_PATH_SEGMENT_NAME}`;
return existsSync(middlewareFile);
}

export function vitePluginMiddleware(
opts: StaticBuildOptions,
_internals: BuildInternals
): VitePlugin {
return {
name: '@astro/plugin-middleware',
options(options) {
return addRollupInput(options, [MIDDLEWARE_MODULE_ID]);
if (middlewareFileExists(opts)) {
return addRollupInput(options, [MIDDLEWARE_MODULE_ID]);
}
},

resolveId(id) {
if (id === MIDDLEWARE_MODULE_ID) {
if (id === MIDDLEWARE_MODULE_ID && middlewareFileExists(opts)) {
return RESOLVED_MIDDLEWARE_MODULE_ID;
}
},
Expand All @@ -33,12 +42,11 @@ export function vitePluginMiddleware(
`${opts.settings.config.srcDir.pathname}/${MIDDLEWARE_PATH_SEGMENT_NAME}`
);
if (middlewareId) {
imports.push(`import { onRequest } from "${middlewareId.id}"`);
imports.push(`import { onRequest } from "${middlewareId.id}";`);
exports.push(`export { onRequest }`);
}
const result = [imports.join('\n'), exports.join('\n')];

return result.join('\n');
return `${imports.join('\n')}\n${exports.join('\n')}`;
}
}
},
};
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/build/plugins/plugin-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): V

let middlewareModule = await this.resolve(MIDDLEWARE_MODULE_ID);
if (middlewareModule) {
let what = await this.load(middlewareModule);
imports.push(`import * as _middleware from "${middlewareModule.id}";`);
exports.push(`export const middleware = _middleware;`);
}
Expand Down
9 changes: 7 additions & 2 deletions packages/astro/src/core/build/plugins/plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getOutFile, getOutFolder } from '../common.js';
import { cssOrder, mergeInlineCss, type BuildInternals } from '../internal.js';
import type { AstroBuildPlugin } from '../plugin';
import { RENDERERS_MODULE_ID } from './plugin-renderers.js';
import { MIDDLEWARE_MODULE_ID } from './plugin-middleware.js';

export const virtualModuleId = '@astrojs-ssr-virtual-entry';
const resolvedVirtualModuleId = '\0' + virtualModuleId;
Expand All @@ -38,9 +39,13 @@ function vitePluginSSR(
return resolvedVirtualModuleId;
}
},
load(id) {
async load(id) {
if (id === resolvedVirtualModuleId) {
const middleware = 'middleware: _main.middleware';
let middleware = '';
const middlewareModule = await this.resolve(MIDDLEWARE_MODULE_ID);
if (middlewareModule) {
middleware = 'middleware: _main.middleware';
}
return `import * as adapter from '${adapter.serverEntrypoint}';
import { renderers } from '${RENDERERS_MODULE_ID}';
import * as _main from '${pagesVirtualModuleId}';
Expand Down

0 comments on commit 4252020

Please sign in to comment.