Skip to content

Commit

Permalink
Fix injectRoute (#7943)
Browse files Browse the repository at this point in the history
* fix: inject route! hack!

* refactor: use integration container to resolve all injected routes

* chore: add changeset

* Fix pnpm workspace injectRoute bug

See #7561 (comment)

Closes #7561

* Revert "Fix pnpm workspace injectRoute bug"

This reverts commit 3082c7c.

* Update .changeset/stupid-pants-press.md

Co-authored-by: Chris Swithinbank <[email protected]>

* Update packages/astro/src/vite-plugin-scripts/page-ssr.ts

Co-authored-by: Chris Swithinbank <[email protected]>

* refactor: cleanup injectedRoute resolution logic

---------

Co-authored-by: Chris Swithinbank <[email protected]>
  • Loading branch information
natemoo-re and delucis authored Aug 7, 2023
1 parent 1a24ea6 commit c2682a1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
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 };
}

0 comments on commit c2682a1

Please sign in to comment.