Skip to content

Commit

Permalink
refactor(astro): emit entry.mjs to import pages via dynamic import (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed May 19, 2023
1 parent 96947fc commit 852d59a
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-rules-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Emit pages as dynamic import chunks during the build
4 changes: 2 additions & 2 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class App {
defaultStatus = 404;
}

let mod = this.#manifest.pageMap.get(routeData.component)!;
let mod = await this.#manifest.pageMap.get(routeData.component)!();

if (routeData.type === 'page') {
let response = await this.#renderPage(request, routeData, mod, defaultStatus);
Expand All @@ -148,7 +148,7 @@ export class App {
if (response.status === 500) {
const fiveHundredRouteData = matchRoute('/500', this.#manifestData);
if (fiveHundredRouteData) {
mod = this.#manifest.pageMap.get(fiveHundredRouteData.component)!;
mod = await this.#manifest.pageMap.get(fiveHundredRouteData.component)!();
try {
let fiveHundredResponse = await this.#renderPage(
request,
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/app/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface RouteInfo {
export type SerializedRouteInfo = Omit<RouteInfo, 'routeData'> & {
routeData: SerializedRouteData;
};
type ImportComponentInstance = () => Promise<ComponentInstance>;

export interface SSRManifest {
adapterName: string;
Expand All @@ -39,7 +40,7 @@ export interface SSRManifest {
base?: string;
assetsPrefix?: string;
markdown: MarkdownRenderingOptions;
pageMap: Map<ComponentPath, ComponentInstance>;
pageMap: Map<ComponentPath, ImportComponentInstance>;
renderers: SSRLoadedRenderer[];
/**
* Map of directive name (e.g. `load`) to the directive script code
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ async function generatePage(
.map(({ sheet }) => sheet)
.reduce(mergeInlineCss, []);

const pageModule = ssrEntry.pageMap?.get(pageData.component);
const pageModulePromise = ssrEntry.pageMap?.get(pageData.component);
const middleware = ssrEntry.middleware;

if (!pageModule) {
if (!pageModulePromise) {
throw new Error(
`Unable to find the module for ${pageData.component}. This is unexpected and likely a bug in Astro, please report.`
);
}

const pageModule = await pageModulePromise();
if (shouldSkipDraft(pageModule, opts.settings)) {
info(opts.logging, null, `${magenta('⚠️')} Skipping draft ${pageData.route.component}`);
return;
Expand Down
5 changes: 4 additions & 1 deletion packages/astro/src/core/build/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ export function* walkParentInfos(
// Returns true if a module is a top-level page. We determine this based on whether
// it is imported by the top-level virtual module.
export function moduleIsTopLevelPage(info: ModuleInfo): boolean {
return info.importers[0] === resolvedPagesVirtualModuleId;
return (
info.importers[0] === resolvedPagesVirtualModuleId ||
info.dynamicImporters[0] == resolvedPagesVirtualModuleId
);
}

// This function walks the dependency graph, going up until it finds a page component.
Expand Down
4 changes: 3 additions & 1 deletion packages/astro/src/core/build/plugins/plugin-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): V
let i = 0;
for (const pageData of eachPageData(internals)) {
const variable = `_page${i}`;
imports.push(`import * as ${variable} from ${JSON.stringify(pageData.moduleSpecifier)};`);
imports.push(
`const ${variable} = () => import(${JSON.stringify(pageData.moduleSpecifier)});`
);
importMap += `[${JSON.stringify(pageData.component)}, ${variable}],`;
i++;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/build/plugins/plugin-prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { BuildInternals } from '../internal.js';
import type { AstroBuildPlugin } from '../plugin.js';
import type { StaticBuildOptions } from '../types';
import { extendManualChunks } from './util.js';
import path from 'node:path';

function vitePluginPrerender(opts: StaticBuildOptions, internals: BuildInternals): VitePlugin {
return {
Expand All @@ -25,7 +26,7 @@ function vitePluginPrerender(opts: StaticBuildOptions, internals: BuildInternals
}
pageInfo.route.prerender = false;
// dynamic pages should all go in their own chunk in the pages/* directory
return `pages/all`;
return `pages/${path.basename(pageInfo.component)}`;
}
},
});
Expand Down
4 changes: 3 additions & 1 deletion packages/astro/src/core/build/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ export interface StaticBuildOptions {
teardownCompiler: boolean;
}

type ImportComponentInstance = () => Promise<ComponentInstance>;

export interface SingleFileBuiltModule {
pageMap: Map<ComponentPath, ComponentInstance>;
pageMap: Map<ComponentPath, ImportComponentInstance>;
middleware: AstroMiddlewareInstance<unknown>;
renderers: SSRLoadedRenderer[];
}
Expand Down

0 comments on commit 852d59a

Please sign in to comment.