From f5adc023b2081cf23bfc2d2b963722fde3110730 Mon Sep 17 00:00:00 2001 From: jonathantneal Date: Fri, 21 Jan 2022 00:14:18 +0000 Subject: [PATCH] [ci] yarn format --- packages/astro/src/core/build/static-build.ts | 27 +++++++++++-------- .../core/build/vite-plugin-hoisted-scripts.ts | 18 ++++++------- packages/astro/src/core/ssr/index.ts | 13 +++++---- packages/astro/src/runtime/server/metadata.ts | 9 ++++--- packages/astro/src/vite-plugin-astro/index.ts | 16 +++++------ packages/astro/test/static-build.test.js | 2 +- 6 files changed, 45 insertions(+), 40 deletions(-) diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 3f4089e4f4..bfce0bbf1d 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -72,9 +72,11 @@ function* throttle(max: number, inPaths: string[]) { } function getByFacadeId(facadeId: string, map: Map): T | undefined { - return map.get(facadeId) || + return ( + map.get(facadeId) || // Check with a leading `/` because on Windows it doesn't have one. - map.get('/' + facadeId); + map.get('/' + facadeId) + ); } export async function staticBuild(opts: StaticBuildOptions) { @@ -118,7 +120,7 @@ export async function staticBuild(opts: StaticBuildOptions) { // Add hoisted scripts const hoistedScripts = new Set(metadata.hoistedScriptPaths()); - if(hoistedScripts.size) { + if (hoistedScripts.size) { const moduleId = new URL('./hoisted.js', astroModuleURL + '/').pathname; internals.hoistedScriptIdToHoistedMap.set(moduleId, hoistedScripts); topLevelImports.add(moduleId); @@ -128,7 +130,6 @@ export async function staticBuild(opts: StaticBuildOptions) { jsInput.add(specifier); } - pageInput.add(astroModuleId); facadeIdToPageDataMap.set(astroModuleId, pageData); } @@ -343,13 +344,17 @@ async function generatePath(pathname: string, opts: StaticBuildOptions, gopts: G children: '', })) ); - const scripts = hoistedId ? new Set([{ - props: { - type: 'module', - src: npath.posix.join(rootpath, hoistedId), - }, - children: '' - }]) : new Set(); + const scripts = hoistedId + ? new Set([ + { + props: { + type: 'module', + src: npath.posix.join(rootpath, hoistedId), + }, + children: '', + }, + ]) + : new Set(); const result = createResult({ astroConfig, logging, origin, params, pathname, renderers, links, scripts }); // Override the `resolve` method so that hydrated components are given the diff --git a/packages/astro/src/core/build/vite-plugin-hoisted-scripts.ts b/packages/astro/src/core/build/vite-plugin-hoisted-scripts.ts index 8606d6a514..24071a241e 100644 --- a/packages/astro/src/core/build/vite-plugin-hoisted-scripts.ts +++ b/packages/astro/src/core/build/vite-plugin-hoisted-scripts.ts @@ -10,19 +10,19 @@ export function vitePluginHoistedScripts(internals: BuildInternals): VitePlugin name: '@astro/rollup-plugin-astro-hoisted-scripts', resolveId(id) { - if(virtualHoistedEntry(id)) { + if (virtualHoistedEntry(id)) { return id; } }, load(id) { - if(virtualHoistedEntry(id)) { + if (virtualHoistedEntry(id)) { let code = ''; - for(let path of internals.hoistedScriptIdToHoistedMap.get(id)!) { - code += `import "${path}";` + for (let path of internals.hoistedScriptIdToHoistedMap.get(id)!) { + code += `import "${path}";`; } return { - code + code, }; } return void 0; @@ -31,13 +31,13 @@ export function vitePluginHoistedScripts(internals: BuildInternals): VitePlugin async generateBundle(_options, bundle) { // Find all page entry points and create a map of the entry point to the hashed hoisted script. // This is used when we render so that we can add the script to the head. - for(const [id, output] of Object.entries(bundle)) { - if(output.type === 'chunk' && output.facadeModuleId && virtualHoistedEntry(output.facadeModuleId)) { + for (const [id, output] of Object.entries(bundle)) { + if (output.type === 'chunk' && output.facadeModuleId && virtualHoistedEntry(output.facadeModuleId)) { const facadeId = output.facadeModuleId!; - const filename = facadeId.slice(0, facadeId.length - "/hoisted.js".length); + const filename = facadeId.slice(0, facadeId.length - '/hoisted.js'.length); internals.facadeIdToHoistedEntryMap.set(filename, id); } } - } + }, }; } diff --git a/packages/astro/src/core/ssr/index.ts b/packages/astro/src/core/ssr/index.ts index 07a5bc7196..421e2dcce7 100644 --- a/packages/astro/src/core/ssr/index.ts +++ b/packages/astro/src/core/ssr/index.ts @@ -220,11 +220,14 @@ export async function render(renderers: Renderer[], mod: ComponentInstance, ssrO if (!Component.isAstroComponentFactory) throw new Error(`Unable to SSR non-Astro component (${route?.component})`); // Add hoisted script tags - const scripts = astroConfig.buildOptions.experimentalStaticBuild ? - new Set(Array.from(mod.$$metadata.hoistedScriptPaths()).map(src => ({ - props: { type: 'module', src }, - children: '' - }))) : new Set(); + const scripts = astroConfig.buildOptions.experimentalStaticBuild + ? new Set( + Array.from(mod.$$metadata.hoistedScriptPaths()).map((src) => ({ + props: { type: 'module', src }, + children: '', + })) + ) + : new Set(); const result = createResult({ astroConfig, logging, origin, params, pathname, renderers, scripts }); // Resolves specifiers in the inline hydrated scripts, such as "@astrojs/renderer-preact/client.js" diff --git a/packages/astro/src/runtime/server/metadata.ts b/packages/astro/src/runtime/server/metadata.ts index dc6a9a3a75..5343c03017 100644 --- a/packages/astro/src/runtime/server/metadata.ts +++ b/packages/astro/src/runtime/server/metadata.ts @@ -81,10 +81,11 @@ export class Metadata { } } - * hoistedScriptPaths() { - for(const metadata of this.deepMetadata()) { - let i = 0, pathname = metadata.mockURL.pathname; - while(i < metadata.hoisted.length) { + *hoistedScriptPaths() { + for (const metadata of this.deepMetadata()) { + let i = 0, + pathname = metadata.mockURL.pathname; + while (i < metadata.hoisted.length) { yield `${pathname}?astro&type=script&index=${i}`; i++; } diff --git a/packages/astro/src/vite-plugin-astro/index.ts b/packages/astro/src/vite-plugin-astro/index.ts index 826028d7b8..418c7925a6 100644 --- a/packages/astro/src/vite-plugin-astro/index.ts +++ b/packages/astro/src/vite-plugin-astro/index.ts @@ -50,32 +50,28 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu throw new Error(`Requests for Astro CSS must include an index.`); } - const transformResult = await cachedCompilation(config, - normalizeFilename(filename), null, viteTransform, opts); + const transformResult = await cachedCompilation(config, normalizeFilename(filename), null, viteTransform, opts); const csses = transformResult.css; const code = csses[query.index]; return { code, }; - } else if(query.type === 'script') { - if(typeof query.index === 'undefined') { + } else if (query.type === 'script') { + if (typeof query.index === 'undefined') { throw new Error(`Requests for hoisted scripts must include an index`); } - const transformResult = await cachedCompilation(config, - normalizeFilename(filename), null, viteTransform, opts); + const transformResult = await cachedCompilation(config, normalizeFilename(filename), null, viteTransform, opts); const scripts = transformResult.scripts; const hoistedScript = scripts[query.index]; - if(!hoistedScript) { + if (!hoistedScript) { throw new Error(`No hoisted script at index ${query.index}`); } return { - code: hoistedScript.type === 'inline' ? - hoistedScript.code! : - `import "${hoistedScript.src!}";` + code: hoistedScript.type === 'inline' ? hoistedScript.code! : `import "${hoistedScript.src!}";`, }; } } diff --git a/packages/astro/test/static-build.test.js b/packages/astro/test/static-build.test.js index 02d4f6c807..e0aee26d2e 100644 --- a/packages/astro/test/static-build.test.js +++ b/packages/astro/test/static-build.test.js @@ -92,6 +92,6 @@ describe('Static build', () => { const indexHTML = await fixture.readFile('/index.html'); const $$ = cheerio.load(indexHTML); expect($$(`script[src="${href}"]`).length).to.equal(0, 'no script added to different page'); - }) + }); }); });