Skip to content

🎥 Make videos programmatically with React

Update builder.ts

Jonny Burgereb7ca23b2c20
diff --git a/packages/.monorepo/builder.ts b/packages/.monorepo/builder.ts
index 800690a446..0ca280bf38 100644
--- a/packages/.monorepo/builder.ts
+++ b/packages/.monorepo/builder.ts
@@ -1,4 +1,5 @@
import {build} from 'bun';
+import {existsSync} from 'node:fs';
import path from 'path';
if (process.env.NODE_ENV !== 'production') {
@@ -7,6 +8,42 @@ if (process.env.NODE_ENV !== 'production') {
type Format = 'esm' | 'cjs';
+const validateExports = (
+ exports: Record<string, './package.json' | Record<string, string>>,
+) => {
+ const keys = Object.keys(exports);
+ for (const key of keys) {
+ const value = exports[key];
+ if (key === './package.json' && value === './package.json') {
+ continue;
+ }
+
+ if (typeof value === 'string') {
+ throw new Error(`Invalid export for ${key}`);
+ }
+
+ if (!value.import || !value.require || !value.module || !value) {
+ throw new Error(`Missing import or require for ${key}`);
+ }
+ const paths = Object.keys(value);
+ for (const entry of paths) {
+ if (
+ entry !== 'import' &&
+ entry !== 'require' &&
+ entry !== 'module' &&
+ entry !== 'types'
+ ) {
+ throw new Error(`Invalid export: ${entry}`);
+ }
+ const pathToCheck = path.join(process.cwd(), value[entry]);
+ const exists = existsSync(pathToCheck);
+ if (!exists) {
+ throw new Error(`Path does not exist: ${pathToCheck}`);
+ }
+ }
+ }
+};
+
export const buildPackage = async ({
formats,
external,
@@ -18,8 +55,13 @@ export const buildPackage = async ({
target: 'node' | 'browser';
entrypoints: string[];
}) => {
+ console.time(`Generated ${formats.join(', ')}.`);
+ const pkg = await Bun.file(path.join(process.cwd(), 'package.json')).json();
+ const newExports = {
+ ...pkg.exports,
+ };
+
for (const format of formats) {
- console.time(`Generated ${format}.`);
const output = await build({
entrypoints: entrypoints.map((e) => path.join(process.cwd(), e)),
naming: `[name].${format === 'esm' ? 'mjs' : 'js'}`,
@@ -31,13 +73,41 @@ export const buildPackage = async ({
throw new Error('Build failed');
}
- const [file] = output.outputs;
- const text = await file.text();
+ for (const file of output.outputs) {
+ const text = await file.text();
+
+ const outputPath = './' + path.join('./dist', format, file.path);
+ await Bun.write(path.join(process.cwd(), outputPath), text);
- await Bun.write(
- `dist/${format}/index.${format === 'esm' ? 'mjs' : 'js'}`,
- text,
- );
- console.timeEnd(`Generated ${format}.`);
+ const firstName = file.path.split('.')[1].slice(1);
+ const exportName = firstName === 'index' ? '.' : firstName;
+ newExports[exportName] = {
+ ...(newExports[exportName] ?? {}),
+ ...(format === 'esm'
+ ? {
+ import: outputPath,
+ module: outputPath,
+ }
+ : {}),
+ ...(format === 'cjs'
+ ? {
+ require: outputPath,
+ }
+ : {}),
+ };
+ }
}
+ validateExports(newExports);
+ await Bun.write(
+ path.join(process.cwd(), 'package.json'),
+ JSON.stringify(
+ {
+ ...pkg,
+ exports: newExports,
+ },
+ null,
+ '\t',
+ ) + '\n',
+ );
+ console.timeEnd(`Generated ${formats.join(', ')}.`);
};