π₯ Make videos programmatically with React
#!/usr/bin/env bun
import {
cpSync,
existsSync,
mkdirSync,
readdirSync,
readFileSync,
writeFileSync,
} from 'fs';
import {rm} from 'fs/promises';
import path from 'path';
import {$, build, S3Client, type BuildConfig} from 'bun';
import plugin from 'bun-plugin-tailwind';
import {createElement} from 'react';
import {renderToString} from 'react-dom/server';
import {App} from './src/App';
import {makeLlmsText} from './src/llms';
import variants from './variants.json';
// Print help text if requested
if (process.argv.includes('--help') || process.argv.includes('-h')) {
console.log(`
ποΈ Bun Build Script
Usage: bun run build.ts [options]
Common Options:
--outdir <path> Output directory (default: "dist")
--minify Enable minification (or --minify.whitespace, --minify.syntax, etc)
--source-map <type> Sourcemap type: none|linked|inline|external
--target <target> Build target: browser|bun|node
--format <format> Output format: esm|cjs|iife
--splitting Enable code splitting
--packages <type> Package handling: bundle|external
--public-path <path> Public path for assets
--env <mode> Environment handling: inline|disable|prefix*
--conditions <list> Package.json export conditions (comma separated)
--external <list> External packages (comma separated)
--banner <text> Add banner text to output
--footer <text> Add footer text to output
--define <obj> Define global constants (e.g. --define.VERSION=1.0.0)
--help, -h Show this help message
Example:
bun run build.ts --outdir=dist --minify --source-map=linked --external=react,react-dom
`);
process.exit(0);
}
// Helper function to convert kebab-case to camelCase
const toCamelCase = (str: string): string => {
return str.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
};
// Helper function to parse a value into appropriate type
const parseValue = (value: string): any => {
// Handle true/false strings
if (value === 'true') return true;
if (value === 'false') return false;
// Handle numbers
if (/^\d+$/.test(value)) return parseInt(value, 10);
if (/^\d*\.\d+$/.test(value)) return parseFloat(value);
// Handle arrays (comma-separated)
if (value.includes(',')) return value.split(',').map((v) => v.trim());
// Default to string
return value;
};
// Magical argument parser that converts CLI args to BuildConfig
function parseArgs(): Partial<BuildConfig> {
const config: Record<string, any> = {};
const args = process.argv.slice(2);
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (!arg.startsWith('--')) continue;
// Handle --no-* flags
if (arg.startsWith('--no-')) {
const key = toCamelCase(arg.slice(5));
config[key] = false;
continue;
}
// Handle --flag (boolean true)
if (
!arg.includes('=') &&
(i === args.length - 1 || args[i + 1].startsWith('--'))
) {
const key = toCamelCase(arg.slice(2));
config[key] = true;
continue;
}
// Handle --key=value or --key value
let key: string;
let value: string;
if (arg.includes('=')) {
[key, value] = arg.slice(2).split('=', 2);
} else {
key = arg.slice(2);
value = args[++i];
}
// Convert kebab-case key to camelCase
key = toCamelCase(key);
// Handle nested properties (e.g. --minify.whitespace)
if (key.includes('.')) {
const [parentKey, childKey] = key.split('.');
config[parentKey] = config[parentKey] || {};
config[parentKey][childKey] = parseValue(value);
} else {
config[key] = parseValue(value);
}
}
return config as Partial<BuildConfig>;
}
// Helper function to format file sizes
const formatFileSize = (bytes: number): string => {
const units = ['B', 'KB', 'MB', 'GB'];
let size = bytes;
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
return `${size.toFixed(2)} ${units[unitIndex]}`;
};
const explicitContentTypes: Record<string, string> = {
'.m3u8': 'application/vnd.apple.mpegurl',
'.m4s': 'video/iso.segment',
'.ts': 'video/mp2t',
'.xml': 'application/xml; charset=utf-8',
};
const getContentType = (file: string) => {
return explicitContentTypes[path.extname(file)];
};
const r2Endpoint =
'https://2fe488b3b0f4deee223aef7464784c46.r2.cloudflarestorage.com';
const r2Bucket = 'parser-media';
console.log('\nπ Starting build process...\n');
// Parse CLI arguments with our magical parser
const cliConfig = parseArgs();
const outdir = cliConfig.outdir || path.join(process.cwd(), 'dist');
if (existsSync(outdir)) {
console.log(`ποΈ Cleaning previous build at ${outdir}`);
await rm(outdir, {recursive: true, force: true});
}
const start = performance.now();
// Scan for all HTML files in the project
const entrypoints = [...new Bun.Glob('**.html').scanSync('src')]
.map((a) => path.resolve('src', a))
.filter((dir) => !dir.includes('node_modules'));
console.log(
`π Found ${entrypoints.length} HTML ${
entrypoints.length === 1 ? 'file' : 'files'
} to process\n`,
);
// Build all the HTML files
const result = await build({
entrypoints,
outdir,
plugins: [plugin],
minify: true,
target: 'browser',
sourcemap: 'linked',
define: {
'process.env.NODE_ENV': JSON.stringify('production'),
},
...cliConfig, // Merge in any CLI-provided options
});
const generatedIndexPath = path.join(outdir, 'index.html');
const rootPlaceholder = '<div id="root"></div>';
const generatedIndex = readFileSync(generatedIndexPath, 'utf8');
if (!generatedIndex.includes(rootPlaceholder)) {
throw new Error(`Could not find ${rootPlaceholder} in ${generatedIndexPath}`);
}
const prerenderedIndex = generatedIndex.replace(
rootPlaceholder,
`<div id="root">${renderToString(createElement(App))}</div>`,
);
writeFileSync(generatedIndexPath, prerenderedIndex);
writeFileSync(path.join(outdir, 'llms.txt'), makeLlmsText(variants));
writeFileSync(
path.join(outdir, 'robots.txt'),
[
'User-agent: *',
'Allow: /',
'',
'Sitemap: https://remotion.media/sitemap.xml',
'',
].join('\n'),
);
writeFileSync(
path.join(outdir, 'sitemap.xml'),
[
'<?xml version="1.0" encoding="UTF-8"?>',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
' <url>',
' <loc>https://remotion.media/</loc>',
' </url>',
'</urlset>',
'',
].join('\n'),
);
// Print the results
const end = performance.now();
const outputTable = result.outputs.map((output) => ({
File: path.relative(process.cwd(), output.path),
Type: output.kind,
Size: formatFileSize(output.size),
}));
console.table(outputTable);
const buildTime = (end - start).toFixed(2);
console.log(`\nβ
Build completed in ${buildTime}ms\n`);
const filesDir =
Bun.env.REMOTION_MEDIA_FILES_DIR ?? path.join(process.cwd(), 'files');
mkdirSync(filesDir, {recursive: true});
for await (const file of new Bun.Glob('chunk-*').scan(filesDir)) {
await rm(path.join(filesDir, file));
}
const indexHtml = path.join(filesDir, 'index.html');
if (existsSync(indexHtml)) {
await rm(indexHtml);
}
const agentsMarkdown = path.join(filesDir, 'AGENTS.md');
if (existsSync(agentsMarkdown)) {
await rm(agentsMarkdown);
}
const files = readdirSync(outdir);
for (const file of files) {
cpSync(path.join(outdir, file), path.join(filesDir, file));
}
if (!Bun.env.AWS_ACCESS_KEY_ID || !Bun.env.AWS_SECRET_ACCESS_KEY) {
console.log(
'Skipping R2 upload: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are not set',
);
} else {
const client = new S3Client({
accessKeyId: Bun.env.AWS_ACCESS_KEY_ID,
secretAccessKey: Bun.env.AWS_SECRET_ACCESS_KEY,
endpoint: r2Endpoint,
bucket: r2Bucket,
});
for (const file of readdirSync(filesDir)) {
const filePath = path.join(filesDir, file);
const exists = await client.exists(file);
const contentType = getContentType(file);
const fileToUpload = Bun.file(filePath);
if (exists) {
const stat = await client.stat(file);
if (stat.size === fileToUpload.size && !contentType) {
console.log(
`Skipping ${file} because it already exists and is the same size`,
);
continue;
}
}
if (contentType) {
await $`AWS_DEFAULT_REGION=auto AWS_EC2_METADATA_DISABLED=true aws --endpoint-url ${r2Endpoint} s3 cp ${filePath} ${`s3://${r2Bucket}/${file}`} --content-type ${contentType} --only-show-errors`;
console.log(`Uploaded ${file}`);
continue;
}
await client.write(file, fileToUpload);
console.log(`Uploaded ${file}`);
}
if (await client.exists('AGENTS.md')) {
await client.delete('AGENTS.md');
console.log('Deleted AGENTS.md');
}
}