[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 importedCssUrls = new Set<string>();
const importedStylesMap = new Map<string, 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(); const ext = path.extname(importedModule.url).toLowerCase();
if (STYLE_EXTENSIONS.has(ext)) { if (STYLE_EXTENSIONS.has(ext)) {
if ( if (

View file

@ -16,8 +16,8 @@ import { render as coreRender } from '../core.js';
import { RouteCache } from '../route-cache.js'; import { RouteCache } from '../route-cache.js';
import { collectMdMetadata } from '../util.js'; import { collectMdMetadata } from '../util.js';
import { getStylesForURL } from './css.js'; import { getStylesForURL } from './css.js';
import { getScriptsForURL } from './scripts.js';
import { resolveClientDevPath } from './resolve.js'; import { resolveClientDevPath } from './resolve.js';
import { getScriptsForURL } from './scripts.js';
export interface SSROptions { export interface SSROptions {
/** an instance of the AstroConfig */ /** 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 { ModuleInfo } from 'rollup';
import type { PluginMetadata as AstroPluginMetadata } from '../../../vite-plugin-astro/types';
import vite from 'vite';
import slash from 'slash'; 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 { viteID } from '../../util.js';
import { createModuleScriptElementWithSrc } from '../ssr-element.js'; import { createModuleScriptElementWithSrc } from '../ssr-element.js';
import { crawlGraph } from './vite.js'; import { crawlGraph } from './vite.js';
import { fileURLToPath } from 'url';
export async function getScriptsForURL( export async function getScriptsForURL(
filePath: URL, filePath: URL,
astroConfig: AstroConfig, astroConfig: AstroConfig,
viteServer: vite.ViteDevServer, viteServer: vite.ViteDevServer
): Promise<Set<SSRElement>> { ): Promise<Set<SSRElement>> {
const elements = new Set<SSRElement>(); const elements = new Set<SSRElement>();
const rootID = viteID(filePath); const rootID = viteID(filePath);
let rootProjectFolder = slash(fileURLToPath(astroConfig.root)); let rootProjectFolder = slash(fileURLToPath(astroConfig.root));
const modInfo = viteServer.pluginContainer.getModuleInfo(rootID); const modInfo = viteServer.pluginContainer.getModuleInfo(rootID);
addHoistedScripts(elements, modInfo, rootProjectFolder); 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; const id = moduleNode.id;
if(id) { if (id) {
const info = viteServer.pluginContainer.getModuleInfo(id); const info = viteServer.pluginContainer.getModuleInfo(id);
addHoistedScripts(elements, info, rootProjectFolder); addHoistedScripts(elements, info, rootProjectFolder);
} }
@ -29,14 +29,18 @@ export async function getScriptsForURL(
return elements; return elements;
} }
function addHoistedScripts(set: Set<SSRElement>, info: ModuleInfo | null, rootProjectFolder: string) { function addHoistedScripts(
if(!info?.meta?.astro) { set: Set<SSRElement>,
info: ModuleInfo | null,
rootProjectFolder: string
) {
if (!info?.meta?.astro) {
return; return;
} }
let id = info.id; let id = info.id;
const astro = info?.meta?.astro as AstroPluginMetadata['astro']; 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 scriptId = `${id}?astro&type=script&index=${i}&lang.ts`;
const element = createModuleScriptElementWithSrc(scriptId); const element = createModuleScriptElementWithSrc(scriptId);
set.add(element); set.add(element);

View file

@ -1,20 +1,20 @@
import vite from 'vite';
import npath from 'path'; import npath from 'path';
import vite from 'vite';
import { unwrapId } from '../../util.js'; import { unwrapId } from '../../util.js';
/** /**
* List of file extensions signalling we can (and should) SSR ahead-of-time * List of file extensions signalling we can (and should) SSR ahead-of-time
* See usage below * 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 */ /** 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, viteServer: vite.ViteDevServer,
_id: string, _id: string,
isFile: boolean, isFile: boolean,
scanned = new Set<string>() scanned = new Set<string>()
) : AsyncGenerator<vite.ModuleNode, void, unknown> { ): AsyncGenerator<vite.ModuleNode, void, unknown> {
const id = unwrapId(_id); const id = unwrapId(_id);
const importedModules = new Set<vite.ModuleNode>(); const importedModules = new Set<vite.ModuleNode>();
const moduleEntriesForId = isFile const moduleEntriesForId = isFile
@ -45,7 +45,7 @@ export async function * crawlGraph(
const { pathname } = new URL(`file://${importedModule.id}`); const { pathname } = new URL(`file://${importedModule.id}`);
if (fileExtensionsToSSR.has(npath.extname(pathname))) { if (fileExtensionsToSSR.has(npath.extname(pathname))) {
const mod = viteServer.moduleGraph.getModuleById(importedModule.id); const mod = viteServer.moduleGraph.getModuleById(importedModule.id);
if(!mod?.ssrModule) { if (!mod?.ssrModule) {
await viteServer.ssrLoadModule(importedModule.id); await viteServer.ssrLoadModule(importedModule.id);
} }
} }
@ -63,6 +63,6 @@ export async function * crawlGraph(
} }
yield importedModule; 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 found = 0;
let moduleScripts = $('[type=module]'); let moduleScripts = $('[type=module]');
moduleScripts.each((i, el) => { 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++; found++;
} }
}) });
expect(found).to.equal(1); expect(found).to.equal(1);
}); });
}); });