[ci] format
This commit is contained in:
parent
5e52814d97
commit
99414f76cf
11 changed files with 58 additions and 55 deletions
|
@ -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",
|
||||
|
|
|
@ -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<void>;
|
||||
'astro:config:done'?: (options: { config: AstroConfig; setAdapter: (adapter: AstroAdapter) => void }) => void | Promise<void>;
|
||||
'astro:server:setup'?: (options: { server: vite.ViteDevServer }) => void | Promise<void>;
|
||||
'astro:server:start'?: (options: { address: AddressInfo }) => void | Promise<void>;
|
||||
'astro:server:done'?: () => void | Promise<void>;
|
||||
|
|
|
@ -17,7 +17,7 @@ export default function createIntegration(): AstroIntegration {
|
|||
},
|
||||
'astro:build:start': ({ buildConfig }) => {
|
||||
buildConfig.staticMode = true;
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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<T>(facadeId: string, map: Map<string, T>): 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(/^\//, ''));
|
||||
}
|
||||
|
|
|
@ -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', {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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<AstroConfig> {
|
||||
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 });
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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'],
|
||||
});
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ export default function createIntegration(): AstroIntegration {
|
|||
hooks: {
|
||||
'astro:config:done': ({ setAdapter }) => {
|
||||
setAdapter(getAdapter());
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue