[ci] format
This commit is contained in:
parent
e4025d1f53
commit
f5b48bc0ae
13 changed files with 46 additions and 50 deletions
|
@ -43,9 +43,9 @@ export class App {
|
||||||
|
|
||||||
const mod = this.#manifest.pageMap.get(routeData.component)!;
|
const mod = this.#manifest.pageMap.get(routeData.component)!;
|
||||||
|
|
||||||
if(routeData.type === 'page') {
|
if (routeData.type === 'page') {
|
||||||
return this.#renderPage(request, routeData, mod);
|
return this.#renderPage(request, routeData, mod);
|
||||||
} else if(routeData.type === 'endpoint') {
|
} else if (routeData.type === 'endpoint') {
|
||||||
return this.#callEndpoint(request, routeData, mod);
|
return this.#callEndpoint(request, routeData, mod);
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Unsupported route type [${routeData.type}].`);
|
throw new Error(`Unsupported route type [${routeData.type}].`);
|
||||||
|
@ -95,8 +95,8 @@ export class App {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'text/html',
|
'Content-Type': 'text/html',
|
||||||
'Content-Length': bytes.byteLength.toString()
|
'Content-Length': bytes.byteLength.toString(),
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,20 +113,20 @@ export class App {
|
||||||
ssr: true,
|
ssr: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
if(result.type === 'response') {
|
if (result.type === 'response') {
|
||||||
return result.response;
|
return result.response;
|
||||||
} else {
|
} else {
|
||||||
const body = result.body;
|
const body = result.body;
|
||||||
const headers = new Headers();
|
const headers = new Headers();
|
||||||
const mimeType = mime.getType(url.pathname);
|
const mimeType = mime.getType(url.pathname);
|
||||||
if(mimeType) {
|
if (mimeType) {
|
||||||
headers.set('Content-Type', mimeType);
|
headers.set('Content-Type', mimeType);
|
||||||
}
|
}
|
||||||
const bytes = this.#encoder.encode(body);
|
const bytes = this.#encoder.encode(body);
|
||||||
headers.set('Content-Length', bytes.byteLength.toString());
|
headers.set('Content-Length', bytes.byteLength.toString());
|
||||||
return new Response(bytes, {
|
return new Response(bytes, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers
|
headers,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,7 +74,7 @@ export async function generatePages(result: RollupOutput, opts: StaticBuildOptio
|
||||||
const ssrEntryURL = new URL(`./entry.mjs?time=${Date.now()}`, outFolder);
|
const ssrEntryURL = new URL(`./entry.mjs?time=${Date.now()}`, outFolder);
|
||||||
const ssrEntry = await import(ssrEntryURL.toString());
|
const ssrEntry = await import(ssrEntryURL.toString());
|
||||||
|
|
||||||
for(const pageData of eachPageData(internals)) {
|
for (const pageData of eachPageData(internals)) {
|
||||||
await generatePage(opts, internals, pageData, ssrEntry);
|
await generatePage(opts, internals, pageData, ssrEntry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,7 +86,7 @@ async function generatePage(
|
||||||
pageData: PageBuildData,
|
pageData: PageBuildData,
|
||||||
ssrEntry: SingleFileBuiltModule
|
ssrEntry: SingleFileBuiltModule
|
||||||
) {
|
) {
|
||||||
let timeStart = performance.now();
|
let timeStart = performance.now();
|
||||||
const renderers = ssrEntry.renderers;
|
const renderers = ssrEntry.renderers;
|
||||||
|
|
||||||
const pageInfo = getPageDataByComponent(internals, pageData.route.component);
|
const pageInfo = getPageDataByComponent(internals, pageData.route.component);
|
||||||
|
@ -95,7 +95,7 @@ async function generatePage(
|
||||||
|
|
||||||
const pageModule = ssrEntry.pageMap.get(pageData.component);
|
const pageModule = ssrEntry.pageMap.get(pageData.component);
|
||||||
|
|
||||||
if(!pageModule) {
|
if (!pageModule) {
|
||||||
throw new Error(`Unable to find the module for ${pageData.component}. This is unexpected and likely a bug in Astro, please report.`);
|
throw new Error(`Unable to find the module for ${pageData.component}. This is unexpected and likely a bug in Astro, please report.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,15 +32,15 @@ export interface BuildInternals {
|
||||||
chunkToReferenceIdMap: Map<string, string>;
|
chunkToReferenceIdMap: Map<string, string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a mapping of pathname to the string source of all collected inline <style> for a page.
|
* This is a mapping of pathname to the string source of all collected inline <style> for a page.
|
||||||
* @deprecated This Map is only used for the legacy build.
|
* @deprecated This Map is only used for the legacy build.
|
||||||
*/
|
*/
|
||||||
astroStyleMap: Map<string, string>;
|
astroStyleMap: Map<string, string>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a virtual JS module that imports all dependent styles for a page.
|
* This is a virtual JS module that imports all dependent styles for a page.
|
||||||
* @deprecated This Map is only used for the legacy build.
|
* @deprecated This Map is only used for the legacy build.
|
||||||
*/
|
*/
|
||||||
astroPageStyleMap: Map<string, string>;
|
astroPageStyleMap: Map<string, string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,30 +82,29 @@ export function trackPageData(internals: BuildInternals, component: string, page
|
||||||
internals.pagesByViteID.set(viteID(componentURL), pageData);
|
internals.pagesByViteID.set(viteID(componentURL), pageData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function* getPageDatasByChunk(internals: BuildInternals, chunk: RenderedChunk): Generator<PageBuildData, void, unknown> {
|
||||||
export function * getPageDatasByChunk(internals: BuildInternals, chunk: RenderedChunk): Generator<PageBuildData, void, unknown> {
|
|
||||||
const pagesByViteID = internals.pagesByViteID;
|
const pagesByViteID = internals.pagesByViteID;
|
||||||
for(const [modulePath] of Object.entries(chunk.modules)) {
|
for (const [modulePath] of Object.entries(chunk.modules)) {
|
||||||
if(pagesByViteID.has(modulePath)) {
|
if (pagesByViteID.has(modulePath)) {
|
||||||
yield pagesByViteID.get(modulePath)!;
|
yield pagesByViteID.get(modulePath)!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPageDataByComponent(internals: BuildInternals, component: string): PageBuildData | undefined {
|
export function getPageDataByComponent(internals: BuildInternals, component: string): PageBuildData | undefined {
|
||||||
if(internals.pagesByComponent.has(component)) {
|
if (internals.pagesByComponent.has(component)) {
|
||||||
return internals.pagesByComponent.get(component);
|
return internals.pagesByComponent.get(component);
|
||||||
}
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPageDataByViteID(internals: BuildInternals, viteid: ViteID): PageBuildData | undefined {
|
export function getPageDataByViteID(internals: BuildInternals, viteid: ViteID): PageBuildData | undefined {
|
||||||
if(internals.pagesByViteID.has(viteid)) {
|
if (internals.pagesByViteID.has(viteid)) {
|
||||||
return internals.pagesByViteID.get(viteid);
|
return internals.pagesByViteID.get(viteid);
|
||||||
}
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function * eachPageData(internals: BuildInternals) {
|
export function* eachPageData(internals: BuildInternals) {
|
||||||
yield * internals.pagesByComponent.values();
|
yield* internals.pagesByComponent.values();
|
||||||
}
|
}
|
||||||
|
|
|
@ -150,8 +150,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
|
||||||
}),
|
}),
|
||||||
...(viteConfig.plugins || []),
|
...(viteConfig.plugins || []),
|
||||||
// SSR needs to be last
|
// SSR needs to be last
|
||||||
isBuildingToSSR(opts.astroConfig) &&
|
isBuildingToSSR(opts.astroConfig) && vitePluginSSR(opts, internals, opts.astroConfig._ctx.adapter!),
|
||||||
vitePluginSSR(opts, internals, opts.astroConfig._ctx.adapter!),
|
|
||||||
],
|
],
|
||||||
publicDir: ssr ? false : viteConfig.publicDir,
|
publicDir: ssr ? false : viteConfig.publicDir,
|
||||||
root: viteConfig.root,
|
root: viteConfig.root,
|
||||||
|
|
|
@ -41,7 +41,7 @@ export function vitePluginHoistedScripts(astroConfig: AstroConfig, internals: Bu
|
||||||
|
|
||||||
const vid = viteID(new URL('.' + pathname, astroConfig.projectRoot));
|
const vid = viteID(new URL('.' + pathname, astroConfig.projectRoot));
|
||||||
const pageInfo = getPageDataByViteID(internals, vid);
|
const pageInfo = getPageDataByViteID(internals, vid);
|
||||||
if(pageInfo) {
|
if (pageInfo) {
|
||||||
pageInfo.hoistedScript = id;
|
pageInfo.hoistedScript = id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
import type { Plugin as VitePlugin } from 'vite';
|
import type { Plugin as VitePlugin } from 'vite';
|
||||||
import type { BuildInternals } from './internal.js';
|
import type { BuildInternals } from './internal.js';
|
||||||
import type { StaticBuildOptions } from './types';
|
import type { StaticBuildOptions } from './types';
|
||||||
|
@ -14,23 +13,23 @@ export function vitePluginPages(opts: StaticBuildOptions, internals: BuildIntern
|
||||||
name: '@astro/plugin-build-pages',
|
name: '@astro/plugin-build-pages',
|
||||||
|
|
||||||
options(options) {
|
options(options) {
|
||||||
if(!isBuildingToSSR(opts.astroConfig)) {
|
if (!isBuildingToSSR(opts.astroConfig)) {
|
||||||
return addRollupInput(options, [virtualModuleId]);
|
return addRollupInput(options, [virtualModuleId]);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
resolveId(id) {
|
resolveId(id) {
|
||||||
if(id === virtualModuleId) {
|
if (id === virtualModuleId) {
|
||||||
return resolvedVirtualModuleId;
|
return resolvedVirtualModuleId;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
load(id) {
|
load(id) {
|
||||||
if(id === resolvedVirtualModuleId) {
|
if (id === resolvedVirtualModuleId) {
|
||||||
let importMap = '';
|
let importMap = '';
|
||||||
let imports = [];
|
let imports = [];
|
||||||
let i = 0;
|
let i = 0;
|
||||||
for(const pageData of eachPageData(internals)) {
|
for (const pageData of eachPageData(internals)) {
|
||||||
const variable = `_page${i}`;
|
const variable = `_page${i}`;
|
||||||
imports.push(`import * as ${variable} from '${pageData.moduleSpecifier}';`);
|
imports.push(`import * as ${variable} from '${pageData.moduleSpecifier}';`);
|
||||||
importMap += `['${pageData.component}', ${variable}],`;
|
importMap += `['${pageData.component}', ${variable}],`;
|
||||||
|
@ -39,10 +38,10 @@ export function vitePluginPages(opts: StaticBuildOptions, internals: BuildIntern
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
let rendererItems = '';
|
let rendererItems = '';
|
||||||
for(const renderer of opts.astroConfig._ctx.renderers) {
|
for (const renderer of opts.astroConfig._ctx.renderers) {
|
||||||
const variable = `_renderer${i}`;
|
const variable = `_renderer${i}`;
|
||||||
imports.push(`import ${variable} from '${renderer.serverEntrypoint}';`);
|
imports.push(`import ${variable} from '${renderer.serverEntrypoint}';`);
|
||||||
rendererItems += `Object.assign(${JSON.stringify(renderer)}, { ssr: ${variable} }),`
|
rendererItems += `Object.assign(${JSON.stringify(renderer)}, { ssr: ${variable} }),`;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,6 +52,6 @@ export const renderers = [${rendererItems}];`;
|
||||||
|
|
||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,10 +53,10 @@ if(_start in adapter) {
|
||||||
|
|
||||||
generateBundle(opts, bundle) {
|
generateBundle(opts, bundle) {
|
||||||
const manifest = buildManifest(buildOpts, internals);
|
const manifest = buildManifest(buildOpts, internals);
|
||||||
|
|
||||||
for(const [_chunkName, chunk] of Object.entries(bundle)) {
|
for (const [_chunkName, chunk] of Object.entries(bundle)) {
|
||||||
if(chunk.type === 'asset') continue;
|
if (chunk.type === 'asset') continue;
|
||||||
if(chunk.modules[resolvedVirtualModuleId]) {
|
if (chunk.modules[resolvedVirtualModuleId]) {
|
||||||
const exp = new RegExp(`['"]${manifestReplace}['"]`);
|
const exp = new RegExp(`['"]${manifestReplace}['"]`);
|
||||||
const code = chunk.code;
|
const code = chunk.code;
|
||||||
chunk.code = code.replace(exp, () => {
|
chunk.code = code.replace(exp, () => {
|
||||||
|
@ -73,9 +73,9 @@ function buildManifest(opts: StaticBuildOptions, internals: BuildInternals): Ser
|
||||||
|
|
||||||
const routes: SerializedRouteInfo[] = [];
|
const routes: SerializedRouteInfo[] = [];
|
||||||
|
|
||||||
for(const pageData of eachPageData(internals)) {
|
for (const pageData of eachPageData(internals)) {
|
||||||
const scripts = Array.from(pageData.scripts);
|
const scripts = Array.from(pageData.scripts);
|
||||||
if(pageData.hoistedScript) {
|
if (pageData.hoistedScript) {
|
||||||
scripts.unshift(pageData.hoistedScript);
|
scripts.unshift(pageData.hoistedScript);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -239,4 +239,3 @@ This file is in BETA. Please test and contribute to the discussion:
|
||||||
</html>
|
</html>
|
||||||
</xsl:template>
|
</xsl:template>
|
||||||
</xsl:stylesheet>`;
|
</xsl:stylesheet>`;
|
||||||
|
|
||||||
|
|
|
@ -138,7 +138,7 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
|
||||||
internals.chunkToReferenceIdMap.set(chunk.fileName, referenceId);
|
internals.chunkToReferenceIdMap.set(chunk.fileName, referenceId);
|
||||||
if (chunk.type === 'chunk') {
|
if (chunk.type === 'chunk') {
|
||||||
const fileName = this.getFileName(referenceId);
|
const fileName = this.getFileName(referenceId);
|
||||||
for(const pageData of getPageDatasByChunk(internals, chunk)) {
|
for (const pageData of getPageDatasByChunk(internals, chunk)) {
|
||||||
pageData.css.add(fileName);
|
pageData.css.add(fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -160,7 +160,7 @@ export function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin {
|
||||||
if (chunk.type === 'chunk') {
|
if (chunk.type === 'chunk') {
|
||||||
// This find shared chunks of CSS and adds them to the main CSS chunks,
|
// This find shared chunks of CSS and adds them to the main CSS chunks,
|
||||||
// so that shared CSS is added to the page.
|
// so that shared CSS is added to the page.
|
||||||
for(const { css: cssSet } of getPageDatasByChunk(internals, chunk)) {
|
for (const { css: cssSet } of getPageDatasByChunk(internals, chunk)) {
|
||||||
for (const imp of chunk.imports) {
|
for (const imp of chunk.imports) {
|
||||||
if (internals.chunkToReferenceIdMap.has(imp) && !pureChunkFilenames.has(imp)) {
|
if (internals.chunkToReferenceIdMap.has(imp) && !pureChunkFilenames.has(imp)) {
|
||||||
const referenceId = internals.chunkToReferenceIdMap.get(imp)!;
|
const referenceId = internals.chunkToReferenceIdMap.get(imp)!;
|
||||||
|
|
|
@ -13,7 +13,7 @@ describe('API routes in SSR', () => {
|
||||||
buildOptions: {
|
buildOptions: {
|
||||||
experimentalSsr: true,
|
experimentalSsr: true,
|
||||||
},
|
},
|
||||||
adapter: testAdapter()
|
adapter: testAdapter(),
|
||||||
});
|
});
|
||||||
await fixture.build();
|
await fixture.build();
|
||||||
});
|
});
|
||||||
|
|
|
@ -22,7 +22,7 @@ export default function () {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
load(id) {
|
load(id) {
|
||||||
if(id === '@my-ssr') {
|
if (id === '@my-ssr') {
|
||||||
return `import { App } from 'astro/app';export function createExports(manifest) { return { manifest, createApp: () => new App(manifest) }; }`;
|
return `import { App } from 'astro/app';export function createExports(manifest) { return { manifest, createApp: () => new App(manifest) }; }`;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -89,9 +89,9 @@ export async function loadFixture(inlineConfig) {
|
||||||
clean: () => fs.promises.rm(config.dist, { maxRetries: 10, recursive: true, force: true }),
|
clean: () => fs.promises.rm(config.dist, { maxRetries: 10, recursive: true, force: true }),
|
||||||
loadTestAdapterApp: async () => {
|
loadTestAdapterApp: async () => {
|
||||||
const url = new URL('./server/entry.mjs', config.dist);
|
const url = new URL('./server/entry.mjs', config.dist);
|
||||||
const {createApp} = await import(url);
|
const { createApp } = await import(url);
|
||||||
return createApp();
|
return createApp();
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ async function writeWebResponse(res: ServerResponse, webResponse: Response) {
|
||||||
const { status, headers, body } = webResponse;
|
const { status, headers, body } = webResponse;
|
||||||
res.writeHead(status, Object.fromEntries(headers.entries()));
|
res.writeHead(status, Object.fromEntries(headers.entries()));
|
||||||
if (body) {
|
if (body) {
|
||||||
for await(const chunk of (body as unknown as Readable)) {
|
for await (const chunk of body as unknown as Readable) {
|
||||||
res.write(chunk);
|
res.write(chunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue