🎥 Make videos programmatically with React
progress caching
Jonny Burgerf947c745bc5f
diff --git a/packages/bundler/src/index.ts b/packages/bundler/src/index.tsindex 0c82d947ff..5e429db922 100644--- a/packages/bundler/src/index.ts+++ b/packages/bundler/src/index.ts@@ -68,4 +68,5 @@ export const startServer = async (};export * from './bundler';+export {cacheExists} from './webpack-cache';export {overrideWebpackConfig};diff --git a/packages/bundler/src/webpack-cache.ts b/packages/bundler/src/webpack-cache.tsnew file mode 100644index 0000000000..6c89f36199--- /dev/null+++ b/packages/bundler/src/webpack-cache.ts@@ -0,0 +1,51 @@+import fs from 'fs';+import path from 'path';++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+export 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');+ } else if (process.versions.pnp === '1') {+ return path.resolve(dir, '.pnp/.cache/webpack');+ } else if (process.versions.pnp === '3') {+ return path.resolve(dir, '.yarn/.cache/webpack');+ } else {+ return path.resolve(dir, 'node_modules/.cache/webpack');+ }+};++export const getWebpackCacheName = (environment: Environment) => {+ return `remotion-${environment}`;+};++export const cacheExists = (environment: Environment) => {+ return fs.existsSync(+ path.join(getWebpackCacheDir(), getWebpackCacheName(environment))+ );+};diff --git a/packages/bundler/src/webpack-config.ts b/packages/bundler/src/webpack-config.tsindex 65f8fdf31c..5d303c891d 100644--- a/packages/bundler/src/webpack-config.ts+++ b/packages/bundler/src/webpack-config.ts@@ -1,6 +1,7 @@import path from 'path';import {WebpackConfiguration, WebpackOverrideFn} from 'remotion';import webpack, {ProgressPlugin} from 'webpack';+import {getWebpackCacheName} from './webpack-cache';const ErrorOverlayPlugin = require('@webhotelier/webpack-fast-refresh/error-overlay');const ReactRefreshPlugin = require('@webhotelier/webpack-fast-refresh');@@ -26,6 +27,7 @@ export const webpackConfig = ({environment,webpackOverride = (f) => f,onProgressUpdate,+ enableCaching = true,}: {entry: string;userDefinedComponent: string;@@ -33,6 +35,7 @@ export const webpackConfig = ({environment: 'development' | 'production';webpackOverride?: WebpackOverrideFn;onProgressUpdate?: (f: number) => void;+ enableCaching?: boolean;}): WebpackConfiguration => {return webpackOverride({optimization: {@@ -46,6 +49,12 @@ export const webpackConfig = ({entries: false,},},+ cache: enableCaching+ ? {+ type: 'filesystem',+ name: getWebpackCacheName(environment),+ }+ : false,devtool: 'cheap-module-source-map',entry: [environment === 'development'diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsxindex e81558c980..f8c72ff574 100644--- a/packages/cli/src/render.tsx+++ b/packages/cli/src/render.tsx@@ -1,4 +1,4 @@-import {bundle} from '@remotion/bundler';+import {bundle, cacheExists} from '@remotion/bundler';import {ensureLocalBrowser,ffmpegHasFeature,@@ -140,12 +140,16 @@ export const render = async () => {cliProgress.Presets.shades_grey);+ const cacheExistedBefore = cacheExists('production');bundlingProgress.start(100, 0);-const bundled = await bundle(fullPath, (progress) => {bundlingProgress.update(progress);});bundlingProgress.stop();+ const cacheExistedAfter = cacheExists('production');+ if (cacheExistedAfter && !cacheExistedBefore) {+ console.log('⚡️ Cached bundle. Subsequent builds will be faster.');+ }const comps = await getCompositions(bundled,Internals.getBrowser() ?? Internals.DEFAULT_BROWSER