From f7335c76a7f8a8251db363c7103624f286ee8d3a Mon Sep 17 00:00:00 2001 From: matthewp Date: Tue, 3 May 2022 13:08:46 +0000 Subject: [PATCH] [ci] format --- packages/astro/src/cli/telemetry.ts | 1 - packages/astro/src/core/render/result.ts | 2 +- .../astro/src/runtime/server/astro-island.ts | 2 +- .../astro/src/runtime/server/hydration.ts | 17 +- packages/astro/src/runtime/server/index.ts | 13 +- packages/telemetry/src/events/session.ts | 46 ++-- packages/telemetry/test/session-event.test.js | 208 +++++++++++------- 7 files changed, 178 insertions(+), 111 deletions(-) diff --git a/packages/astro/src/cli/telemetry.ts b/packages/astro/src/cli/telemetry.ts index 147e405ab..ff30b35d8 100644 --- a/packages/astro/src/cli/telemetry.ts +++ b/packages/astro/src/cli/telemetry.ts @@ -44,4 +44,3 @@ export async function update(subcommand: string, { flags, telemetry }: Telemetry } } } - diff --git a/packages/astro/src/core/render/result.ts b/packages/astro/src/core/render/result.ts index 27fc652d8..1bee4ab77 100644 --- a/packages/astro/src/core/render/result.ts +++ b/packages/astro/src/core/render/result.ts @@ -201,7 +201,7 @@ ${extra}` _metadata: { renderers, pathname, - needsHydrationStyles: false + needsHydrationStyles: false, }, }; diff --git a/packages/astro/src/runtime/server/astro-island.ts b/packages/astro/src/runtime/server/astro-island.ts index 3eadeb1f5..8cb573cf8 100644 --- a/packages/astro/src/runtime/server/astro-island.ts +++ b/packages/astro/src/runtime/server/astro-island.ts @@ -29,7 +29,7 @@ customElements.define('astro-island', class extends HTMLElement { * This is a minified version of the above. If you modify the above you need to * copy/paste it into a .js file and then run: * > node_modules/.bin/terser --mangle --compress -- file.js - * + * * And copy/paste the result below */ export const islandScript = `customElements.define("astro-island",class extends HTMLElement{async connectedCallback(){const[{default:t}]=await Promise.all([import(this.getAttribute("directive-url")),import(this.getAttribute("before-hydration-url"))]);const e=JSON.parse(this.getAttribute("opts"));t(this,e,(async()=>{const t=this.getAttribute("props");const e=t?JSON.parse(t):{};const r=this.getAttribute("renderer-url");const[{default:s},{default:i}]=await Promise.all([import(this.getAttribute("component-url")),r?import(r):()=>()=>{}]);return(t,r)=>i(t)(s,e,r)}))}});`; diff --git a/packages/astro/src/runtime/server/hydration.ts b/packages/astro/src/runtime/server/hydration.ts index f29379b10..04d678c3f 100644 --- a/packages/astro/src/runtime/server/hydration.ts +++ b/packages/astro/src/runtime/server/hydration.ts @@ -4,7 +4,6 @@ import { hydrationSpecifier, serializeListValue } from './util.js'; import { escapeHTML } from './escape.js'; import serializeJavaScript from 'serialize-javascript'; - // Serializes props passed into a component so that they can be reused during hydration. // The value is any export function serializeProps(value: any) { @@ -116,25 +115,27 @@ export async function generateHydrateScript( children: '', props: { // This is for HMR, probably can avoid it in prod - uid: astroId - } + uid: astroId, + }, }; // Add component url island.props['component-url'] = await result.resolve(componentUrl); // Add renderer url - if(renderer.clientEntrypoint) { + if (renderer.clientEntrypoint) { island.props['renderer-url'] = await result.resolve(renderer.clientEntrypoint); island.props['props'] = escapeHTML(serializeProps(props)); } island.props['directive-url'] = await result.resolve(hydrationSpecifier(hydrate)); island.props['before-hydration-url'] = await result.resolve('astro:scripts/before-hydration.js'); - island.props['opts'] = escapeHTML(JSON.stringify({ - name: metadata.displayName, - value: metadata.hydrateArgs || '' - })) + island.props['opts'] = escapeHTML( + JSON.stringify({ + name: metadata.displayName, + value: metadata.hydrateArgs || '', + }) + ); return island; } diff --git a/packages/astro/src/runtime/server/index.ts b/packages/astro/src/runtime/server/index.ts index 69c97144b..aa2d1d574 100644 --- a/packages/astro/src/runtime/server/index.ts +++ b/packages/astro/src/runtime/server/index.ts @@ -321,27 +321,22 @@ If you're still stuck, please open an issue on GitHub or join us at https://astr const needsAstroTemplate = children && !/<\/?astro-fragment\>/.test(html); const template = needsAstroTemplate ? `` : ''; - if(needsAstroTemplate) { + if (needsAstroTemplate) { island.props.tmpl = ''; } - island.children = `${ - html ?? '' - }${template}`; + island.children = `${html ?? ''}${template}`; // Add the astro-island definition only once. Since the SSRResult object // is scoped to a page renderer we can use it as a key to know if the script // has been rendered or not. let script = ''; - if(!resultsWithHydrationScript.has(result)) { + if (!resultsWithHydrationScript.has(result)) { resultsWithHydrationScript.add(result); script = ``; } - return markHTMLString( - script + - renderElement('astro-island', island, false) - ); + return markHTMLString(script + renderElement('astro-island', island, false)); } /** Create the Astro.fetchContent() runtime function. */ diff --git a/packages/telemetry/src/events/session.ts b/packages/telemetry/src/events/session.ts index fce5976aa..ee1daca89 100644 --- a/packages/telemetry/src/events/session.ts +++ b/packages/telemetry/src/events/session.ts @@ -45,24 +45,37 @@ function getExperimentalFeatures(astroConfig?: Record): string[] | }, [] as string[]); } -const secondLevelViteKeys = new Set(["resolve", "css", "json", "server", "server.fs", "build", "preview", "optimizeDeps", "ssr", "worker"]); +const secondLevelViteKeys = new Set([ + 'resolve', + 'css', + 'json', + 'server', + 'server.fs', + 'build', + 'preview', + 'optimizeDeps', + 'ssr', + 'worker', +]); function viteConfigKeys(obj: Record | undefined, parentKey: string): string[] { - if(!obj) { + if (!obj) { return []; } - return Object.entries(obj).map(([key, value]) => { - if(typeof value === 'object' && !Array.isArray(value)) { - const localKey = parentKey ? parentKey + '.' + key : key; - if(secondLevelViteKeys.has(localKey)) { - let keys = viteConfigKeys(value, localKey).map(subkey => key + '.' + subkey); - keys.unshift(key); - return keys; + return Object.entries(obj) + .map(([key, value]) => { + if (typeof value === 'object' && !Array.isArray(value)) { + const localKey = parentKey ? parentKey + '.' + key : key; + if (secondLevelViteKeys.has(localKey)) { + let keys = viteConfigKeys(value, localKey).map((subkey) => key + '.' + subkey); + keys.unshift(key); + return keys; + } } - } - return key; - }).flat(1); + return key; + }) + .flat(1); } export function eventCliSession( @@ -79,11 +92,10 @@ export function eventCliSession( config: astroConfig ? { hasViteConfig: Object.keys(astroConfig?.vite).length > 0, - markdownPlugins: - [ - astroConfig?.markdown?.remarkPlugins ?? [], - astroConfig?.markdown?.rehypePlugins ?? [], - ].flat(1), + markdownPlugins: [ + astroConfig?.markdown?.remarkPlugins ?? [], + astroConfig?.markdown?.rehypePlugins ?? [], + ].flat(1), hasBase: astroConfig?.base !== '/', viteKeys: viteConfigKeys(astroConfig?.vite, ''), adapter: astroConfig?.adapter?.name ?? null, diff --git a/packages/telemetry/test/session-event.test.js b/packages/telemetry/test/session-event.test.js index 5a23bade8..354ed2878 100644 --- a/packages/telemetry/test/session-event.test.js +++ b/packages/telemetry/test/session-event.test.js @@ -17,32 +17,45 @@ describe('Session event', () => { a: 'b', }, publicDir: 'some/dir', - } + }, }); - const [{ payload }] = events.eventCliSession({ - cliCommand: 'dev', - astroVersion: '0.0.0' - }, config); - expect(payload.config.viteKeys).is.deep.equal(['css', 'css.modules', 'base', 'mode', 'define', 'publicDir']); - }) + const [{ payload }] = events.eventCliSession( + { + cliCommand: 'dev', + astroVersion: '0.0.0', + }, + config + ); + expect(payload.config.viteKeys).is.deep.equal([ + 'css', + 'css.modules', + 'base', + 'mode', + 'define', + 'publicDir', + ]); + }); it('vite.resolve keys are captured', async () => { const config = await mockConfig({ vite: { resolve: { alias: { - a: 'b' + a: 'b', }, - dedupe: ['one', 'two'] - } - } + dedupe: ['one', 'two'], + }, + }, }); - const [{ payload }] = events.eventCliSession({ - cliCommand: 'dev', - astroVersion: '0.0.0' - }, config); + const [{ payload }] = events.eventCliSession( + { + cliCommand: 'dev', + astroVersion: '0.0.0', + }, + config + ); expect(payload.config.viteKeys).is.deep.equal(['resolve', 'resolve.alias', 'resolve.dedupe']); }); @@ -50,20 +63,29 @@ describe('Session event', () => { const config = await mockConfig({ vite: { resolve: { - dedupe: ['one', 'two'] + dedupe: ['one', 'two'], }, css: { modules: [], - postcss: {} - } - } + postcss: {}, + }, + }, }); - const [{ payload }] = events.eventCliSession({ - cliCommand: 'dev', - astroVersion: '0.0.0' - }, config); - expect(payload.config.viteKeys).is.deep.equal(['resolve', 'resolve.dedupe', 'css', 'css.modules', 'css.postcss']); + const [{ payload }] = events.eventCliSession( + { + cliCommand: 'dev', + astroVersion: '0.0.0', + }, + config + ); + expect(payload.config.viteKeys).is.deep.equal([ + 'resolve', + 'resolve.dedupe', + 'css', + 'css.modules', + 'css.postcss', + ]); }); it('vite.server keys are captured', async () => { @@ -74,17 +96,27 @@ describe('Session event', () => { open: true, fs: { strict: true, - allow: ['a', 'b'] - } - } - } + allow: ['a', 'b'], + }, + }, + }, }); - const [{ payload }] = events.eventCliSession({ - cliCommand: 'dev', - astroVersion: '0.0.0' - }, config); - expect(payload.config.viteKeys).is.deep.equal(['server', 'server.host', 'server.open', 'server.fs', 'server.fs.strict', 'server.fs.allow']); + const [{ payload }] = events.eventCliSession( + { + cliCommand: 'dev', + astroVersion: '0.0.0', + }, + config + ); + expect(payload.config.viteKeys).is.deep.equal([ + 'server', + 'server.host', + 'server.open', + 'server.fs', + 'server.fs.strict', + 'server.fs.allow', + ]); }); it('vite.build keys are captured', async () => { @@ -94,20 +126,27 @@ describe('Session event', () => { target: 'one', outDir: 'some/dir', cssTarget: { - one: 'two' - } - } - } + one: 'two', + }, + }, + }, }); - const [{ payload }] = events.eventCliSession({ - cliCommand: 'dev', - astroVersion: '0.0.0' - }, config); - expect(payload.config.viteKeys).is.deep.equal(['build', 'build.target', 'build.outDir', 'build.cssTarget']); + const [{ payload }] = events.eventCliSession( + { + cliCommand: 'dev', + astroVersion: '0.0.0', + }, + config + ); + expect(payload.config.viteKeys).is.deep.equal([ + 'build', + 'build.target', + 'build.outDir', + 'build.cssTarget', + ]); }); - it('vite.preview keys are captured', async () => { const config = await mockConfig({ vite: { @@ -115,17 +154,25 @@ describe('Session event', () => { host: 'example.com', port: 8080, another: { - a: 'b' - } - } - } + a: 'b', + }, + }, + }, }); - const [{ payload }] = events.eventCliSession({ - cliCommand: 'dev', - astroVersion: '0.0.0' - }, config); - expect(payload.config.viteKeys).is.deep.equal(['preview', 'preview.host', 'preview.port', 'preview.another']); + const [{ payload }] = events.eventCliSession( + { + cliCommand: 'dev', + astroVersion: '0.0.0', + }, + config + ); + expect(payload.config.viteKeys).is.deep.equal([ + 'preview', + 'preview.host', + 'preview.port', + 'preview.another', + ]); }); it('vite.optimizeDeps keys are captured', async () => { @@ -133,16 +180,23 @@ describe('Session event', () => { vite: { optimizeDeps: { entries: ['one', 'two'], - exclude: ['secret', 'name'] - } - } + exclude: ['secret', 'name'], + }, + }, }); - const [{ payload }] = events.eventCliSession({ - cliCommand: 'dev', - astroVersion: '0.0.0' - }, config); - expect(payload.config.viteKeys).is.deep.equal(['optimizeDeps', 'optimizeDeps.entries', 'optimizeDeps.exclude']); + const [{ payload }] = events.eventCliSession( + { + cliCommand: 'dev', + astroVersion: '0.0.0', + }, + config + ); + expect(payload.config.viteKeys).is.deep.equal([ + 'optimizeDeps', + 'optimizeDeps.entries', + 'optimizeDeps.exclude', + ]); }); it('vite.ssr keys are captured', async () => { @@ -150,15 +204,18 @@ describe('Session event', () => { vite: { ssr: { external: ['a'], - target: { one: 'two' } - } - } + target: { one: 'two' }, + }, + }, }); - const [{ payload }] = events.eventCliSession({ - cliCommand: 'dev', - astroVersion: '0.0.0' - }, config); + const [{ payload }] = events.eventCliSession( + { + cliCommand: 'dev', + astroVersion: '0.0.0', + }, + config + ); expect(payload.config.viteKeys).is.deep.equal(['ssr', 'ssr.external', 'ssr.target']); }); @@ -167,15 +224,18 @@ describe('Session event', () => { vite: { worker: { format: { a: 'b' }, - plugins: ['a', 'b'] - } - } + plugins: ['a', 'b'], + }, + }, }); - const [{ payload }] = events.eventCliSession({ - cliCommand: 'dev', - astroVersion: '0.0.0' - }, config); + const [{ payload }] = events.eventCliSession( + { + cliCommand: 'dev', + astroVersion: '0.0.0', + }, + config + ); expect(payload.config.viteKeys).is.deep.equal(['worker', 'worker.format', 'worker.plugins']); }); });