Skip to content

🎥 Make videos programmatically with React

new bundle() signature

Jonny Burger2cbc894cbf2c
diff --git a/packages/bundler/src/bundle.ts b/packages/bundler/src/bundle.ts
index c6d458ce9c..b045f58886 100644
--- a/packages/bundler/src/bundle.ts
+++ b/packages/bundler/src/bundle.ts
@@ -9,8 +9,6 @@ import {copyDir} from './copy-dir';
import {indexHtml} from './index-html';
import {webpackConfig} from './webpack-config';
-const entry = require.resolve('./renderEntry');
-
const promisified = promisify(webpack);
const prepareOutDir = async (specified: string | null) => {
@@ -38,7 +36,7 @@ const trimTrailingSlash = (p: string): string => {
return p;
};
-export type BundleOptions = {
+export type LegacyBundleOptions = {
webpackOverride?: WebpackOverrideFn;
outDir?: string;
enableCaching?: boolean;
@@ -58,8 +56,10 @@ export const getConfig = ({
entryPoint: string;
resolvedRemotionRoot: string;
onProgressUpdate?: (progress: number) => void;
- options?: BundleOptions;
+ options?: LegacyBundleOptions;
}) => {
+ const entry = require.resolve('./renderEntry');
+
return webpackConfig({
entry,
userDefinedComponent: entryPoint,
@@ -77,14 +77,45 @@ export const getConfig = ({
});
};
-export const bundle = async (
- entryPoint: string,
- onProgressUpdate?: (progress: number) => void,
- options?: BundleOptions
-): Promise<string> => {
- const resolvedRemotionRoot = options?.rootDir ?? process.cwd();
+export type BundleOptions = {
+ entryPoint: string;
+ onProgressUpdate?: (progress: number) => void;
+} & LegacyBundleOptions;
+
+type Arguments =
+ | [options: BundleOptions]
+ | [
+ entryPoint: string,
+ onProgressUpdate?: (progress: number) => void,
+ options?: LegacyBundleOptions
+ ];
+
+const convertArgumentsIntoOptions = (args: Arguments): BundleOptions => {
+ if ((args.length as number) === 0) {
+ throw new TypeError('bundle() was called without arguments');
+ }
- const outDir = await prepareOutDir(options?.outDir ?? null);
+ const firstArg = args[0];
+ if (typeof firstArg === 'string') {
+ return {
+ entryPoint: firstArg,
+ onProgressUpdate: args[1],
+ ...(args[2] ?? {}),
+ };
+ }
+
+ if (typeof firstArg.entryPoint !== 'string') {
+ throw new TypeError('bundle() was called without the `entryPoint` option');
+ }
+
+ return firstArg;
+};
+
+export async function bundle(...args: Arguments): Promise<string> {
+ const actualArgs = convertArgumentsIntoOptions(args);
+ const resolvedRemotionRoot = actualArgs?.rootDir ?? process.cwd();
+
+ const outDir = await prepareOutDir(actualArgs?.outDir ?? null);
// The config might use an override which might use
// `process.cwd()`. The context should always be the Remotion root.
@@ -94,6 +125,7 @@ export const bundle = async (
process.chdir(resolvedRemotionRoot);
}
+ const {entryPoint, onProgressUpdate, ...options} = actualArgs;
const [, config] = getConfig({
outDir,
entryPoint,
@@ -116,7 +148,7 @@ export const bundle = async (
throw new Error(errors[0].message + '\n' + errors[0].details);
}
- const baseDir = options?.publicPath ?? '/';
+ const baseDir = actualArgs?.publicPath ?? '/';
const staticHash =
'/' +
[trimTrailingSlash(trimLeadingSlash(baseDir)), 'public']
@@ -142,4 +174,4 @@ export const bundle = async (
fs.writeFileSync(path.join(outDir, 'index.html'), html);
return outDir;
-};
+}
diff --git a/packages/bundler/src/index.ts b/packages/bundler/src/index.ts
index dec24fe282..72fc4d5abe 100644
--- a/packages/bundler/src/index.ts
+++ b/packages/bundler/src/index.ts
@@ -14,7 +14,7 @@ export const BundlerInternals = {
getConfig,
};
-export {bundle, BundleOptions} from './bundle';
+export {bundle, BundleOptions, LegacyBundleOptions} from './bundle';
export {webpack};
export type WebpackConfiguration = webpack.Configuration;
diff --git a/packages/bundler/src/test/validate-bundle.test.ts b/packages/bundler/src/test/validate-bundle.test.ts
new file mode 100644
index 0000000000..6e970976e7
--- /dev/null
+++ b/packages/bundler/src/test/validate-bundle.test.ts
@@ -0,0 +1,14 @@
+import {expect, test} from 'vitest';
+import {bundle} from '../bundle';
+
+test('Should reject bad bundle options', () => {
+ // @ts-expect-error
+ expect(() => bundle()).rejects.toThrow(
+ /bundle\(\) was called without arguments/
+ );
+
+ // @ts-expect-error
+ expect(() => bundle({})).rejects.toThrow(
+ /bundle\(\) was called without the `entryPoint` option/
+ );
+});