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