mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
refactor(build): don't emit middleware.mjs
(#12479)
This commit is contained in:
parent
c3b7e7cfa1
commit
ab0580bc94
3 changed files with 12 additions and 31 deletions
|
@ -33,6 +33,7 @@ import { getOutputDirectory } from '../../prerender/utils.js';
|
|||
import type { SSRManifestI18n } from '../app/types.js';
|
||||
import { NoPrerenderedRoutesWithDomains } from '../errors/errors-data.js';
|
||||
import { AstroError, AstroErrorData } from '../errors/index.js';
|
||||
import { NOOP_MIDDLEWARE_FN } from '../middleware/noop-middleware.js';
|
||||
import { getRedirectLocationOrThrow, routeIsRedirect } from '../redirects/index.js';
|
||||
import { RenderContext } from '../render-context.js';
|
||||
import { callGetStaticPaths } from '../render/route-cache.js';
|
||||
|
@ -65,14 +66,9 @@ export async function generatePages(options: StaticBuildOptions, internals: Buil
|
|||
const baseDirectory = getOutputDirectory(options.settings.config);
|
||||
const renderersEntryUrl = new URL('renderers.mjs', baseDirectory);
|
||||
const renderers = await import(renderersEntryUrl.toString());
|
||||
let middleware: MiddlewareHandler = (_, next) => next();
|
||||
try {
|
||||
// middleware.mjs is not emitted if there is no user middleware
|
||||
// in which case the import fails with ERR_MODULE_NOT_FOUND, and we fall back to a no-op middleware
|
||||
middleware = await import(new URL('middleware.mjs', baseDirectory).toString()).then(
|
||||
(mod) => mod.onRequest,
|
||||
);
|
||||
} catch {}
|
||||
const middleware: MiddlewareHandler = internals.middlewareEntryPoint
|
||||
? await import(internals.middlewareEntryPoint.toString()).then((mod) => mod.onRequest)
|
||||
: NOOP_MIDDLEWARE_FN;
|
||||
manifest = createBuildManifest(
|
||||
options.settings,
|
||||
internals,
|
||||
|
|
|
@ -137,15 +137,13 @@ export class BuildPipeline extends Pipeline {
|
|||
const renderersEntryUrl = new URL(`renderers.mjs?time=${Date.now()}`, baseDirectory);
|
||||
const renderers = await import(renderersEntryUrl.toString());
|
||||
|
||||
const middleware = await import(new URL('middleware.mjs', baseDirectory).toString())
|
||||
.then((mod) => {
|
||||
return function () {
|
||||
return { onRequest: mod.onRequest };
|
||||
};
|
||||
})
|
||||
// middleware.mjs is not emitted if there is no user middleware
|
||||
// in which case the import fails with ERR_MODULE_NOT_FOUND, and we fall back to a no-op middleware
|
||||
.catch(() => manifest.middleware);
|
||||
const middleware = internals.middlewareEntryPoint
|
||||
? await import(internals.middlewareEntryPoint.toString()).then((mod) => {
|
||||
return function () {
|
||||
return { onRequest: mod.onRequest };
|
||||
};
|
||||
})
|
||||
: manifest.middleware;
|
||||
|
||||
if (!renderers) {
|
||||
throw new Error(
|
||||
|
|
|
@ -13,7 +13,6 @@ export const MIDDLEWARE_MODULE_ID = '\0astro-internal:middleware';
|
|||
const NOOP_MIDDLEWARE = '\0noop-middleware';
|
||||
|
||||
export function vitePluginMiddleware({ settings }: { settings: AstroSettings }): VitePlugin {
|
||||
let isCommandBuild = false;
|
||||
let resolvedMiddlewareId: string | undefined = undefined;
|
||||
const hasIntegrationMiddleware =
|
||||
settings.middlewares.pre.length > 0 || settings.middlewares.post.length > 0;
|
||||
|
@ -21,9 +20,6 @@ export function vitePluginMiddleware({ settings }: { settings: AstroSettings }):
|
|||
|
||||
return {
|
||||
name: '@astro/plugin-middleware',
|
||||
config(_, { command }) {
|
||||
isCommandBuild = command === 'build';
|
||||
},
|
||||
async resolveId(id) {
|
||||
if (id === MIDDLEWARE_MODULE_ID) {
|
||||
const middlewareId = await this.resolve(
|
||||
|
@ -53,15 +49,6 @@ export function vitePluginMiddleware({ settings }: { settings: AstroSettings }):
|
|||
if (!userMiddlewareIsPresent && settings.config.i18n?.routing === 'manual') {
|
||||
throw new AstroError(MissingMiddlewareForInternationalization);
|
||||
}
|
||||
// In the build, tell Vite to emit this file
|
||||
if (isCommandBuild) {
|
||||
this.emitFile({
|
||||
type: 'chunk',
|
||||
preserveSignature: 'strict',
|
||||
fileName: 'middleware.mjs',
|
||||
id,
|
||||
});
|
||||
}
|
||||
|
||||
const preMiddleware = createMiddlewareImports(settings.middlewares.pre, 'pre');
|
||||
const postMiddleware = createMiddlewareImports(settings.middlewares.post, 'post');
|
||||
|
@ -124,7 +111,7 @@ export function vitePluginMiddlewareBuild(
|
|||
|
||||
writeBundle(_, bundle) {
|
||||
for (const [chunkName, chunk] of Object.entries(bundle)) {
|
||||
if (chunk.type !== 'asset' && chunk.fileName === 'middleware.mjs') {
|
||||
if (chunk.type !== 'asset' && chunk.facadeModuleId === MIDDLEWARE_MODULE_ID) {
|
||||
const outputDirectory = getOutputDirectory(opts.settings.config);
|
||||
internals.middlewareEntryPoint = new URL(chunkName, outputDirectory);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue