Home / Function/ crawlGraph() — astro Function Reference

crawlGraph() — astro Function Reference

Architecture documentation for the crawlGraph() function in vite.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  1990e7cb_6306_328a_9c7d_94c08193d1d1["crawlGraph()"]
  07d85cd0_2c3b_d625_d196_6081b8d3c820["vite.ts"]
  1990e7cb_6306_328a_9c7d_94c08193d1d1 -->|defined in| 07d85cd0_2c3b_d625_d196_6081b8d3c820
  ddebbe73_0992_18e1_1ef1_fc21c7e2f362["isImportedBy()"]
  1990e7cb_6306_328a_9c7d_94c08193d1d1 -->|calls| ddebbe73_0992_18e1_1ef1_fc21c7e2f362
  style 1990e7cb_6306_328a_9c7d_94c08193d1d1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/vite-plugin-astro-server/vite.ts lines 17–116

export async function* crawlGraph(
	environment: RunnableDevEnvironment,
	_id: string,
	isRootFile: boolean,
	scanned = new Set<string>(),
): AsyncGenerator<EnvironmentModuleNode, void, unknown> {
	const id = unwrapId(_id);
	const importedModules = new Set<EnvironmentModuleNode>();

	const moduleEntriesForId = isRootFile
		? // "getModulesByFile" pulls from a delayed module cache (fun implementation detail),
			// So we can get up-to-date info on initial server load.
			// Needed for slower CSS preprocessing like Tailwind
			(environment.moduleGraph.getModulesByFile(id) ?? new Set())
		: // For non-root files, we're safe to pull from "getModuleById" based on testing.
			// TODO: Find better invalidation strategy to use "getModuleById" in all cases!
			new Set([environment.moduleGraph.getModuleById(id)]);

	// Collect all imported modules for the module(s).
	for (const entry of moduleEntriesForId) {
		// Handle this in case an module entries weren't found for ID
		// This seems possible with some virtual IDs (ex: `astro:markdown/*.md`)
		if (!entry) {
			continue;
		}
		if (id === entry.id) {
			scanned.add(id);

			// NOTE: It may be worth revisiting if we can crawl direct imports of the module since
			// `.importedModules` would also include modules that are dynamically watched, not imported.
			// That way we no longer need the below `continue` skips.

			// CSS requests `importedModules` are usually from `@import`, but we don't really need
			// to crawl into those as the `@import` code are already inlined into this `id`.
			// If CSS requests `importedModules` contain non-CSS files, e.g. Tailwind might add HMR
			// dependencies as `importedModules`, we should also skip them as they aren't really
			// imported. Without this, every hoisted script in the project is added to every page!
			if (isCSSRequest(id)) {
				continue;
			}
			// Some special Vite queries like `?url` or `?raw` are known to be a simple default export
			// and doesn't have any imports to crawl. However, since they would `this.addWatchFile` the
			// underlying module, our logic would crawl into them anyways which is incorrect as they
			// don't take part in the final rendering, so we skip it here.
			if (hasSpecialQueries(id)) {
				continue;
			}

			for (const importedModule of entry.importedModules) {
				if (!importedModule.id) continue;

				// some dynamically imported modules are *not* server rendered in time
				// to only SSR modules that we can safely transform, we check against
				// a list of file extensions based on our built-in vite plugins

				// Strip special query params like "?content".
				// NOTE: Cannot use `new URL()` here because not all IDs will be valid paths.
				// For example, `virtual:image-loader` if you don't have the plugin installed.
				const importedModulePathname = importedModule.id.replace(STRIP_QUERY_PARAMS_REGEX, '');

				const isFileTypeNeedingSSR = fileExtensionsToSSR.has(npath.extname(importedModulePathname));
				// A propagation stopping point is a module with the ?astroPropagatedAssets flag.
				// When we encounter one of these modules we don't want to continue traversing.
				const isPropagationStoppingPoint = importedModule.id.includes(PROPAGATED_ASSET_QUERY_PARAM);
				if (
					isFileTypeNeedingSSR &&
					// Should not SSR a module with ?astroPropagatedAssets
					!isPropagationStoppingPoint
				) {
					const mod = environment.moduleGraph.getModuleById(importedModule.id);
					if (!mod?.ssrModule) {
						try {
							await environment.runner.import(importedModule.id);
						} catch {
							/** Likely an out-of-date module entry! Silently continue. */
						}
					}
				}

				// Make sure the `importedModule` traversed is explicitly imported by the user, and not by HMR
				// TODO: This isn't very performant. Maybe look into using `ssrTransformResult` but make sure it

Domain

Subdomains

Frequently Asked Questions

What does crawlGraph() do?
crawlGraph() is a function in the astro codebase, defined in packages/astro/src/vite-plugin-astro-server/vite.ts.
Where is crawlGraph() defined?
crawlGraph() is defined in packages/astro/src/vite-plugin-astro-server/vite.ts at line 17.
What does crawlGraph() call?
crawlGraph() calls 1 function(s): isImportedBy.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free