Home / Function/ astroContentVirtualModPlugin() — astro Function Reference

astroContentVirtualModPlugin() — astro Function Reference

Architecture documentation for the astroContentVirtualModPlugin() function in vite-plugin-content-virtual-mod.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  754289da_2be2_fdd7_44b9_fb417cc29838["astroContentVirtualModPlugin()"]
  22d17eb4_64b9_b222_54e2_71c9cd084abb["vite-plugin-content-virtual-mod.ts"]
  754289da_2be2_fdd7_44b9_fb417cc29838 -->|defined in| 22d17eb4_64b9_b222_54e2_71c9cd084abb
  3cd48fe4_109f_8e01_4a57_b017061f1e11["invalidateDataStore()"]
  754289da_2be2_fdd7_44b9_fb417cc29838 -->|calls| 3cd48fe4_109f_8e01_4a57_b017061f1e11
  17b064e5_94fc_5c78_1929_cbcca4abb080["generateContentEntryFile()"]
  754289da_2be2_fdd7_44b9_fb417cc29838 -->|calls| 17b064e5_94fc_5c78_1929_cbcca4abb080
  style 754289da_2be2_fdd7_44b9_fb417cc29838 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/content/vite-plugin-content-virtual-mod.ts lines 46–209

export function astroContentVirtualModPlugin({
	settings,
	fs,
}: AstroContentVirtualModPluginParams): Plugin {
	let dataStoreFile: URL;
	let devServer: ViteDevServer;
	let liveConfig: string;
	return {
		name: 'astro-content-virtual-mod-plugin',
		enforce: 'pre',
		config(_, env) {
			dataStoreFile = getDataStoreFile(settings, env.command === 'serve');
			const contentPaths = getContentPaths(
				settings.config,
				undefined,
				settings.config.legacy?.collectionsBackwardsCompat,
			);
			if (contentPaths.liveConfig.exists) {
				liveConfig = normalizePath(fileURLToPath(contentPaths.liveConfig.url));
			}
		},
		buildStart() {
			if (devServer) {
				// We defer adding the data store file to the watcher until the server is ready
				devServer.watcher.add(fileURLToPath(dataStoreFile));
				// Manually invalidate the data store to avoid a race condition in file watching
				invalidateDataStore(devServer);
			}
		},
		resolveId: {
			filter: {
				id: new RegExp(
					`^(${VIRTUAL_MODULE_ID}|${DATA_STORE_VIRTUAL_ID}|${MODULES_MJS_ID}|${ASSET_IMPORTS_VIRTUAL_ID})$|(?:\\?|&)${CONTENT_MODULE_FLAG}(?:&|=|$)`,
				),
			},
			async handler(id, importer) {
				if (id === VIRTUAL_MODULE_ID) {
					// Live content config can't import the virtual module directly,
					// because it would create a circular dependency from the collection exports.
					// Instead, we resolve the config util module, because that's all that it should use anyway.
					if (liveConfig && importer && liveConfig === normalizePath(importer)) {
						return this.resolve('astro/virtual-modules/live-config', importer, {
							skipSelf: true,
						});
					}
					return RESOLVED_VIRTUAL_MODULE_ID;
				}
				if (id === DATA_STORE_VIRTUAL_ID) {
					return RESOLVED_DATA_STORE_VIRTUAL_ID;
				}

				if (isDeferredModule(id)) {
					const [, query] = id.split('?');
					const params = new URLSearchParams(query);
					const fileName = params.get('fileName');
					let importPath = undefined;
					if (fileName && URL.canParse(fileName, settings.config.root.toString())) {
						importPath = fileURLToPath(new URL(fileName, settings.config.root));
					}
					if (importPath) {
						return await this.resolve(`${importPath}?${CONTENT_RENDER_FLAG}`);
					}
				}

				if (id === MODULES_MJS_ID) {
					const modules = new URL(MODULES_IMPORTS_FILE, settings.dotAstroDir);
					if (fs.existsSync(modules)) {
						return fileURLToPath(modules);
					}
					return MODULES_MJS_VIRTUAL_ID;
				}

				if (id === ASSET_IMPORTS_VIRTUAL_ID) {
					const assetImportsFile = new URL(ASSET_IMPORTS_FILE, settings.dotAstroDir);
					if (fs.existsSync(assetImportsFile)) {
						return fileURLToPath(assetImportsFile);
					}
					return ASSET_IMPORTS_RESOLVED_STUB_ID;
				}
			},
		},

Subdomains

Frequently Asked Questions

What does astroContentVirtualModPlugin() do?
astroContentVirtualModPlugin() is a function in the astro codebase, defined in packages/astro/src/content/vite-plugin-content-virtual-mod.ts.
Where is astroContentVirtualModPlugin() defined?
astroContentVirtualModPlugin() is defined in packages/astro/src/content/vite-plugin-content-virtual-mod.ts at line 46.
What does astroContentVirtualModPlugin() call?
astroContentVirtualModPlugin() calls 2 function(s): generateContentEntryFile, invalidateDataStore.

Analyze Your Own Codebase

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

Try Supermodel Free