Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix injectRoute #7943

Merged
merged 12 commits into from
Aug 7, 2023
5 changes: 5 additions & 0 deletions .changeset/stupid-pants-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Ensure that injected routes from `node_modules` are properly detected
15 changes: 10 additions & 5 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1316,16 +1316,20 @@ export interface AstroUserConfig {
*/
export type InjectedScriptStage = 'before-hydration' | 'head-inline' | 'page' | 'page-ssr';

/**
* Resolved Astro Config
* Config with user settings along with all defaults filled in.
*/

export interface InjectedRoute {
pattern: string;
entryPoint: string;
prerender?: boolean;
}

export interface ResolvedInjectedRoute extends InjectedRoute {
resolvedEntryPoint?: URL;
}

/**
* Resolved Astro Config
* Config with user settings along with all defaults filled in.
*/
export interface AstroConfig extends z.output<typeof AstroConfigSchema> {
// Public:
// This is a more detailed type than zod validation gives us.
Expand Down Expand Up @@ -1414,6 +1418,7 @@ export interface AstroSettings {
config: AstroConfig;
adapter: AstroAdapter | undefined;
injectedRoutes: InjectedRoute[];
resolvedInjectedRoutes: ResolvedInjectedRoute[];
pageExtensions: string[];
contentEntryTypes: ContentEntryType[];
dataEntryTypes: DataEntryType[];
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/config/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function createBaseSettings(config: AstroConfig): AstroSettings {

adapter: undefined,
injectedRoutes: [],
resolvedInjectedRoutes: [],
pageExtensions: ['.astro', '.html', ...SUPPORTED_MARKDOWN_FILE_EXTENSIONS],
contentEntryTypes: [markdownContentEntryType],
dataEntryTypes: [
Expand Down
5 changes: 3 additions & 2 deletions packages/astro/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ function isInPagesDir(file: URL, config: AstroConfig): boolean {
}

function isInjectedRoute(file: URL, settings: AstroSettings) {
for (const route of settings.injectedRoutes) {
if (file.toString().endsWith(route.entryPoint)) return true;
let fileURL = file.toString();
for (const route of settings.resolvedInjectedRoutes) {
if (route.resolvedEntryPoint && fileURL === route.resolvedEntryPoint.toString()) return true;
}
return false;
}
Expand Down
19 changes: 18 additions & 1 deletion packages/astro/src/vite-plugin-integrations-container/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { Plugin as VitePlugin } from 'vite';
import type { AstroSettings } from '../@types/astro.js';
import type { PluginContext } from 'rollup';
import type { AstroSettings, InjectedRoute, ResolvedInjectedRoute } from '../@types/astro.js';
import type { LogOptions } from '../core/logger/core.js';

import { normalizePath } from 'vite';
import { runHookServerSetup } from '../integrations/index.js';

/** Connect Astro integrations into Vite, as needed. */
Expand All @@ -16,5 +19,19 @@ export default function astroIntegrationsContainerPlugin({
configureServer(server) {
runHookServerSetup({ config: settings.config, server, logging });
},
async buildStart() {
// Ensure the injectedRoutes are all resolved to their final paths through Rollup
settings.resolvedInjectedRoutes = await Promise.all(settings.injectedRoutes.map(route => resolveEntryPoint.call(this, route)))
},
};
}

async function resolveEntryPoint(this: PluginContext, route: InjectedRoute): Promise<ResolvedInjectedRoute> {
const resolvedId = await this.resolve(route.entryPoint)
.then(res => res?.id)
.catch(() => undefined);
if (!resolvedId) return route;

const resolvedEntryPoint = new URL(`file://${normalizePath(resolvedId)}`);
return { ...route, resolvedEntryPoint };
}