diff --git a/packages/astro/package.json b/packages/astro/package.json index 9f4493527..938d534c8 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -14,7 +14,11 @@ "homepage": "https://astro.build", "types": "./dist/types/@types/astro.d.ts", "typesVersions": { - "*": { "app/*": ["./dist/types/core/app/*"] } + "*": { + "app/*": [ + "./dist/types/core/app/*" + ] + } }, "exports": { ".": "./astro.js", diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 9f2668d4f..52c06dff1 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -665,7 +665,7 @@ export interface AstroIntegration { // more generalized. Consider the SSR use-case as well. // injectElement: (stage: vite.HtmlTagDescriptor, element: string) => void; }) => void; - 'astro:config:done'?: (options: {config: AstroConfig, setAdapter: (adapter: AstroAdapter) => void; }) => void | Promise; + 'astro:config:done'?: (options: { config: AstroConfig; setAdapter: (adapter: AstroAdapter) => void }) => void | Promise; 'astro:server:setup'?: (options: { server: vite.ViteDevServer }) => void | Promise; 'astro:server:start'?: (options: { address: AddressInfo }) => void | Promise; 'astro:server:done'?: () => void | Promise; diff --git a/packages/astro/src/adapter-ssg/index.ts b/packages/astro/src/adapter-ssg/index.ts index ca1f7127d..703289d8f 100644 --- a/packages/astro/src/adapter-ssg/index.ts +++ b/packages/astro/src/adapter-ssg/index.ts @@ -17,7 +17,7 @@ export default function createIntegration(): AstroIntegration { }, 'astro:build:start': ({ buildConfig }) => { buildConfig.staticMode = true; - } - } + }, + }, }; } diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 9d3d9f8de..dedba1e4f 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -16,7 +16,6 @@ import { render } from '../render/core.js'; import { createLinkStylesheetElementSet, createModuleScriptElementWithSrcSet } from '../render/ssr-element.js'; import { getOutRoot, getOutFolder, getOutFile } from './common.js'; - // Render is usually compute, which Node.js can't parallelize well. // In real world testing, dropping from 10->1 showed a notiable perf // improvement. In the future, we can revisit a smarter parallel @@ -41,7 +40,6 @@ export function getByFacadeId(facadeId: string, map: Map): T | und ); } - // Throttle the rendering a paths to prevents creating too many Promises on the microtask queue. function* throttle(max: number, inPaths: string[]) { let tmp = []; @@ -150,7 +148,6 @@ interface GeneratePathOptions { renderers: SSRLoadedRenderer[]; } - function addPageName(pathname: string, opts: StaticBuildOptions): void { opts.pageNames.push(pathname.replace(/\/?$/, '/').replace(/^\//, '')); } diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 2ae97ae53..e9dd75478 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -82,7 +82,7 @@ export async function staticBuild(opts: StaticBuildOptions) { // Build your project (SSR application code, assets, client JS, etc.) const ssrResult = (await ssrBuild(opts, internals, pageInput)) as RollupOutput; - if(opts.buildConfig.staticMode) { + if (opts.buildConfig.staticMode) { await generatePages(ssrResult, opts, internals, facadeIdToPageDataMap); await cleanSsrOutput(opts); } else { @@ -128,8 +128,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp }), ...(viteConfig.plugins || []), // SSR needs to be last - opts.astroConfig._ctx.adapter?.serverEntrypoint && - vitePluginSSR(opts, internals, opts.astroConfig._ctx.adapter), + opts.astroConfig._ctx.adapter?.serverEntrypoint && vitePluginSSR(opts, internals, opts.astroConfig._ctx.adapter), ], publicDir: ssr ? false : viteConfig.publicDir, root: viteConfig.root, @@ -187,7 +186,6 @@ async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals, }); } - async function cleanSsrOutput(opts: StaticBuildOptions) { // The SSR output is all .mjs files, the client output is not. const files = await glob('**/*.mjs', { diff --git a/packages/astro/src/core/build/vite-plugin-ssr.ts b/packages/astro/src/core/build/vite-plugin-ssr.ts index ed4ad2284..6ba82a2b7 100644 --- a/packages/astro/src/core/build/vite-plugin-ssr.ts +++ b/packages/astro/src/core/build/vite-plugin-ssr.ts @@ -16,28 +16,32 @@ export function vitePluginSSR(buildOpts: StaticBuildOptions, internals: BuildInt return { name: '@astrojs/vite-plugin-astro-ssr', options(opts) { - if(Array.isArray(opts.input)) { + if (Array.isArray(opts.input)) { opts.input.push(virtualModuleId); } else { return { - input: [virtualModuleId] + input: [virtualModuleId], }; } }, resolveId(id) { - if(id === virtualModuleId) { + if (id === virtualModuleId) { return resolvedVirtualModuleId; } }, load(id) { - if(id === resolvedVirtualModuleId) { + if (id === resolvedVirtualModuleId) { return `import * as adapter from '${adapter.serverEntrypoint}'; import { deserializeManifest as _deserializeManifest } from 'astro/app'; const _manifest = _deserializeManifest('${manifestReplace}'); -${adapter.exports ? `const _exports = adapter.createExports(_manifest); -${adapter.exports.map(name => `export const ${name} = _exports['${name}'];`).join('\n')} -` : ''} +${ + adapter.exports + ? `const _exports = adapter.createExports(_manifest); +${adapter.exports.map((name) => `export const ${name} = _exports['${name}'];`).join('\n')} +` + : '' +} const _start = 'start'; if(_start in adapter) { adapter[_start](_manifest); @@ -45,13 +49,13 @@ if(_start in adapter) { } return void 0; }, - + generateBundle(opts, bundle) { const manifest = buildManifest(bundle, 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, () => { @@ -60,8 +64,8 @@ if(_start in adapter) { chunk.fileName = 'entry.mjs'; } } - } - } + }, + }; } function buildManifest(bundle: OutputBundle, opts: StaticBuildOptions, internals: BuildInternals): SerializedSSRManifest { diff --git a/packages/astro/src/integrations/index.ts b/packages/astro/src/integrations/index.ts index 470b3d032..788a8d179 100644 --- a/packages/astro/src/integrations/index.ts +++ b/packages/astro/src/integrations/index.ts @@ -5,7 +5,7 @@ import { mergeConfig } from '../core/config.js'; import ssgAdapter from '../adapter-ssg/index.js'; export async function runHookConfigSetup({ config: _config, command }: { config: AstroConfig; command: 'dev' | 'build' }): Promise { - if(_config.adapter) { + if (_config.adapter) { _config.integrations.push(_config.adapter); } @@ -36,24 +36,24 @@ export async function runHookConfigDone({ config }: { config: AstroConfig }) { await integration.hooks['astro:config:done']({ config, setAdapter(adapter) { - if(config._ctx.adapter && config._ctx.adapter.name !== adapter.name) { + if (config._ctx.adapter && config._ctx.adapter.name !== adapter.name) { throw new Error(`Adapter already set to ${config._ctx.adapter.name}. You can only have one adapter.`); } config._ctx.adapter = adapter; - } + }, }); } } // Call the default adapter - if(!config._ctx.adapter) { + if (!config._ctx.adapter) { const integration = ssgAdapter(); config.integrations.push(integration); - if(integration.hooks['astro:config:done']) { + if (integration.hooks['astro:config:done']) { await integration.hooks['astro:config:done']({ config, setAdapter(adapter) { config._ctx.adapter = adapter; - } + }, }); } } @@ -83,7 +83,7 @@ export async function runHookServerDone({ config }: { config: AstroConfig }) { } } -export async function runHookBuildStart({ config, buildConfig }: { config: AstroConfig, buildConfig: BuildConfig }) { +export async function runHookBuildStart({ config, buildConfig }: { config: AstroConfig; buildConfig: BuildConfig }) { for (const integration of config.integrations) { if (integration.hooks['astro:build:start']) { await integration.hooks['astro:build:start']({ buildConfig }); diff --git a/packages/astro/test/ssr-dynamic.test.js b/packages/astro/test/ssr-dynamic.test.js index 127cfa65c..30d9fdd11 100644 --- a/packages/astro/test/ssr-dynamic.test.js +++ b/packages/astro/test/ssr-dynamic.test.js @@ -14,13 +14,13 @@ describe('Dynamic pages in SSR', () => { buildOptions: { experimentalSsr: true, }, - adapter: testAdapter() + adapter: testAdapter(), }); await fixture.build(); }); it('Do not have to implement getStaticPaths', async () => { - const {createApp} = await import('./fixtures/ssr-dynamic/dist/server/entry.mjs'); + const { createApp } = await import('./fixtures/ssr-dynamic/dist/server/entry.mjs'); const app = createApp(new URL('./fixtures/ssr-dynamic/dist/server/', import.meta.url)); const request = new Request('http://example.com/123'); const response = await app.render(request); diff --git a/packages/astro/test/test-adapter.js b/packages/astro/test/test-adapter.js index b1efe0f09..2665cc4c1 100644 --- a/packages/astro/test/test-adapter.js +++ b/packages/astro/test/test-adapter.js @@ -1,10 +1,10 @@ import { viteID } from '../dist/core/util.js'; /** - * + * * @returns {import('../src/@types/astro').AstroIntegration} */ -export default function() { +export default function () { return { name: 'my-ssr-adapter', hooks: { @@ -14,30 +14,30 @@ export default function() { plugins: [ { resolveId(id) { - if(id === '@my-ssr') { + if (id === '@my-ssr') { return id; - } else if(id === 'astro/app') { + } else if (id === 'astro/app') { const id = viteID(new URL('../dist/core/app/index.js', import.meta.url)); return id; } }, load(id) { - if(id === '@my-ssr') { + if (id === '@my-ssr') { return `import { App } from 'astro/app';export function createExports(manifest) { return { manifest, createApp: (root) => new App(manifest, root) }; }`; } - } - } + }, + }, ], - } - }) + }, + }); }, 'astro:config:done': ({ setAdapter }) => { setAdapter({ name: 'my-ssr-adapter', serverEntrypoint: '@my-ssr', - exports: ['manifest', 'createApp'] + exports: ['manifest', 'createApp'], }); - } + }, }, - } + }; } diff --git a/packages/integrations/node/src/index.ts b/packages/integrations/node/src/index.ts index 903d5b1cc..b90cd9d2e 100644 --- a/packages/integrations/node/src/index.ts +++ b/packages/integrations/node/src/index.ts @@ -14,7 +14,7 @@ export default function createIntegration(): AstroIntegration { hooks: { 'astro:config:done': ({ setAdapter }) => { setAdapter(getAdapter()); - } - } + }, + }, }; } diff --git a/packages/integrations/node/src/server.ts b/packages/integrations/node/src/server.ts index 791dc58b2..79a51cdfe 100644 --- a/packages/integrations/node/src/server.ts +++ b/packages/integrations/node/src/server.ts @@ -4,7 +4,7 @@ import { NodeApp } from 'astro/app/node'; import { polyfill } from '@astrojs/webapi'; polyfill(globalThis, { - exclude: 'window document' + exclude: 'window document', }); export function createExports(manifest: SSRManifest) { @@ -13,22 +13,22 @@ export function createExports(manifest: SSRManifest) { async handler(req: IncomingMessage, res: ServerResponse, next?: (err?: unknown) => void) { const route = app.match(req); - if(route) { + if (route) { try { const response = await app.render(req); await writeWebResponse(res, response); - } catch(err: unknown) { - if(next) { + } catch (err: unknown) { + if (next) { next(err); } else { throw err; } } - } else if(next) { + } else if (next) { return next(); } - } - } + }, + }; } async function writeWebResponse(res: ServerResponse, webResponse: Response) {