-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create virtual module for middleware (#7132)
- Loading branch information
Showing
5 changed files
with
80 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Emit middleware as an entrypoint during build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/astro/src/core/build/plugins/plugin-middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { Plugin as VitePlugin } from 'vite'; | ||
import { MIDDLEWARE_PATH_SEGMENT_NAME } from '../../constants.js'; | ||
import { addRollupInput } from '../add-rollup-input.js'; | ||
import type { BuildInternals } from '../internal.js'; | ||
import type { AstroBuildPlugin } from '../plugin'; | ||
import type { StaticBuildOptions } from '../types'; | ||
|
||
export const MIDDLEWARE_MODULE_ID = '@astro-middleware'; | ||
export const RESOLVED_MIDDLEWARE_MODULE_ID = '\0@astro-middleware'; | ||
|
||
let inputs: Set<string> = new Set(); | ||
export function vitePluginMiddleware( | ||
opts: StaticBuildOptions, | ||
_internals: BuildInternals | ||
): VitePlugin { | ||
return { | ||
name: '@astro/plugin-middleware', | ||
options(options) { | ||
if (opts.settings.config.experimental.middleware) { | ||
return addRollupInput(options, [MIDDLEWARE_MODULE_ID]); | ||
} | ||
}, | ||
|
||
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( | ||
`${opts.settings.config.srcDir.pathname}/${MIDDLEWARE_PATH_SEGMENT_NAME}` | ||
); | ||
if (middlewareId) { | ||
imports.push(`import { onRequest } from "${middlewareId.id}"`); | ||
exports.push(`export { onRequest }`); | ||
} | ||
const result = [imports.join('\n'), exports.join('\n')]; | ||
|
||
return result.join('\n'); | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
export function pluginMiddleware( | ||
opts: StaticBuildOptions, | ||
internals: BuildInternals | ||
): AstroBuildPlugin { | ||
return { | ||
build: 'ssr', | ||
hooks: { | ||
'build:before': () => { | ||
return { | ||
vitePlugin: vitePluginMiddleware(opts, internals), | ||
}; | ||
}, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters