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:
parent
f2b8372c0c
commit
f5adc023b2
6 changed files with 45 additions and 40 deletions
|
@ -72,9 +72,11 @@ function* throttle(max: number, inPaths: string[]) {
|
|||
}
|
||||
|
||||
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.
|
||||
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<SSRElement>([{
|
||||
const scripts = hoistedId
|
||||
? new Set<SSRElement>([
|
||||
{
|
||||
props: {
|
||||
type: 'module',
|
||||
src: npath.posix.join(rootpath, hoistedId),
|
||||
},
|
||||
children: ''
|
||||
}]) : new Set<SSRElement>();
|
||||
children: '',
|
||||
},
|
||||
])
|
||||
: new Set<SSRElement>();
|
||||
const result = createResult({ astroConfig, logging, origin, params, pathname, renderers, links, scripts });
|
||||
|
||||
// Override the `resolve` method so that hydrated components are given the
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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<SSRElement>(Array.from(mod.$$metadata.hoistedScriptPaths()).map(src => ({
|
||||
const scripts = astroConfig.buildOptions.experimentalStaticBuild
|
||||
? new Set<SSRElement>(
|
||||
Array.from(mod.$$metadata.hoistedScriptPaths()).map((src) => ({
|
||||
props: { type: 'module', src },
|
||||
children: ''
|
||||
}))) : new Set<SSRElement>();
|
||||
children: '',
|
||||
}))
|
||||
)
|
||||
: new Set<SSRElement>();
|
||||
|
||||
const result = createResult({ astroConfig, logging, origin, params, pathname, renderers, scripts });
|
||||
// Resolves specifiers in the inline hydrated scripts, such as "@astrojs/renderer-preact/client.js"
|
||||
|
|
|
@ -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++;
|
||||
}
|
||||
|
|
|
@ -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!}";`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue