Home / Function/ build() — astro Function Reference

build() — astro Function Reference

Architecture documentation for the build() function in build.js from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  66836503_9c73_8364_2f68_033932da2823["build()"]
  35c0b351_a315_b75b_aa2a_7f7a823ba60f["build.js"]
  66836503_9c73_8364_2f68_033932da2823 -->|defined in| 35c0b351_a315_b75b_aa2a_7f7a823ba60f
  2d473f2d_5791_aa1c_1e75_da0db7416851["getPrebuilds()"]
  66836503_9c73_8364_2f68_033932da2823 -->|calls| 2d473f2d_5791_aa1c_1e75_da0db7416851
  9430cf94_cdae_0511_de17_f315fa672a00["readPackageJSON()"]
  66836503_9c73_8364_2f68_033932da2823 -->|calls| 9430cf94_cdae_0511_de17_f315fa672a00
  364ea233_0325_d0c3_d524_7cd1b4b3767d["getDefinedEntries()"]
  66836503_9c73_8364_2f68_033932da2823 -->|calls| 364ea233_0325_d0c3_d524_7cd1b4b3767d
  268890e3_eabf_9b31_c7b7_e4c2c6f1a52b["clean()"]
  66836503_9c73_8364_2f68_033932da2823 -->|calls| 268890e3_eabf_9b31_c7b7_e4c2c6f1a52b
  399fe77d_1916_cbd4_3f65_b4a804530add["prebuild()"]
  66836503_9c73_8364_2f68_033932da2823 -->|calls| 399fe77d_1916_cbd4_3f65_b4a804530add
  style 66836503_9c73_8364_2f68_033932da2823 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

scripts/cmd/build.js lines 36–121

export default async function build(...args) {
	const config = Object.assign({}, defaultConfig);
	const isDev = args.slice(-1)[0] === 'IS_DEV';
	const prebuilds = getPrebuilds(isDev, args);
	const patterns = args
		.filter((f) => !!f) // remove empty args
		.filter((f) => !f.startsWith('--')) // remove flags
		.map((f) => f.replace(/^'/, '').replace(/'$/, '')); // Needed for Windows: glob strings contain surrounding string chars??? remove these
	let entryPoints = [].concat(
		...(await Promise.all(
			patterns.map((pattern) =>
				glob(pattern, { filesOnly: true, expandDirectories: false, absolute: true }),
			),
		)),
	);

	const noClean = args.includes('--no-clean-dist');
	const cleanDts = args.includes('--clean-dts');
	const bundle = args.includes('--bundle');
	const forceCJS = args.includes('--force-cjs');

	const { type = 'module', dependencies = {} } = await readPackageJSON('./package.json');

	config.define = {};
	for (const [key, value] of await getDefinedEntries()) {
		config.define[`process.env.${key}`] = JSON.stringify(value);
	}
	const format = type === 'module' && !forceCJS ? 'esm' : 'cjs';

	const outdir = 'dist';

	if (!noClean) {
		await clean(outdir, cleanDts);
	}

	if (!isDev) {
		await esbuild.build({
			...config,
			bundle,
			external: bundle ? Object.keys(dependencies) : undefined,
			entryPoints,
			outdir,
			outExtension: forceCJS ? { '.js': '.cjs' } : {},
			format,
		});
		return;
	}

	const rebuildPlugin = {
		name: 'astro:rebuild',
		setup(build) {
			build.onEnd(async (result) => {
				if (prebuilds.length) {
					await prebuild(...prebuilds);
				}
				const date = dt.format(new Date());
				if (result && result.errors.length) {
					console.error(colors.dim(`[${date}] `) + colors.red(error || result.errors.join('\n')));
				} else {
					if (result.warnings.length) {
						console.info(
							colors.dim(`[${date}] `) +
								colors.yellow('! updated with warnings:\n' + result.warnings.join('\n')),
						);
					}
					console.info(colors.dim(`[${date}] `) + colors.green('√ updated'));
				}
			});
		},
	};

	const builder = await esbuild.context({
		...config,
		entryPoints,
		outdir,
		format,
		sourcemap: 'linked',
		plugins: [rebuildPlugin],
	});

	await builder.watch();

Domain

Subdomains

Frequently Asked Questions

What does build() do?
build() is a function in the astro codebase, defined in scripts/cmd/build.js.
Where is build() defined?
build() is defined in scripts/cmd/build.js at line 36.
What does build() call?
build() calls 5 function(s): clean, getDefinedEntries, getPrebuilds, prebuild, readPackageJSON.

Analyze Your Own Codebase

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

Try Supermodel Free