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