Home / Function/ fileURLIntegration() — astro Function Reference

fileURLIntegration() — astro Function Reference

Architecture documentation for the fileURLIntegration() function in file-url.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  559622ef_223e_93a4_b6d2_04bd6a4bb967["fileURLIntegration()"]
  b732267f_d6c0_cf1d_49c8_16fd06792612["file-url.ts"]
  559622ef_223e_93a4_b6d2_04bd6a4bb967 -->|defined in| b732267f_d6c0_cf1d_49c8_16fd06792612
  bfe59b07_265f_7284_2914_b0fdc506d8e8["copyFile()"]
  559622ef_223e_93a4_b6d2_04bd6a4bb967 -->|calls| bfe59b07_265f_7284_2914_b0fdc506d8e8
  style 559622ef_223e_93a4_b6d2_04bd6a4bb967 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/db/src/core/integration/file-url.ts lines 12–98

export function fileURLIntegration(): AstroIntegration {
	const fileNames: string[] = [];

	function createVitePlugin(command: 'build' | 'preview' | 'dev' | 'sync'): VitePlugin {
		let referenceIds: string[] = [];
		return {
			name: '@astrojs/db/file-url',
			enforce: 'pre',
			load: {
				filter: {
					id: /\?fileurl$/,
				},
				async handler(id) {
					const filePath = id.slice(0, id.indexOf('?'));
					if (command === 'build') {
						const data = await fs.promises.readFile(filePath);
						const name = path.basename(filePath);
						const referenceId = this.emitFile({
							name,
							source: data,
							type: 'asset',
						});
						referenceIds.push(referenceId);
						return `export default import.meta.ROLLUP_FILE_URL_${referenceId};`;
					}
					// dev mode
					else {
						return `export default new URL(${JSON.stringify(pathToFileURL(filePath).toString())})`;
					}
				},
			},
			generateBundle() {
				// Save file names so we can copy them back over.
				for (const referenceId of referenceIds) {
					fileNames.push(this.getFileName(referenceId));
				}
				// Reset `referenceIds` for later generateBundle() runs.
				// Prevents lookup for ids that have already been copied.
				referenceIds = [];
			},
		};
	}

	let config: AstroConfig;
	return {
		name: '@astrojs/db/file-url',
		hooks: {
			'astro:config:setup'({ updateConfig, command }) {
				updateConfig({
					vite: {
						plugins: [createVitePlugin(command)],
					},
				});
			},
			'astro:config:done': ({ config: _config }) => {
				config = _config;
			},
			async 'astro:build:done'() {
				if (config.output === 'static') {
					// Delete the files since they are only used for the build process.
					const unlinks: Promise<unknown>[] = [];
					for (const fileName of fileNames) {
						const url = new URL(fileName, config.outDir);
						unlinks.push(fs.promises.unlink(url));
					}
					await Promise.all(unlinks);
					// Delete the assets directory if it is empty.
					// NOTE(fks): Ignore errors here because this is expected to fail
					// if the directory contains files, or if it does not exist.
					// If it errors for some unknown reason, it's not a big deal.
					const assetDir = new URL(config.build.assets, config.outDir);
					await fs.promises.rmdir(assetDir).catch(() => []);
				} else {
					// Move files back over to the dist output path
					const moves: Promise<unknown>[] = [];
					for (const fileName of fileNames) {
						const fromUrl = new URL(fileName, config.build.client);
						const toUrl = new URL(fileName, config.build.server);
						const toDir = new URL('./', toUrl);
						moves.push(copyFile(toDir, fromUrl, toUrl));
					}

Domain

Subdomains

Calls

Frequently Asked Questions

What does fileURLIntegration() do?
fileURLIntegration() is a function in the astro codebase, defined in packages/db/src/core/integration/file-url.ts.
Where is fileURLIntegration() defined?
fileURLIntegration() is defined in packages/db/src/core/integration/file-url.ts at line 12.
What does fileURLIntegration() call?
fileURLIntegration() calls 1 function(s): copyFile.

Analyze Your Own Codebase

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

Try Supermodel Free