0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00

[ci] yarn format

This commit is contained in:
jonathantneal 2022-01-21 00:14:18 +00:00 committed by GitHub Actions
parent f2b8372c0c
commit f5adc023b2
6 changed files with 45 additions and 40 deletions

View file

@ -72,9 +72,11 @@ function* throttle(max: number, inPaths: string[]) {
} }
function getByFacadeId<T>(facadeId: string, map: Map<string, T>): T | undefined { function getByFacadeId<T>(facadeId: string, map: Map<string, T>): T | undefined {
return map.get(facadeId) || return (
map.get(facadeId) ||
// Check with a leading `/` because on Windows it doesn't have one. // Check with a leading `/` because on Windows it doesn't have one.
map.get('/' + facadeId); map.get('/' + facadeId)
);
} }
export async function staticBuild(opts: StaticBuildOptions) { export async function staticBuild(opts: StaticBuildOptions) {
@ -118,7 +120,7 @@ export async function staticBuild(opts: StaticBuildOptions) {
// Add hoisted scripts // Add hoisted scripts
const hoistedScripts = new Set(metadata.hoistedScriptPaths()); const hoistedScripts = new Set(metadata.hoistedScriptPaths());
if(hoistedScripts.size) { if (hoistedScripts.size) {
const moduleId = new URL('./hoisted.js', astroModuleURL + '/').pathname; const moduleId = new URL('./hoisted.js', astroModuleURL + '/').pathname;
internals.hoistedScriptIdToHoistedMap.set(moduleId, hoistedScripts); internals.hoistedScriptIdToHoistedMap.set(moduleId, hoistedScripts);
topLevelImports.add(moduleId); topLevelImports.add(moduleId);
@ -128,7 +130,6 @@ export async function staticBuild(opts: StaticBuildOptions) {
jsInput.add(specifier); jsInput.add(specifier);
} }
pageInput.add(astroModuleId); pageInput.add(astroModuleId);
facadeIdToPageDataMap.set(astroModuleId, pageData); facadeIdToPageDataMap.set(astroModuleId, pageData);
} }
@ -343,13 +344,17 @@ async function generatePath(pathname: string, opts: StaticBuildOptions, gopts: G
children: '', children: '',
})) }))
); );
const scripts = hoistedId ? new Set<SSRElement>([{ const scripts = hoistedId
props: { ? new Set<SSRElement>([
type: 'module', {
src: npath.posix.join(rootpath, hoistedId), props: {
}, type: 'module',
children: '' src: npath.posix.join(rootpath, hoistedId),
}]) : new Set<SSRElement>(); },
children: '',
},
])
: new Set<SSRElement>();
const result = createResult({ astroConfig, logging, origin, params, pathname, renderers, links, scripts }); const result = createResult({ astroConfig, logging, origin, params, pathname, renderers, links, scripts });
// Override the `resolve` method so that hydrated components are given the // Override the `resolve` method so that hydrated components are given the

View file

@ -10,19 +10,19 @@ export function vitePluginHoistedScripts(internals: BuildInternals): VitePlugin
name: '@astro/rollup-plugin-astro-hoisted-scripts', name: '@astro/rollup-plugin-astro-hoisted-scripts',
resolveId(id) { resolveId(id) {
if(virtualHoistedEntry(id)) { if (virtualHoistedEntry(id)) {
return id; return id;
} }
}, },
load(id) { load(id) {
if(virtualHoistedEntry(id)) { if (virtualHoistedEntry(id)) {
let code = ''; let code = '';
for(let path of internals.hoistedScriptIdToHoistedMap.get(id)!) { for (let path of internals.hoistedScriptIdToHoistedMap.get(id)!) {
code += `import "${path}";` code += `import "${path}";`;
} }
return { return {
code code,
}; };
} }
return void 0; return void 0;
@ -31,13 +31,13 @@ export function vitePluginHoistedScripts(internals: BuildInternals): VitePlugin
async generateBundle(_options, bundle) { async generateBundle(_options, bundle) {
// Find all page entry points and create a map of the entry point to the hashed hoisted script. // 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. // This is used when we render so that we can add the script to the head.
for(const [id, output] of Object.entries(bundle)) { for (const [id, output] of Object.entries(bundle)) {
if(output.type === 'chunk' && output.facadeModuleId && virtualHoistedEntry(output.facadeModuleId)) { if (output.type === 'chunk' && output.facadeModuleId && virtualHoistedEntry(output.facadeModuleId)) {
const facadeId = 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); internals.facadeIdToHoistedEntryMap.set(filename, id);
} }
} }
} },
}; };
} }

View file

@ -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})`); if (!Component.isAstroComponentFactory) throw new Error(`Unable to SSR non-Astro component (${route?.component})`);
// Add hoisted script tags // Add hoisted script tags
const scripts = astroConfig.buildOptions.experimentalStaticBuild ? const scripts = astroConfig.buildOptions.experimentalStaticBuild
new Set<SSRElement>(Array.from(mod.$$metadata.hoistedScriptPaths()).map(src => ({ ? new Set<SSRElement>(
props: { type: 'module', src }, Array.from(mod.$$metadata.hoistedScriptPaths()).map((src) => ({
children: '' props: { type: 'module', src },
}))) : new Set<SSRElement>(); children: '',
}))
)
: new Set<SSRElement>();
const result = createResult({ astroConfig, logging, origin, params, pathname, renderers, scripts }); const result = createResult({ astroConfig, logging, origin, params, pathname, renderers, scripts });
// Resolves specifiers in the inline hydrated scripts, such as "@astrojs/renderer-preact/client.js" // Resolves specifiers in the inline hydrated scripts, such as "@astrojs/renderer-preact/client.js"

View file

@ -81,10 +81,11 @@ export class Metadata {
} }
} }
* hoistedScriptPaths() { *hoistedScriptPaths() {
for(const metadata of this.deepMetadata()) { for (const metadata of this.deepMetadata()) {
let i = 0, pathname = metadata.mockURL.pathname; let i = 0,
while(i < metadata.hoisted.length) { pathname = metadata.mockURL.pathname;
while (i < metadata.hoisted.length) {
yield `${pathname}?astro&type=script&index=${i}`; yield `${pathname}?astro&type=script&index=${i}`;
i++; i++;
} }

View file

@ -50,32 +50,28 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
throw new Error(`Requests for Astro CSS must include an index.`); throw new Error(`Requests for Astro CSS must include an index.`);
} }
const transformResult = await cachedCompilation(config, const transformResult = await cachedCompilation(config, normalizeFilename(filename), null, viteTransform, opts);
normalizeFilename(filename), null, viteTransform, opts);
const csses = transformResult.css; const csses = transformResult.css;
const code = csses[query.index]; const code = csses[query.index];
return { return {
code, code,
}; };
} else if(query.type === 'script') { } else if (query.type === 'script') {
if(typeof query.index === 'undefined') { if (typeof query.index === 'undefined') {
throw new Error(`Requests for hoisted scripts must include an index`); throw new Error(`Requests for hoisted scripts must include an index`);
} }
const transformResult = await cachedCompilation(config, const transformResult = await cachedCompilation(config, normalizeFilename(filename), null, viteTransform, opts);
normalizeFilename(filename), null, viteTransform, opts);
const scripts = transformResult.scripts; const scripts = transformResult.scripts;
const hoistedScript = scripts[query.index]; const hoistedScript = scripts[query.index];
if(!hoistedScript) { if (!hoistedScript) {
throw new Error(`No hoisted script at index ${query.index}`); throw new Error(`No hoisted script at index ${query.index}`);
} }
return { return {
code: hoistedScript.type === 'inline' ? code: hoistedScript.type === 'inline' ? hoistedScript.code! : `import "${hoistedScript.src!}";`,
hoistedScript.code! :
`import "${hoistedScript.src!}";`
}; };
} }
} }

View file

@ -92,6 +92,6 @@ describe('Static build', () => {
const indexHTML = await fixture.readFile('/index.html'); const indexHTML = await fixture.readFile('/index.html');
const $$ = cheerio.load(indexHTML); const $$ = cheerio.load(indexHTML);
expect($$(`script[src="${href}"]`).length).to.equal(0, 'no script added to different page'); expect($$(`script[src="${href}"]`).length).to.equal(0, 'no script added to different page');
}) });
}); });
}); });