Home / File/ file-url.ts — astro Source File

file-url.ts — astro Source File

Architecture documentation for file-url.ts, a typescript file in the astro codebase. 5 imports, 0 dependents.

File typescript CoreAstro RoutingSystem 5 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  b732267f_d6c0_cf1d_49c8_16fd06792612["file-url.ts"]
  6ae6435e_1314_7d59_4826_25e9a2ada03f["./utils.js"]
  b732267f_d6c0_cf1d_49c8_16fd06792612 --> 6ae6435e_1314_7d59_4826_25e9a2ada03f
  e16a223b_37f3_6b25_1ee1_2b7bcb9d9415["node:fs"]
  b732267f_d6c0_cf1d_49c8_16fd06792612 --> e16a223b_37f3_6b25_1ee1_2b7bcb9d9415
  c52a5f83_66e3_37d7_9ebb_767f7129bc62["node:path"]
  b732267f_d6c0_cf1d_49c8_16fd06792612 --> c52a5f83_66e3_37d7_9ebb_767f7129bc62
  d9a92db9_c95e_9165_13ac_24b3d859d946["node:url"]
  b732267f_d6c0_cf1d_49c8_16fd06792612 --> d9a92db9_c95e_9165_13ac_24b3d859d946
  f16d8c76_2866_6150_bd14_0347b59abfe9["astro"]
  b732267f_d6c0_cf1d_49c8_16fd06792612 --> f16d8c76_2866_6150_bd14_0347b59abfe9
  style b732267f_d6c0_cf1d_49c8_16fd06792612 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import fs from 'node:fs';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import type { AstroConfig, AstroIntegration } from 'astro';
import type { VitePlugin } from '../utils.js';

async function copyFile(toDir: URL, fromUrl: URL, toUrl: URL) {
	await fs.promises.mkdir(toDir, { recursive: true });
	await fs.promises.rename(fromUrl, toUrl);
}

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));
					}
					await Promise.all(moves);
				}
			},
		},
	};
}

Domain

Subdomains

Dependencies

  • ./utils.js
  • astro
  • node:fs
  • node:path
  • node:url

Frequently Asked Questions

What does file-url.ts do?
file-url.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RoutingSystem subdomain.
What functions are defined in file-url.ts?
file-url.ts defines 2 function(s): copyFile, fileURLIntegration.
What does file-url.ts depend on?
file-url.ts imports 5 module(s): ./utils.js, astro, node:fs, node:path, node:url.
Where is file-url.ts in the architecture?
file-url.ts is located at packages/db/src/core/integration/file-url.ts (domain: CoreAstro, subdomain: RoutingSystem, directory: packages/db/src/core/integration).

Analyze Your Own Codebase

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

Try Supermodel Free