Home / Function/ prebuild() — astro Function Reference

prebuild() — astro Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  399fe77d_1916_cbd4_3f65_b4a804530add["prebuild()"]
  94d4ebd1_5989_8457_de40_0682a9174dd4["prebuild.js"]
  399fe77d_1916_cbd4_3f65_b4a804530add -->|defined in| 94d4ebd1_5989_8457_de40_0682a9174dd4
  66836503_9c73_8364_2f68_033932da2823["build()"]
  66836503_9c73_8364_2f68_033932da2823 -->|calls| 399fe77d_1916_cbd4_3f65_b4a804530add
  b63c5031_980c_3ac7_7417_c81b31b723b3["escapeTemplateLiterals()"]
  399fe77d_1916_cbd4_3f65_b4a804530add -->|calls| b63c5031_980c_3ac7_7417_c81b31b723b3
  style 399fe77d_1916_cbd4_3f65_b4a804530add fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

scripts/cmd/prebuild.js lines 12–117

export default async function prebuild(...args) {
	let buildToString = args.indexOf('--to-string');
	if (buildToString !== -1) {
		args.splice(buildToString, 1);
		buildToString = true;
	}
	let minify = true;
	let minifyIdx = args.indexOf('--no-minify');
	if (minifyIdx !== -1) {
		minify = false;
		args.splice(minifyIdx, 1);
	}

	let patterns = args;
	// NOTE: absolute paths returned are forward slashes on windows
	let entryPoints = [].concat(
		...(await Promise.all(
			patterns.map((pattern) => glob(pattern, { onlyFiles: true, absolute: true })),
		)),
	);

	function getPrebuildURL(entryfilepath, dev = false) {
		const entryURL = pathToFileURL(entryfilepath);
		const basename = path.basename(entryfilepath);
		const ext = path.extname(entryfilepath);
		const name = basename.slice(0, basename.indexOf(ext));
		const outname = dev ? `${name}.prebuilt-dev${ext}` : `${name}.prebuilt${ext}`;
		const outURL = new URL('./' + outname, entryURL);
		return outURL;
	}

	async function prebuildFile(filepath) {
		let tscode = await fs.promises.readFile(filepath, 'utf-8');
		// If we're bundling a client directive, modify the code to match `packages/astro/src/core/client-directive/build.ts`.
		// If updating this code, make sure to also update that file.
		if (filepath.includes('runtime/client')) {
			// `export default xxxDirective` is a convention used in the current client directives that we use
			// to make sure we bundle this right. We'll error below if this convention isn't followed.
			const newTscode = tscode.replace(
				/export default (.*?)Directive/,
				(_, name) =>
					`(self.Astro || (self.Astro = {})).${name} = ${name}Directive;window.dispatchEvent(new Event('astro:${name}'))`,
			);
			if (newTscode === tscode) {
				console.error(
					colors.red(
						`${filepath} doesn't follow the \`export default xxxDirective\` convention. The prebuilt output may be wrong. ` +
							`For more information, check out ${fileURLToPath(import.meta.url)}`,
					),
				);
			}
			tscode = newTscode;
		}

		const esbuildOptions = {
			stdin: {
				contents: tscode,
				resolveDir: path.dirname(filepath),
				loader: 'ts',
				sourcefile: filepath,
			},
			format: 'iife',
			target: ['es2018'],
			minify,
			bundle: true,
			write: false,
		};

		const results = await Promise.all(
			[
				{
					build: await esbuild.build(esbuildOptions),
					dev: false,
				},
				filepath.includes('astro-island')
					? {
							build: await esbuild.build({
								...esbuildOptions,
								define: { 'process.env.NODE_ENV': '"development"' },
							}),
							dev: true,

Domain

Subdomains

Called By

Frequently Asked Questions

What does prebuild() do?
prebuild() is a function in the astro codebase, defined in scripts/cmd/prebuild.js.
Where is prebuild() defined?
prebuild() is defined in scripts/cmd/prebuild.js at line 12.
What does prebuild() call?
prebuild() calls 1 function(s): escapeTemplateLiterals.
What calls prebuild()?
prebuild() is called by 1 function(s): build.

Analyze Your Own Codebase

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

Try Supermodel Free