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 type { SSRManifestI18n } from '../app/types.js';
|
||||||
import { NoPrerenderedRoutesWithDomains } from '../errors/errors-data.js';
|
import { NoPrerenderedRoutesWithDomains } from '../errors/errors-data.js';
|
||||||
import { AstroError, AstroErrorData } from '../errors/index.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 { getRedirectLocationOrThrow, routeIsRedirect } from '../redirects/index.js';
|
||||||
import { RenderContext } from '../render-context.js';
|
import { RenderContext } from '../render-context.js';
|
||||||
import { callGetStaticPaths } from '../render/route-cache.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 baseDirectory = getOutputDirectory(options.settings.config);
|
||||||
const renderersEntryUrl = new URL('renderers.mjs', baseDirectory);
|
const renderersEntryUrl = new URL('renderers.mjs', baseDirectory);
|
||||||
const renderers = await import(renderersEntryUrl.toString());
|
const renderers = await import(renderersEntryUrl.toString());
|
||||||
let middleware: MiddlewareHandler = (_, next) => next();
|
const middleware: MiddlewareHandler = internals.middlewareEntryPoint
|
||||||
try {
|
? await import(internals.middlewareEntryPoint.toString()).then((mod) => mod.onRequest)
|
||||||
// middleware.mjs is not emitted if there is no user middleware
|
: NOOP_MIDDLEWARE_FN;
|
||||||
// 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 {}
|
|
||||||
manifest = createBuildManifest(
|
manifest = createBuildManifest(
|
||||||
options.settings,
|
options.settings,
|
||||||
internals,
|
internals,
|
||||||
|
|
|
@ -137,15 +137,13 @@ export class BuildPipeline extends Pipeline {
|
||||||
const renderersEntryUrl = new URL(`renderers.mjs?time=${Date.now()}`, baseDirectory);
|
const renderersEntryUrl = new URL(`renderers.mjs?time=${Date.now()}`, baseDirectory);
|
||||||
const renderers = await import(renderersEntryUrl.toString());
|
const renderers = await import(renderersEntryUrl.toString());
|
||||||
|
|
||||||
const middleware = await import(new URL('middleware.mjs', baseDirectory).toString())
|
const middleware = internals.middlewareEntryPoint
|
||||||
.then((mod) => {
|
? await import(internals.middlewareEntryPoint.toString()).then((mod) => {
|
||||||
return function () {
|
return function () {
|
||||||
return { onRequest: mod.onRequest };
|
return { onRequest: mod.onRequest };
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
// middleware.mjs is not emitted if there is no user middleware
|
: manifest.middleware;
|
||||||
// in which case the import fails with ERR_MODULE_NOT_FOUND, and we fall back to a no-op middleware
|
|
||||||
.catch(() => manifest.middleware);
|
|
||||||
|
|
||||||
if (!renderers) {
|
if (!renderers) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
|
|
@ -13,7 +13,6 @@ export const MIDDLEWARE_MODULE_ID = '\0astro-internal:middleware';
|
||||||
const NOOP_MIDDLEWARE = '\0noop-middleware';
|
const NOOP_MIDDLEWARE = '\0noop-middleware';
|
||||||
|
|
||||||
export function vitePluginMiddleware({ settings }: { settings: AstroSettings }): VitePlugin {
|
export function vitePluginMiddleware({ settings }: { settings: AstroSettings }): VitePlugin {
|
||||||
let isCommandBuild = false;
|
|
||||||
let resolvedMiddlewareId: string | undefined = undefined;
|
let resolvedMiddlewareId: string | undefined = undefined;
|
||||||
const hasIntegrationMiddleware =
|
const hasIntegrationMiddleware =
|
||||||
settings.middlewares.pre.length > 0 || settings.middlewares.post.length > 0;
|
settings.middlewares.pre.length > 0 || settings.middlewares.post.length > 0;
|
||||||
|
@ -21,9 +20,6 @@ export function vitePluginMiddleware({ settings }: { settings: AstroSettings }):
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: '@astro/plugin-middleware',
|
name: '@astro/plugin-middleware',
|
||||||
config(_, { command }) {
|
|
||||||
isCommandBuild = command === 'build';
|
|
||||||
},
|
|
||||||
async resolveId(id) {
|
async resolveId(id) {
|
||||||
if (id === MIDDLEWARE_MODULE_ID) {
|
if (id === MIDDLEWARE_MODULE_ID) {
|
||||||
const middlewareId = await this.resolve(
|
const middlewareId = await this.resolve(
|
||||||
|
@ -53,15 +49,6 @@ export function vitePluginMiddleware({ settings }: { settings: AstroSettings }):
|
||||||
if (!userMiddlewareIsPresent && settings.config.i18n?.routing === 'manual') {
|
if (!userMiddlewareIsPresent && settings.config.i18n?.routing === 'manual') {
|
||||||
throw new AstroError(MissingMiddlewareForInternationalization);
|
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 preMiddleware = createMiddlewareImports(settings.middlewares.pre, 'pre');
|
||||||
const postMiddleware = createMiddlewareImports(settings.middlewares.post, 'post');
|
const postMiddleware = createMiddlewareImports(settings.middlewares.post, 'post');
|
||||||
|
@ -124,7 +111,7 @@ export function vitePluginMiddlewareBuild(
|
||||||
|
|
||||||
writeBundle(_, bundle) {
|
writeBundle(_, bundle) {
|
||||||
for (const [chunkName, chunk] of Object.entries(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);
|
const outputDirectory = getOutputDirectory(opts.settings.config);
|
||||||
internals.middlewareEntryPoint = new URL(chunkName, outputDirectory);
|
internals.middlewareEntryPoint = new URL(chunkName, outputDirectory);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue