Skip to content

πŸŽ₯ Make videos programmatically with React

Replace suppressHmrForFile with suppressBundlerUpdateForFile everywhere

Jonny Burgerabd34199cfdb
diff --git a/packages/bundler/src/watch-ignore-next-change-plugin.ts b/packages/bundler/src/watch-ignore-next-change-plugin.ts
index 11fac4325c..12d85c8859 100644
--- a/packages/bundler/src/watch-ignore-next-change-plugin.ts
+++ b/packages/bundler/src/watch-ignore-next-change-plugin.ts
@@ -93,6 +93,18 @@ export class WatchIgnoreNextChangePlugin {
if (dirTs !== undefined) {
this.snapshotDirTimestamps.set(dir, dirTs);
}
+
+ // Also snapshot ancestor directories β€” watchpack may report them
+ // as changed alongside the file itself.
+ let ancestor = path.dirname(dir);
+ while (ancestor !== path.dirname(ancestor)) {
+ const ancestorTs = info.contextTimeInfoEntries.get(ancestor);
+ if (ancestorTs !== undefined) {
+ this.snapshotDirTimestamps.set(ancestor, ancestorTs);
+ }
+
+ ancestor = path.dirname(ancestor);
+ }
}
this.trace('[WatchIgnoreNextChange] Registered ignore for', file);
@@ -126,13 +138,24 @@ export class WatchIgnoreNextChangePlugin {
callback,
callbackUndelayed,
) => {
+ const isAncestorOfIgnoredFile = (dir: string): boolean => {
+ for (const file of self.filesToIgnore) {
+ if (file.startsWith(dir + path.sep)) {
+ return true;
+ }
+ }
+
+ return false;
+ };
+
const wrappedCallbackUndelayed: CallbackUndelayed = (
fileName,
changeTime,
) => {
if (
self.filesToIgnore.has(fileName) ||
- self.dirsToIgnore.has(fileName)
+ self.dirsToIgnore.has(fileName) ||
+ isAncestorOfIgnoredFile(fileName)
) {
self.trace(
'[WatchIgnoreNextChange] Suppressed callbackUndelayed for',
@@ -205,6 +228,29 @@ export class WatchIgnoreNextChangePlugin {
}
}
+ // Remove ancestor directories of suppressed files from changedFiles.
+ // Watchpack may report parent directories (e.g. /src) as changed
+ // alongside the file itself (e.g. /src/Sub/index.tsx).
+ // dirsToIgnore only has the immediate parent, so ancestors
+ // like /src would remain and trigger a rebuild.
+ if (changedFiles && suppressedFiles.length > 0) {
+ for (const changed of [...changedFiles]) {
+ for (const file of suppressedFiles) {
+ if (file.startsWith(changed + path.sep)) {
+ changedFiles.delete(changed);
+ if (dirTimestamps) {
+ const prev = self.snapshotDirTimestamps.get(changed);
+ if (prev !== undefined) {
+ dirTimestamps.set(changed, prev);
+ }
+ }
+
+ break;
+ }
+ }
+ }
+ }
+
const remainingChanges = changedFiles?.size ?? 0;
const remainingRemovals = removedFiles?.size ?? 0;
@@ -288,6 +334,16 @@ export class WatchIgnoreNextChangePlugin {
watchpack.aggregatedChanges.delete(dir);
watchpack.aggregatedRemovals.delete(dir);
}
+
+ // Also clear ancestor directories of suppressed files
+ for (const file of suppressedFiles) {
+ let ancestor = path.dirname(path.dirname(file));
+ while (ancestor !== path.dirname(ancestor)) {
+ watchpack.aggregatedChanges.delete(ancestor);
+ watchpack.aggregatedRemovals.delete(ancestor);
+ ancestor = path.dirname(ancestor);
+ }
+ }
} else {
callback(
err,
diff --git a/packages/studio-server/src/preview-server/hmr-suppression.ts b/packages/studio-server/src/preview-server/hmr-suppression.ts
deleted file mode 100644
index 50905ea394..0000000000
--- a/packages/studio-server/src/preview-server/hmr-suppression.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-const suppressedFiles = new Map<string, number>();
-
-export function suppressHmrForFile(absolutePath: string) {
- suppressedFiles.set(
- absolutePath,
- (suppressedFiles.get(absolutePath) ?? 0) + 1,
- );
-}
-
-export function shouldSuppressHmr(filename: string | null): boolean {
- if (filename === null) {
- return false;
- }
-
- const count = suppressedFiles.get(filename) ?? 0;
- if (count > 0) {
- if (count === 1) {
- suppressedFiles.delete(filename);
- } else {
- suppressedFiles.set(filename, count - 1);
- }
-
- return true;
- }
-
- return false;
-}
diff --git a/packages/studio-server/src/preview-server/hot-middleware/index.tsx b/packages/studio-server/src/preview-server/hot-middleware/index.tsx
index d6f06aa06b..a929748e13 100644
--- a/packages/studio-server/src/preview-server/hot-middleware/index.tsx
+++ b/packages/studio-server/src/preview-server/hot-middleware/index.tsx
@@ -11,7 +11,6 @@ import type {LogLevel} from '@remotion/renderer';
import {RenderInternals} from '@remotion/renderer';
import type {HotMiddlewareMessage, ModuleMap} from '@remotion/studio-shared';
import {hotMiddlewareOptions} from '@remotion/studio-shared';
-import {shouldSuppressHmr} from '../hmr-suppression';
import type {WebpackStats} from './types';
declare global {
@@ -137,19 +136,11 @@ export const webpackHotMiddleware = (
hotMiddlewareOptions.heartbeat,
);
let latestStats: webpack.Stats | null = null;
- let currentBuildSuppressed = false;
compiler.hooks.invalid.tap('remotion', onInvalid);
compiler.hooks.done.tap('remotion', onDone);
- function onInvalid(filename: string | null) {
- if (shouldSuppressHmr(filename)) {
- currentBuildSuppressed = true;
- latestStats = null;
- return;
- }
-
- currentBuildSuppressed = false;
+ function onInvalid() {
latestStats = null;
RenderInternals.Log.info({indent: false, logLevel}, 'Building...');
eventStream?.publish({
@@ -160,9 +151,7 @@ export const webpackHotMiddleware = (
function onDone(statsResult: webpack.Stats) {
// Keep hold of latest stats so they can be propagated to new clients
latestStats = statsResult;
- publishStats('built', latestStats, eventStream, currentBuildSuppressed);
-
- currentBuildSuppressed = false;
+ publishStats('built', latestStats, eventStream);
}
const middleware = function (
@@ -173,7 +162,7 @@ export const webpackHotMiddleware = (
if (!pathMatch(req.url as string, hotMiddlewareOptions.path)) return next();
eventStream?.handler(req, res);
if (latestStats) {
- publishStats('sync', latestStats, eventStream, false);
+ publishStats('sync', latestStats, eventStream);
}
};
@@ -241,7 +230,6 @@ function publishStats(
action: HotMiddlewareMessage['action'],
statsResult: webpack.Stats,
eventStream: EventStream | null,
- suppressed: boolean,
) {
const stats = statsResult.toJson({
all: false,
@@ -269,7 +257,6 @@ function publishStats(
warnings: _stats.warnings || [],
errors: _stats.errors || [],
modules: buildModuleMap(_stats.modules),
- suppressed,
});
});
}
diff --git a/packages/studio-server/src/preview-server/routes/apply-visual-control-change.ts b/packages/studio-server/src/preview-server/routes/apply-visual-control-change.ts
index 223c5d27c7..28cf4c1f02 100644
--- a/packages/studio-server/src/preview-server/routes/apply-visual-control-change.ts
+++ b/packages/studio-server/src/preview-server/routes/apply-visual-control-change.ts
@@ -10,13 +10,13 @@ import {applyCodemod} from '../../codemods/recast-mods';
import {writeFileAndNotifyFileWatchers} from '../../file-watcher';
import {makeHyperlink} from '../../hyperlinks/make-link';
import type {ApiHandler} from '../api-types';
-import {suppressHmrForFile} from '../hmr-suppression';
import {waitForLiveEventsListener} from '../live-events';
import {
printUndoHint,
pushToUndoStack,
suppressUndoStackInvalidation,
} from '../undo-stack';
+import {suppressBundlerUpdateForFile} from '../watch-ignore-next-change';
import {warnAboutPrettierOnce} from './log-update';
export const applyVisualControlHandler: ApiHandler<
@@ -83,7 +83,7 @@ export const applyVisualControlHandler: ApiHandler<
'visual-control',
);
suppressUndoStackInvalidation(absolutePath);
- suppressHmrForFile(absolutePath);
+ suppressBundlerUpdateForFile(absolutePath);
writeFileAndNotifyFileWatchers(absolutePath, output);
waitForLiveEventsListener().then((listener) => {
diff --git a/packages/studio-server/src/preview-server/routes/save-sequence-props.ts b/packages/studio-server/src/preview-server/routes/save-sequence-props.ts
index 397d884323..7d647ca77c 100644
--- a/packages/studio-server/src/preview-server/routes/save-sequence-props.ts
+++ b/packages/studio-server/src/preview-server/routes/save-sequence-props.ts
@@ -7,12 +7,12 @@ import type {
import {updateSequenceProps} from '../../codemods/update-sequence-props';
import {writeFileAndNotifyFileWatchers} from '../../file-watcher';
import type {ApiHandler} from '../api-types';
-import {suppressHmrForFile} from '../hmr-suppression';
import {
printUndoHint,
pushToUndoStack,
suppressUndoStackInvalidation,
} from '../undo-stack';
+import {suppressBundlerUpdateForFile} from '../watch-ignore-next-change';
import {computeSequencePropsStatus} from './can-update-sequence-props';
import {formatPropChange, logUpdate, normalizeQuotes} from './log-update';
@@ -77,7 +77,7 @@ export const saveSequencePropsHandler: ApiHandler<
'sequence-props',
);
suppressUndoStackInvalidation(absolutePath);
- suppressHmrForFile(absolutePath);
+ suppressBundlerUpdateForFile(absolutePath);
writeFileAndNotifyFileWatchers(absolutePath, output);
logUpdate({
diff --git a/packages/studio-server/src/preview-server/routes/update-default-props.ts b/packages/studio-server/src/preview-server/routes/update-default-props.ts
index c5fac9966d..ab3b9c363c 100644
--- a/packages/studio-server/src/preview-server/routes/update-default-props.ts
+++ b/packages/studio-server/src/preview-server/routes/update-default-props.ts
@@ -9,7 +9,6 @@ import {updateDefaultProps} from '../../codemods/update-default-props';
import {writeFileAndNotifyFileWatchers} from '../../file-watcher';
import {makeHyperlink} from '../../hyperlinks/make-link';
import type {ApiHandler} from '../api-types';
-import {suppressHmrForFile} from '../hmr-suppression';
import {getProjectInfo} from '../project-info';
import {
printUndoHint,
@@ -57,7 +56,6 @@ export const updateDefaultPropsHandler: ApiHandler<
'default-props',
);
suppressUndoStackInvalidation(projectInfo.rootFile);
- suppressHmrForFile(projectInfo.rootFile);
suppressBundlerUpdateForFile(projectInfo.rootFile);
writeFileAndNotifyFileWatchers(projectInfo.rootFile, output);
diff --git a/packages/studio-server/src/preview-server/undo-stack.ts b/packages/studio-server/src/preview-server/undo-stack.ts
index d15bad5159..ac517f414e 100644
--- a/packages/studio-server/src/preview-server/undo-stack.ts
+++ b/packages/studio-server/src/preview-server/undo-stack.ts
@@ -9,7 +9,6 @@ import {
writeFileAndNotifyFileWatchers,
} from '../file-watcher';
import {makeHyperlink} from '../hyperlinks/make-link';
-import {suppressHmrForFile} from './hmr-suppression';
import {waitForLiveEventsListener} from './live-events';
import {suppressBundlerUpdateForFile} from './watch-ignore-next-change';
@@ -255,7 +254,6 @@ export function popUndo(): {success: true} | {success: false; reason: string} {
});
suppressUndoStackInvalidation(entry.filePath);
- suppressHmrForFile(entry.filePath);
suppressBundlerUpdateForFile(entry.filePath);
writeFileAndNotifyFileWatchers(entry.filePath, entry.oldContents);
@@ -291,8 +289,8 @@ export function popRedo(): {success: true} | {success: false; reason: string} {
});
suppressUndoStackInvalidation(entry.filePath);
- suppressHmrForFile(entry.filePath);
suppressBundlerUpdateForFile(entry.filePath);
+
writeFileAndNotifyFileWatchers(entry.filePath, entry.oldContents);
RenderInternals.Log.verbose(
diff --git a/packages/studio-shared/src/hot-middleware.ts b/packages/studio-shared/src/hot-middleware.ts
index 4415bff0e4..54de454e6d 100644
--- a/packages/studio-shared/src/hot-middleware.ts
+++ b/packages/studio-shared/src/hot-middleware.ts
@@ -13,7 +13,6 @@ export type HotMiddlewareMessage =
modules: {
[key: string]: string;
};
- suppressed: boolean;
};
export const hotMiddlewareOptions = {
diff --git a/packages/studio/src/hot-middleware-client/client.ts b/packages/studio/src/hot-middleware-client/client.ts
index 78223bd274..82d84a95ed 100644
--- a/packages/studio/src/hot-middleware-client/client.ts
+++ b/packages/studio/src/hot-middleware-client/client.ts
@@ -180,12 +180,7 @@ function processMessage(obj: HotMiddlewareMessage) {
if (applyUpdate) {
window.remotion_finishedBuilding?.();
- processUpdate(
- obj.hash,
- obj.modules,
- hotMiddlewareOptions,
- obj.suppressed,
- );
+ processUpdate(obj.hash, obj.modules, hotMiddlewareOptions);
}
break;
diff --git a/packages/studio/src/hot-middleware-client/process-update.ts b/packages/studio/src/hot-middleware-client/process-update.ts
index d3d6f71d7c..cfbea1bf9d 100644
--- a/packages/studio/src/hot-middleware-client/process-update.ts
+++ b/packages/studio/src/hot-middleware-client/process-update.ts
@@ -65,12 +65,7 @@ export const processUpdate = function (
hash: string | undefined,
moduleMap: ModuleMap,
options: HotMiddlewareOptions,
- suppressed: boolean,
) {
- if (suppressed) {
- return;
- }
-
async function check() {
const cb = function (err: Error | null, updatedModules: ModuleId[] | null) {
if (err) return handleError(err);