Home / Function/ copyDependenciesToFunction() — astro Function Reference

copyDependenciesToFunction() — astro Function Reference

Architecture documentation for the copyDependenciesToFunction() function in nft.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  a715a943_cd79_ee96_1ca0_ae06b7d56247["copyDependenciesToFunction()"]
  0dd9b9f9_6119_4df3_6882_edf5907ca5f7["nft.ts"]
  a715a943_cd79_ee96_1ca0_ae06b7d56247 -->|defined in| 0dd9b9f9_6119_4df3_6882_edf5907ca5f7
  style a715a943_cd79_ee96_1ca0_ae06b7d56247 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/integrations/netlify/src/lib/nft.ts lines 9–83

export async function copyDependenciesToFunction(
	{
		entry,
		outDir,
		includeFiles,
		excludeFiles,
		logger,
		root,
	}: {
		entry: URL;
		outDir: URL;
		includeFiles: URL[];
		excludeFiles: URL[];
		logger: AstroIntegrationLogger;
		root: URL;
	},
	// we want to pass the caching by reference, and not by value
	cache: object,
): Promise<{ handler: string }> {
	const entryPath = fileURLToPath(entry);
	logger.info(`Bundling function ${relative(fileURLToPath(outDir), entryPath)}`);

	// Set the base to the workspace root
	const base = pathToFileURL(appendForwardSlash(searchForWorkspaceRoot(fileURLToPath(root))));

	// The Vite bundle includes an import to `@vercel/nft` for some reason,
	// and that trips up `@vercel/nft` itself during the adapter build. Using a
	// dynamic import helps prevent the issue.
	// TODO: investigate why
	const { nodeFileTrace } = await import('@vercel/nft');
	const result = await nodeFileTrace([entryPath], {
		base: fileURLToPath(base),
		cache,
	});

	for (const error of result.warnings) {
		if (error.message.startsWith('Failed to resolve dependency')) {
			const [, module, file] =
				/Cannot find module '(.+?)' loaded from (.+)/.exec(error.message) || [];

			// The import(astroRemark) sometimes fails to resolve, but it's not a problem
			if (module === '@astrojs/') continue;

			// Sharp is always external and won't be able to be resolved, but that's also not a problem
			if (module === 'sharp') continue;

			if (entryPath === file) {
				logger.debug(
					`The module "${module}" couldn't be resolved. This may not be a problem, but it's worth checking.`,
				);
			} else {
				logger.debug(
					`The module "${module}" inside the file "${file}" couldn't be resolved. This may not be a problem, but it's worth checking.`,
				);
			}
		}
		// parse errors are likely not js and can safely be ignored,
		// such as this html file in "main" meant for nw instead of node:
		// https://github.com/vercel/nft/issues/311
		else if (!error.message.startsWith('Failed to parse')) {
			throw error;
		}
	}

	const commonAncestor = await copyFilesToFolder(
		[...result.fileList].map((file) => new URL(file, base)).concat(includeFiles),
		outDir,
		excludeFiles,
	);

	return {
		// serverEntry location inside the outDir, converted to posix
		handler: relative(commonAncestor, entryPath).split(sep).join(posix.sep),
	};
}

Domain

Subdomains

Frequently Asked Questions

What does copyDependenciesToFunction() do?
copyDependenciesToFunction() is a function in the astro codebase, defined in packages/integrations/netlify/src/lib/nft.ts.
Where is copyDependenciesToFunction() defined?
copyDependenciesToFunction() is defined in packages/integrations/netlify/src/lib/nft.ts at line 9.

Analyze Your Own Codebase

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

Try Supermodel Free