Skip to content

🎥 Make videos programmatically with React

alright

Jonny Burger2f5f967df214
diff --git a/packages/bundler/src/index.ts b/packages/bundler/src/index.ts
index 1888345fa6..3da7443dd8 100644
--- a/packages/bundler/src/index.ts
+++ b/packages/bundler/src/index.ts
@@ -1,4 +1,5 @@
import {indexHtml} from './index-html';
+import {cacheExists, clearCache} from './webpack-cache';
import {webpackConfig} from './webpack-config';
import esbuild = require('esbuild');
import webpack = require('webpack');
@@ -7,6 +8,8 @@ export const BundlerInternals = {
esbuild,
webpackConfig,
indexHtml,
+ cacheExists,
+ clearCache,
};
export {bundle} from './bundle';
diff --git a/packages/bundler/src/p-limit.ts b/packages/bundler/src/p-limit.ts
deleted file mode 100644
index a56ab84de9..0000000000
--- a/packages/bundler/src/p-limit.ts
+++ /dev/null
@@ -1,75 +0,0 @@
-export const pLimit = (concurrency: number) => {
- const queue: Function[] = [];
- let activeCount = 0;
-
- const next = () => {
- activeCount--;
-
- if (queue.length > 0) {
- queue.shift()?.();
- }
- };
-
- const run = async <Arguments extends unknown[], ReturnType>(
- fn: (..._arguments: Arguments) => PromiseLike<ReturnType> | ReturnType,
- resolve: (res: Promise<ReturnType>) => void,
- ...args: Arguments
- ) => {
- activeCount++;
-
- // eslint-disable-next-line require-await
- const result = (async () => fn(...args))();
-
- resolve(result);
-
- try {
- await result;
- } catch {}
-
- next();
- };
-
- const enqueue = <Arguments extends unknown[], ReturnType>(
- fn: (..._arguments: Arguments) => PromiseLike<ReturnType> | ReturnType,
- resolve: (res: Promise<ReturnType>) => void,
- ...args: Arguments
- ) => {
- queue.push(() => run(fn, resolve, ...args));
-
- (async () => {
- // This function needs to wait until the next microtask before comparing
- // `activeCount` to `concurrency`, because `activeCount` is updated asynchronously
- // when the run function is dequeued and called. The comparison in the if-statement
- // needs to happen asynchronously as well to get an up-to-date value for `activeCount`.
- await Promise.resolve();
-
- if (activeCount < concurrency && queue.length > 0) {
- queue.shift()?.();
- }
- })();
- };
-
- const generator = <Arguments extends unknown[], ReturnType>(
- fn: (..._arguments: Arguments) => PromiseLike<ReturnType> | ReturnType,
- ...args: Arguments
- ) =>
- new Promise<ReturnType>((resolve) => {
- enqueue(fn, resolve, ...args);
- });
-
- Object.defineProperties(generator, {
- activeCount: {
- get: () => activeCount,
- },
- pendingCount: {
- get: () => queue.length,
- },
- clearQueue: {
- value: () => {
- queue.length = 0;
- },
- },
- });
-
- return generator;
-};
diff --git a/packages/cli/src/preview-server/webpack-cache.ts b/packages/cli/src/preview-server/webpack-cache.ts
deleted file mode 100644
index 1d38c7eafd..0000000000
--- a/packages/cli/src/preview-server/webpack-cache.ts
+++ /dev/null
@@ -1,96 +0,0 @@
-import fs from 'fs';
-import path from 'path';
-import {random} from 'remotion';
-
-type Environment = 'development' | 'production';
-
-declare global {
- // eslint-disable-next-line @typescript-eslint/no-namespace
- namespace NodeJS {
- interface ProcessVersions {
- pnp?: string;
- }
- }
-}
-
-// Inlined from https://github.com/webpack/webpack/blob/4c2ee7a4ddb8db2362ca83b6c4190523387ba7ee/lib/config/defaults.js#L265
-// An algorithm to determine where Webpack will cache the depencies
-const getWebpackCacheDir = () => {
- const cwd = process.cwd();
- let dir: string | undefined = cwd;
- for (;;) {
- try {
- if (fs.statSync(path.join(dir, 'package.json')).isFile()) {
- break;
- }
- // eslint-disable-next-line no-empty
- } catch (e) {}
-
- const parent = path.dirname(dir);
- if (dir === parent) {
- dir = undefined;
- break;
- }
-
- dir = parent;
- }
-
- if (!dir) {
- return path.resolve(cwd, '.cache/webpack');
- }
-
- if (process.versions.pnp === '1') {
- return path.resolve(dir, '.pnp/.cache/webpack');
- }
-
- if (process.versions.pnp === '3') {
- return path.resolve(dir, '.yarn/.cache/webpack');
- }
-
- return path.resolve(dir, 'node_modules/.cache/webpack');
-};
-
-const remotionCacheLocation = (
- environment: Environment,
- inputProps: object | null
-) => {
- return path.join(
- getWebpackCacheDir(),
- getWebpackCacheName(environment, inputProps)
- );
-};
-
-export const clearCache = (
- environment: Environment,
- inputProps: object | null
-) => {
- return (fs.promises.rm ?? fs.promises.rmdir)(
- remotionCacheLocation(environment, inputProps),
- {
- recursive: true,
- }
- );
-};
-
-export const getWebpackCacheName = (
- environment: Environment,
- inputProps: object | null
-) => {
- // In development, let's reset the cache when input props
- // are changing, because they are injected using Webpack and if changed,
- // it will get the cached version
- if (environment === 'development') {
- return `remotion-v3-${environment}-${random(JSON.stringify(inputProps))}`;
- }
-
- // In production, the cache is independent from input props because
- // they are passed over URL params. Speed is mostly important in production.
- return `remotion-v3-${environment}`;
-};
-
-export const cacheExists = (
- environment: Environment,
- inputProps: object | null
-) => {
- return fs.existsSync(remotionCacheLocation(environment, inputProps));
-};
diff --git a/packages/cli/src/setup-cache.ts b/packages/cli/src/setup-cache.ts
index 4b16524a74..c55d9671bf 100644
--- a/packages/cli/src/setup-cache.ts
+++ b/packages/cli/src/setup-cache.ts
@@ -1,4 +1,4 @@
-import {bundle} from '@remotion/bundler';
+import {bundle, BundlerInternals} from '@remotion/bundler';
import {Internals} from 'remotion';
import {Log} from './log';
import {quietFlagProvided} from './parse-command-line';
@@ -7,14 +7,13 @@ import {
makeBundlingProgress,
} from './progress-bar';
import {RenderStep} from './step';
-import {cacheExists, clearCache} from './webpack-cache';
export const bundleOnCli = async (fullPath: string, steps: RenderStep[]) => {
const shouldCache = Internals.getWebpackCaching();
- const cacheExistedBefore = cacheExists('production', null);
+ const cacheExistedBefore = BundlerInternals.cacheExists('production', null);
if (cacheExistedBefore && !shouldCache) {
Log.info('🧹 Cache disabled but found. Deleting... ');
- await clearCache('production', null);
+ await BundlerInternals.clearCache('production', null);
}
const bundleStartTime = Date.now();