0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-03 22:29:08 -05:00

[ci] format

This commit is contained in:
matthewp 2022-07-18 15:35:14 +00:00 committed by fredkbot
parent 3acb9ec264
commit b282cdb21f
6 changed files with 71 additions and 65 deletions

View file

@ -15,7 +15,7 @@ export async function getStylesForURL(
const importedCssUrls = new Set<string>();
const importedStylesMap = new Map<string, string>();
for await(const importedModule of crawlGraph(viteServer, viteID(filePath), true)) {
for await (const importedModule of crawlGraph(viteServer, viteID(filePath), true)) {
const ext = path.extname(importedModule.url).toLowerCase();
if (STYLE_EXTENSIONS.has(ext)) {
if (

View file

@ -16,8 +16,8 @@ import { render as coreRender } from '../core.js';
import { RouteCache } from '../route-cache.js';
import { collectMdMetadata } from '../util.js';
import { getStylesForURL } from './css.js';
import { getScriptsForURL } from './scripts.js';
import { resolveClientDevPath } from './resolve.js';
import { getScriptsForURL } from './scripts.js';
export interface SSROptions {
/** an instance of the AstroConfig */

View file

@ -1,26 +1,26 @@
import type { AstroConfig, SSRElement } from '../../../@types/astro';
import type { ModuleInfo } from 'rollup';
import type { PluginMetadata as AstroPluginMetadata } from '../../../vite-plugin-astro/types';
import vite from 'vite';
import slash from 'slash';
import { fileURLToPath } from 'url';
import vite from 'vite';
import type { AstroConfig, SSRElement } from '../../../@types/astro';
import type { PluginMetadata as AstroPluginMetadata } from '../../../vite-plugin-astro/types';
import { viteID } from '../../util.js';
import { createModuleScriptElementWithSrc } from '../ssr-element.js';
import { crawlGraph } from './vite.js';
import { fileURLToPath } from 'url';
export async function getScriptsForURL(
filePath: URL,
astroConfig: AstroConfig,
viteServer: vite.ViteDevServer,
viteServer: vite.ViteDevServer
): Promise<Set<SSRElement>> {
const elements = new Set<SSRElement>();
const rootID = viteID(filePath);
let rootProjectFolder = slash(fileURLToPath(astroConfig.root));
const modInfo = viteServer.pluginContainer.getModuleInfo(rootID);
addHoistedScripts(elements, modInfo, rootProjectFolder);
for await(const moduleNode of crawlGraph(viteServer, rootID, true)) {
for await (const moduleNode of crawlGraph(viteServer, rootID, true)) {
const id = moduleNode.id;
if(id) {
if (id) {
const info = viteServer.pluginContainer.getModuleInfo(id);
addHoistedScripts(elements, info, rootProjectFolder);
}
@ -29,14 +29,18 @@ export async function getScriptsForURL(
return elements;
}
function addHoistedScripts(set: Set<SSRElement>, info: ModuleInfo | null, rootProjectFolder: string) {
if(!info?.meta?.astro) {
function addHoistedScripts(
set: Set<SSRElement>,
info: ModuleInfo | null,
rootProjectFolder: string
) {
if (!info?.meta?.astro) {
return;
}
let id = info.id;
const astro = info?.meta?.astro as AstroPluginMetadata['astro'];
for(let i = 0; i < astro.scripts.length; i++) {
for (let i = 0; i < astro.scripts.length; i++) {
const scriptId = `${id}?astro&type=script&index=${i}&lang.ts`;
const element = createModuleScriptElementWithSrc(scriptId);
set.add(element);

View file

@ -1,20 +1,20 @@
import vite from 'vite';
import npath from 'path';
import vite from 'vite';
import { unwrapId } from '../../util.js';
/**
* List of file extensions signalling we can (and should) SSR ahead-of-time
* See usage below
*/
const fileExtensionsToSSR = new Set(['.astro', '.md']);
const fileExtensionsToSSR = new Set(['.astro', '.md']);
/** recursively crawl the module graph to get all style files imported by parent id */
export async function * crawlGraph(
export async function* crawlGraph(
viteServer: vite.ViteDevServer,
_id: string,
isFile: boolean,
scanned = new Set<string>()
) : AsyncGenerator<vite.ModuleNode, void, unknown> {
): AsyncGenerator<vite.ModuleNode, void, unknown> {
const id = unwrapId(_id);
const importedModules = new Set<vite.ModuleNode>();
const moduleEntriesForId = isFile
@ -45,7 +45,7 @@ export async function * crawlGraph(
const { pathname } = new URL(`file://${importedModule.id}`);
if (fileExtensionsToSSR.has(npath.extname(pathname))) {
const mod = viteServer.moduleGraph.getModuleById(importedModule.id);
if(!mod?.ssrModule) {
if (!mod?.ssrModule) {
await viteServer.ssrLoadModule(importedModule.id);
}
}
@ -63,6 +63,6 @@ export async function * crawlGraph(
}
yield importedModule;
yield * crawlGraph(viteServer, importedModule.id, false, scanned);
yield* crawlGraph(viteServer, importedModule.id, false, scanned);
}
}

View file

@ -135,10 +135,12 @@ describe('Scripts (hoisted and not)', () => {
let found = 0;
let moduleScripts = $('[type=module]');
moduleScripts.each((i, el) => {
if($(el).attr('src').includes('Glob/GlobComponent.astro?astro&type=script&index=0&lang.ts')) {
if (
$(el).attr('src').includes('Glob/GlobComponent.astro?astro&type=script&index=0&lang.ts')
) {
found++;
}
})
});
expect(found).to.equal(1);
});
});