Skip to content

🎥 Make videos programmatically with React

reset webpack cache if config changes

Jonny Burgera10f892137f0
diff --git a/packages/bundler/src/bundle.ts b/packages/bundler/src/bundle.ts
index 67faa6b0a1..b51b5a78b7 100644
--- a/packages/bundler/src/bundle.ts
+++ b/packages/bundler/src/bundle.ts
@@ -38,34 +38,46 @@ const trimTrailingSlash = (p: string): string => {
return p;
};
+type Options = {
+ webpackOverride?: WebpackOverrideFn;
+ outDir?: string;
+ enableCaching?: boolean;
+ publicPath?: string;
+};
+
+export const getConfig = (
+ outDir: string,
+ entryPoint: string,
+ onProgressUpdate?: (progress: number) => void,
+ options?: Options
+) => {
+ return webpackConfig({
+ entry,
+ userDefinedComponent: entryPoint,
+ outDir,
+ environment: 'production',
+ webpackOverride:
+ options?.webpackOverride ?? Internals.defaultOverrideFunction,
+ onProgressUpdate,
+ enableCaching:
+ options?.enableCaching ?? Internals.DEFAULT_WEBPACK_CACHE_ENABLED,
+ maxTimelineTracks: 15,
+ // For production, the variables are set dynamically
+ envVariables: {},
+ entryPoints: [],
+ });
+};
+
export const bundle = async (
entryPoint: string,
onProgressUpdate?: (progress: number) => void,
- options?: {
- webpackOverride?: WebpackOverrideFn;
- outDir?: string;
- enableCaching?: boolean;
- publicPath?: string;
- }
+ options?: Options
): Promise<string> => {
const outDir = await prepareOutDir(options?.outDir ?? null);
- const output = await promisified([
- webpackConfig({
- entry,
- userDefinedComponent: entryPoint,
- outDir,
- environment: 'production',
- webpackOverride:
- options?.webpackOverride ?? Internals.defaultOverrideFunction,
- onProgressUpdate,
- enableCaching:
- options?.enableCaching ?? Internals.DEFAULT_WEBPACK_CACHE_ENABLED,
- maxTimelineTracks: 15,
- // For production, the variables are set dynamically
- envVariables: {},
- entryPoints: [],
- }),
- ]);
+
+ const [, config] = getConfig(outDir, entryPoint, onProgressUpdate, options);
+
+ const output = await promisified([config]);
if (!output) {
throw new Error('Expected webpack output');
}
diff --git a/packages/bundler/src/index.ts b/packages/bundler/src/index.ts
index 3da7443dd8..3f6c4df896 100644
--- a/packages/bundler/src/index.ts
+++ b/packages/bundler/src/index.ts
@@ -1,3 +1,4 @@
+import {getConfig} from './bundle';
import {indexHtml} from './index-html';
import {cacheExists, clearCache} from './webpack-cache';
import {webpackConfig} from './webpack-config';
@@ -10,6 +11,7 @@ export const BundlerInternals = {
indexHtml,
cacheExists,
clearCache,
+ getConfig,
};
export {bundle} from './bundle';
diff --git a/packages/bundler/src/webpack-cache.ts b/packages/bundler/src/webpack-cache.ts
index 8c58bea757..ad34d10ca1 100644
--- a/packages/bundler/src/webpack-cache.ts
+++ b/packages/bundler/src/webpack-cache.ts
@@ -49,29 +49,32 @@ const getWebpackCacheDir = () => {
return path.resolve(dir, 'node_modules/.cache/webpack');
};
-const remotionCacheLocation = (environment: Environment) => {
- return path.join(getWebpackCacheDir(), getWebpackCacheName(environment));
+const remotionCacheLocation = (environment: Environment, hash: string) => {
+ return path.join(
+ getWebpackCacheDir(),
+ getWebpackCacheName(environment, hash)
+ );
};
-export const clearCache = (environment: Environment) => {
+export const clearCache = (environment: Environment, hash: string) => {
return (fs.promises.rm ?? fs.promises.rmdir)(
- remotionCacheLocation(environment),
+ remotionCacheLocation(environment, hash),
{
recursive: true,
}
);
};
-export const getWebpackCacheName = (environment: Environment) => {
+export const getWebpackCacheName = (environment: Environment, hash: string) => {
if (environment === 'development') {
- return `remotion-v3-${environment}`;
+ return `remotion-v3-${environment}-${hash}`;
}
// 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}`;
+ return `remotion-v3-${environment}-${hash}`;
};
-export const cacheExists = (environment: Environment) => {
- return fs.existsSync(remotionCacheLocation(environment));
+export const cacheExists = (environment: Environment, hash: string) => {
+ return fs.existsSync(remotionCacheLocation(environment, hash));
};
diff --git a/packages/bundler/src/webpack-config.ts b/packages/bundler/src/webpack-config.ts
index 7b3a5c464a..b8c25f234e 100644
--- a/packages/bundler/src/webpack-config.ts
+++ b/packages/bundler/src/webpack-config.ts
@@ -1,3 +1,4 @@
+import {createHash} from 'crypto';
import ReactDOM from 'react-dom';
import type {WebpackConfiguration, WebpackOverrideFn} from 'remotion';
import {Internals} from 'remotion';
@@ -54,8 +55,8 @@ export const webpackConfig = ({
envVariables: Record<string, string>;
maxTimelineTracks: number;
entryPoints: string[];
-}): WebpackConfiguration => {
- return webpackOverride({
+}): [string, WebpackConfiguration] => {
+ const conf: webpack.Configuration = {
optimization: {
minimize: false,
},
@@ -71,12 +72,7 @@ export const webpackConfig = ({
aggregateTimeout: 0,
ignored: ['**/.git/**', '**/node_modules/**'],
},
- cache: enableCaching
- ? {
- type: 'filesystem',
- name: getWebpackCacheName(environment),
- }
- : false,
+
devtool:
environment === 'development'
? 'cheap-module-source-map'
@@ -117,7 +113,6 @@ export const webpackConfig = ({
hashFunction: 'xxhash64',
globalObject: 'this',
filename: 'bundle.js',
- path: outDir,
devtoolModuleFilenameTemplate: '[resource-path]',
assetModuleFilename:
environment === 'development' ? '[path][name][ext]' : '[hash][ext]',
@@ -182,5 +177,23 @@ export const webpackConfig = ({
},
],
},
- });
+ };
+ const hash = createHash('md5').update(JSON.stringify(conf)).digest('hex');
+ return [
+ hash,
+ webpackOverride({
+ ...conf,
+ cache: enableCaching
+ ? {
+ type: 'filesystem',
+ name: getWebpackCacheName(environment, hash),
+ version: hash,
+ }
+ : false,
+ output: {
+ ...conf.output,
+ path: outDir,
+ },
+ }),
+ ];
};
diff --git a/packages/cli/src/preview-server/start-server.ts b/packages/cli/src/preview-server/start-server.ts
index 073c0882c0..f75fd684e7 100644
--- a/packages/cli/src/preview-server/start-server.ts
+++ b/packages/cli/src/preview-server/start-server.ts
@@ -31,7 +31,7 @@ export const startServer = async (
path.join(os.tmpdir(), 'react-motion-graphics')
);
- const config = BundlerInternals.webpackConfig({
+ const [, config] = BundlerInternals.webpackConfig({
entry,
userDefinedComponent,
outDir: tmpDir,
diff --git a/packages/cli/src/setup-cache.ts b/packages/cli/src/setup-cache.ts
index 6a3a3c201a..d09d0005d6 100644
--- a/packages/cli/src/setup-cache.ts
+++ b/packages/cli/src/setup-cache.ts
@@ -16,14 +16,34 @@ export const bundleOnCli = async ({
steps: RenderStep[];
}) => {
const shouldCache = Internals.getWebpackCaching();
- const cacheExistedBefore = BundlerInternals.cacheExists('production');
+
+ const onProgress = (progress: number) => {
+ bundlingProgress.update(
+ makeBundlingProgress({
+ progress: progress / 100,
+ steps,
+ doneIn: null,
+ })
+ );
+ };
+
+ const options = {
+ enableCaching: shouldCache,
+ webpackOverride:
+ Internals.getWebpackOverrideFn() ?? Internals.defaultOverrideFunction,
+ };
+
+ const [hash] = BundlerInternals.getConfig('', fullPath, onProgress, options);
+
+ const cacheExistedBefore = BundlerInternals.cacheExists('production', hash);
if (cacheExistedBefore && !shouldCache) {
Log.info('🧹 Cache disabled but found. Deleting... ');
- await BundlerInternals.clearCache('production');
+ await BundlerInternals.clearCache('production', hash);
}
const bundleStartTime = Date.now();
const bundlingProgress = createOverwriteableCliOutput(quietFlagProvided());
+
const bundled = await bundle(
fullPath,
(progress) => {
@@ -35,11 +55,7 @@ export const bundleOnCli = async ({
})
);
},
- {
- enableCaching: shouldCache,
- webpackOverride:
- Internals.getWebpackOverrideFn() ?? Internals.defaultOverrideFunction,
- }
+ options
);
bundlingProgress.update(
makeBundlingProgress({
@@ -49,7 +65,7 @@ export const bundleOnCli = async ({
}) + '\n'
);
Log.verbose('Bundled under', bundled);
- const cacheExistedAfter = BundlerInternals.cacheExists('production');
+ const cacheExistedAfter = BundlerInternals.cacheExists('production', hash);
if (cacheExistedAfter && !cacheExistedBefore) {
Log.info('⚡️ Cached bundle. Subsequent renders will be faster.');
}