Skip to content

🎥 Make videos programmatically with React

JS HMR

Jonny Burger16ccddf6311c
diff --git a/packages/bundler/package.json b/packages/bundler/package.json
index d8b146071e..cb46e0cedb 100644
--- a/packages/bundler/package.json
+++ b/packages/bundler/package.json
@@ -39,7 +39,6 @@
"@types/node": "^16.7.5",
"@types/react": "18.0.26",
"@types/react-dom": "18.0.10",
- "@types/webpack-env": "^1.16.0",
"@typescript-eslint/eslint-plugin": "5.18.0",
"@typescript-eslint/parser": "5.18.0",
"eslint": "8.25.0",
diff --git a/packages/bundler/src/fast-refresh/helpers.ts b/packages/bundler/src/fast-refresh/helpers.ts
index f04265f6ed..cbc4e1d3f0 100644
--- a/packages/bundler/src/fast-refresh/helpers.ts
+++ b/packages/bundler/src/fast-refresh/helpers.ts
@@ -159,11 +159,11 @@ function scheduleUpdate() {
};
// Only trigger refresh if the webpack HMR state is idle
- if (module.hot?.status() === 'idle') {
+ if (__webpack_module__.hot?.status() === 'idle') {
return;
}
- module.hot?.addStatusHandler((status) => {
+ __webpack_module__.hot?.addStatusHandler((status) => {
if (status === 'idle') {
execute();
}
diff --git a/packages/bundler/src/fast-refresh/loader.ts b/packages/bundler/src/fast-refresh/loader.ts
index 117d7a1fe9..4ec7b64eb5 100644
--- a/packages/bundler/src/fast-refresh/loader.ts
+++ b/packages/bundler/src/fast-refresh/loader.ts
@@ -35,6 +35,113 @@
import type {LoaderDefinition} from 'webpack';
+declare global {
+ const __webpack_hash__: unknown;
+
+ interface HotNotifierInfo {
+ type:
+ | 'self-declined'
+ | 'declined'
+ | 'unaccepted'
+ | 'accepted'
+ | 'disposed'
+ | 'accept-errored'
+ | 'self-accept-errored'
+ | 'self-accept-error-handler-errored';
+ /**
+ * The module in question.
+ */
+ moduleId: number;
+ /**
+ * For errors: the module id owning the accept handler.
+ */
+ dependencyId?: number | undefined;
+ /**
+ * For declined/accepted/unaccepted: the chain from where the update was propagated.
+ */
+ chain?: number[] | undefined;
+ /**
+ * For declined: the module id of the declining parent
+ */
+ parentId?: number | undefined;
+ /**
+ * For accepted: the modules that are outdated and will be disposed
+ */
+ outdatedModules?: number[] | undefined;
+ /**
+ * For accepted: The location of accept handlers that will handle the update
+ */
+ outdatedDependencies?:
+ | {
+ [dependencyId: number]: number[];
+ }
+ | undefined;
+ /**
+ * For errors: the thrown error
+ */
+ error?: Error | undefined;
+ /**
+ * For self-accept-error-handler-errored: the error thrown by the module
+ * before the error handler tried to handle it.
+ */
+ originalError?: Error | undefined;
+ }
+
+ interface AcceptOptions {
+ /**
+ * If true the update process continues even if some modules are not accepted (and would bubble to the entry point).
+ */
+ ignoreUnaccepted?: boolean | undefined;
+ /**
+ * Ignore changes made to declined modules.
+ */
+ ignoreDeclined?: boolean | undefined;
+ /**
+ * Ignore errors throw in accept handlers, error handlers and while reevaluating module.
+ */
+ ignoreErrored?: boolean | undefined;
+ /**
+ * Notifier for declined modules.
+ */
+ onDeclined?: ((info: HotNotifierInfo) => void) | undefined;
+ /**
+ * Notifier for unaccepted modules.
+ */
+ onUnaccepted?: ((info: HotNotifierInfo) => void) | undefined;
+ /**
+ * Notifier for accepted modules.
+ */
+ onAccepted?: ((info: HotNotifierInfo) => void) | undefined;
+ /**
+ * Notifier for disposed modules.
+ */
+ onDisposed?: ((info: HotNotifierInfo) => void) | undefined;
+ /**
+ * Notifier for errors.
+ */
+ onErrored?: ((info: HotNotifierInfo) => void) | undefined;
+ /**
+ * Indicates that apply() is automatically called by check function
+ */
+ autoApply?: boolean | undefined;
+ }
+ const __webpack_module__: {
+ id: string;
+ exports: unknown;
+ hot: {
+ accept: () => void;
+ dispose: (onDispose: (data: Record<string, unknown>) => void) => void;
+ invalidate: () => void;
+ data?: Record<string, unknown>;
+ addStatusHandler(callback: (status: string) => void): void;
+ status(): string;
+ apply(options?: AcceptOptions): Promise<ModuleId[]>;
+ check(autoApply?: boolean): Promise<null | ModuleId[]>;
+ };
+ };
+ type ModuleId = string | number;
+}
+
// This file is copied from the @vercel/next.js, with removed TS annotations
//
// https://github.com/vercel/next.js/blob/canary/packages/react-refresh-utils/loader.ts
@@ -50,21 +157,14 @@ function RefreshModuleRuntime() {
// AMP / No-JS mode does not inject these helpers:
'$RefreshHelpers$' in self
) {
- // @ts-expect-error
- // eslint-disable-next-line no-proto
- const currentExports = module.__proto__.exports;
- let prevExports = null;
-
- // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
- if (module.hot && module.hot.data && module.hot.data.prevExports) {
- prevExports = module.hot.data.prevExports;
- }
+ const currentExports = __webpack_module__.exports;
+ const prevExports = __webpack_module__.hot.data?.prevExports ?? null;
// This cannot happen in MainTemplate because the exports mismatch between
// templating and execution.
self.$RefreshHelpers$.registerExportsForReactRefresh(
currentExports,
- module.id
+ __webpack_module__.id
);
// A module can be accepted automatically based on its exports, e.g. when
@@ -72,12 +172,12 @@ function RefreshModuleRuntime() {
if (self.$RefreshHelpers$.isReactRefreshBoundary(currentExports)) {
// Save the previous exports on update so we can compare the boundary
// signatures.
- (module.hot as __WebpackModuleApi.Hot).dispose((data) => {
+ __webpack_module__.hot.dispose((data) => {
data.prevExports = currentExports;
});
// Unconditionally accept an update to this module, we'll check if it's
// still a Refresh Boundary later.
- (module.hot as __WebpackModuleApi.Hot).accept();
+ __webpack_module__.hot.accept();
// This field is set when the previous version of this module was a
// Refresh Boundary, letting us know we need to check for invalidation or
@@ -96,8 +196,7 @@ function RefreshModuleRuntime() {
currentExports
)
) {
- // @ts-expect-error
- (module.hot as __WebpackModuleApi.Hot).invalidate();
+ __webpack_module__.hot.invalidate();
} else {
self.$RefreshHelpers$.scheduleUpdate();
}
@@ -109,8 +208,7 @@ function RefreshModuleRuntime() {
// because we already accepted this update (accidental side effect).
const isNoLongerABoundary = prevExports !== null;
if (isNoLongerABoundary) {
- // @ts-expect-error
- (module.hot as __WebpackModuleApi).invalidate();
+ __webpack_module__.hot.invalidate();
}
}
}
diff --git a/packages/cli/package.json b/packages/cli/package.json
index a2bce36ec7..2909ae48eb 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -47,7 +47,6 @@
"@types/react": "18.0.26",
"@types/react-dom": "18.0.10",
"@types/semver": "^7.3.4",
- "@types/webpack-env": "^1.16.0",
"@typescript-eslint/eslint-plugin": "5.18.0",
"@typescript-eslint/parser": "5.18.0",
"eslint": "8.25.0",
diff --git a/packages/cli/src/preview-server/error-overlay/entry-basic.ts b/packages/cli/src/preview-server/error-overlay/entry-basic.ts
index 6042d05a16..9f9a749fd8 100644
--- a/packages/cli/src/preview-server/error-overlay/entry-basic.ts
+++ b/packages/cli/src/preview-server/error-overlay/entry-basic.ts
@@ -3,8 +3,8 @@ import {mountRemotionOverlay} from './remotion-overlay';
import {setErrorsRef} from './remotion-overlay/Overlay';
startReportingRuntimeErrors(() => {
- if (module.hot) {
- module.hot.addStatusHandler((status) => {
+ if (__webpack_module__.hot) {
+ __webpack_module__.hot.addStatusHandler((status) => {
if (status === 'apply') {
if (didUnmountReactApp()) {
return window.location.reload();
diff --git a/packages/cli/src/preview-server/hot-middleware/index.tsx b/packages/cli/src/preview-server/hot-middleware/index.tsx
index 4f7253945f..718e0a1daf 100644
--- a/packages/cli/src/preview-server/hot-middleware/index.tsx
+++ b/packages/cli/src/preview-server/hot-middleware/index.tsx
@@ -11,6 +11,113 @@ import {Log} from '../../log';
import type {HotMiddlewareMessage, ModuleMap, WebpackStats} from './types';
import {hotMiddlewareOptions} from './types';
+declare global {
+ const __webpack_hash__: unknown;
+
+ interface HotNotifierInfo {
+ type:
+ | 'self-declined'
+ | 'declined'
+ | 'unaccepted'
+ | 'accepted'
+ | 'disposed'
+ | 'accept-errored'
+ | 'self-accept-errored'
+ | 'self-accept-error-handler-errored';
+ /**
+ * The module in question.
+ */
+ moduleId: number;
+ /**
+ * For errors: the module id owning the accept handler.
+ */
+ dependencyId?: number | undefined;
+ /**
+ * For declined/accepted/unaccepted: the chain from where the update was propagated.
+ */
+ chain?: number[] | undefined;
+ /**
+ * For declined: the module id of the declining parent
+ */
+ parentId?: number | undefined;
+ /**
+ * For accepted: the modules that are outdated and will be disposed
+ */
+ outdatedModules?: number[] | undefined;
+ /**
+ * For accepted: The location of accept handlers that will handle the update
+ */
+ outdatedDependencies?:
+ | {
+ [dependencyId: number]: number[];
+ }
+ | undefined;
+ /**
+ * For errors: the thrown error
+ */
+ error?: Error | undefined;
+ /**
+ * For self-accept-error-handler-errored: the error thrown by the module
+ * before the error handler tried to handle it.
+ */
+ originalError?: Error | undefined;
+ }
+
+ interface AcceptOptions {
+ /**
+ * If true the update process continues even if some modules are not accepted (and would bubble to the entry point).
+ */
+ ignoreUnaccepted?: boolean | undefined;
+ /**
+ * Ignore changes made to declined modules.
+ */
+ ignoreDeclined?: boolean | undefined;
+ /**
+ * Ignore errors throw in accept handlers, error handlers and while reevaluating module.
+ */
+ ignoreErrored?: boolean | undefined;
+ /**
+ * Notifier for declined modules.
+ */
+ onDeclined?: ((info: HotNotifierInfo) => void) | undefined;
+ /**
+ * Notifier for unaccepted modules.
+ */
+ onUnaccepted?: ((info: HotNotifierInfo) => void) | undefined;
+ /**
+ * Notifier for accepted modules.
+ */
+ onAccepted?: ((info: HotNotifierInfo) => void) | undefined;
+ /**
+ * Notifier for disposed modules.
+ */
+ onDisposed?: ((info: HotNotifierInfo) => void) | undefined;
+ /**
+ * Notifier for errors.
+ */
+ onErrored?: ((info: HotNotifierInfo) => void) | undefined;
+ /**
+ * Indicates that apply() is automatically called by check function
+ */
+ autoApply?: boolean | undefined;
+ }
+ const __webpack_module__: {
+ id: string;
+ exports: unknown;
+ hot: {
+ accept: () => void;
+ dispose: (onDispose: (data: Record<string, unknown>) => void) => void;
+ invalidate: () => void;
+ data?: Record<string, unknown>;
+ addStatusHandler(callback: (status: string) => void): void;
+ status(): string;
+ apply(options?: AcceptOptions): Promise<ModuleId[]>;
+ check(autoApply?: boolean): Promise<null | ModuleId[]>;
+ };
+ };
+ type ModuleId = string | number;
+}
+
const pathMatch = function (url: string, path: string) {
try {
return parse(url).pathname === path;
diff --git a/packages/cli/src/preview-server/hot-middleware/process-update.ts b/packages/cli/src/preview-server/hot-middleware/process-update.ts
index 15a545be87..b9340cc7a6 100644
--- a/packages/cli/src/preview-server/hot-middleware/process-update.ts
+++ b/packages/cli/src/preview-server/hot-middleware/process-update.ts
@@ -13,9 +13,7 @@
import type {HotMiddlewareOptions, ModuleMap} from './types';
-/* global __webpack_hash__ */
-
-if (!module.hot) {
+if (!__webpack_module__.hot) {
throw new Error('[Fast refresh] Hot Module Replacement is disabled.');
}
@@ -23,7 +21,7 @@ const hmrDocsUrl = 'https://webpack.js.org/concepts/hot-module-replacement/'; //
let lastHash: string | undefined;
const failureStatuses = {abort: 1, fail: 1};
-const applyOptions: __WebpackModuleApi.AcceptOptions = {
+const applyOptions: AcceptOptions = {
ignoreUnaccepted: true,
ignoreDeclined: true,
ignoreErrored: true,
@@ -61,15 +59,12 @@ export const processUpdate = function (
options: HotMiddlewareOptions
) {
const {reload} = options;
- if (!upToDate(hash) && module.hot?.status() === 'idle') {
+ if (!upToDate(hash) && __webpack_module__.hot?.status() === 'idle') {
check();
}
async function check() {
- const cb = function (
- err: Error | null,
- updatedModules: __WebpackModuleApi.ModuleId[]
- ) {
+ const cb = function (err: Error | null, updatedModules: ModuleId[] | null) {
if (err) return handleError(err);
if (!updatedModules) {
@@ -88,7 +83,7 @@ export const processUpdate = function (
const applyCallback = function (
applyErr: Error | null,
- renewedModules: __WebpackModuleApi.ModuleId[]
+ renewedModules: ModuleId[]
) {
if (applyErr) return handleError(applyErr);
@@ -99,10 +94,10 @@ export const processUpdate = function (
logUpdates(updatedModules, renewedModules);
};
- const applyResult = module.hot?.apply(applyOptions, applyCallback);
+ const applyResult = __webpack_module__.hot?.apply(applyOptions);
if ((applyResult as unknown as Promise<unknown>)?.then) {
// HotModuleReplacement.runtime.js refers to the result as `outdatedModules`
- (applyResult as unknown as Promise<__WebpackModuleApi.ModuleId[]>)
+ (applyResult as unknown as Promise<ModuleId[]>)
.then((outdatedModules) => {
applyCallback(null, outdatedModules);
})
@@ -111,19 +106,14 @@ export const processUpdate = function (
};
try {
- const result = await (module.hot?.check(false, cb) as unknown as Promise<
- __WebpackModuleApi.ModuleId[]
- >);
+ const result = await __webpack_module__.hot?.check(false);
cb(null, result);
} catch (err) {
cb(err as Error, []);
}
}
- function logUpdates(
- updatedModules: __WebpackModuleApi.ModuleId[],
- renewedModules: __WebpackModuleApi.ModuleId[]
- ) {
+ function logUpdates(updatedModules: ModuleId[], renewedModules: ModuleId[]) {
const unacceptedModules =
updatedModules?.filter((moduleId) => {
return renewedModules && renewedModules.indexOf(moduleId) < 0;
@@ -163,7 +153,7 @@ export const processUpdate = function (
}
function handleError(err: Error) {
- if ((module.hot?.status() ?? 'nope') in failureStatuses) {
+ if ((__webpack_module__.hot?.status() ?? 'nope') in failureStatuses) {
if (options.warn) {
console.warn(
'[Fast refresh] Cannot check for update (Full reload needed)'
diff --git a/packages/core/package.json b/packages/core/package.json
index 68c8635320..091af7d8e1 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -31,7 +31,6 @@
"@types/node": "^16.7.5",
"@types/react": "18.0.26",
"@types/react-dom": "18.0.10",
- "@types/webpack-env": "^1.16.0",
"@vitejs/plugin-react": "^2.0.0",
"eslint": "8.25.0",
"eslint-plugin-require-extensions": "^0.1.2",
diff --git a/packages/core/src/RemotionRoot.tsx b/packages/core/src/RemotionRoot.tsx
index 48a1a0fa62..090ce5ece7 100644
--- a/packages/core/src/RemotionRoot.tsx
+++ b/packages/core/src/RemotionRoot.tsx
@@ -18,9 +18,18 @@ import type {
SetTimelineContextValue,
TimelineContextValue,
} from './timeline-position-state.js';
-import {SetTimelineContext, TimelineContext} from './timeline-position-state.js';
+import {
+ SetTimelineContext,
+ TimelineContext,
+} from './timeline-position-state.js';
import {DurationsContextProvider} from './video/duration-state.js';
+declare const __webpack_module__: {
+ hot: {
+ addStatusHandler(callback: (status: string) => void): void;
+ };
+};
+
export const RemotionRoot: React.FC<{
children: React.ReactNode;
numberOfAudioTags: number;
@@ -73,12 +82,15 @@ export const RemotionRoot: React.FC<{
}, [fastRefreshes]);
useEffect(() => {
- if (module.hot) {
- module.hot.addStatusHandler((status) => {
- if (status === 'idle') {
- setFastRefreshes((i) => i + 1);
- }
- });
+ // TODO: This can be moved to renderer?
+ if (typeof __webpack_module__ !== 'undefined') {
+ if (__webpack_module__.hot) {
+ __webpack_module__.hot.addStatusHandler((status) => {
+ if (status === 'idle') {
+ setFastRefreshes((i) => i + 1);
+ }
+ });
+ }
}
}, []);
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 84833c0796..c2eb6dd85a 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -80,7 +80,6 @@ importers:
'@types/node': ^16.7.5
'@types/react': 18.0.26
'@types/react-dom': 18.0.10
- '@types/webpack-env': ^1.16.0
'@typescript-eslint/eslint-plugin': 5.18.0
'@typescript-eslint/parser': 5.18.0
css-loader: 5.2.7
@@ -111,7 +110,6 @@ importers:
'@types/node': 16.10.2
'@types/react': 18.0.26
'@types/react-dom': 18.0.10
- '@types/webpack-env': 1.16.2
'@typescript-eslint/eslint-plugin': 5.18.0_sjor5xiixfpkviiagy75ykktdy
'@typescript-eslint/parser': 5.18.0_t54va6jeo5lf3vtqih5uiyrm5e
eslint: 8.25.0
@@ -138,7 +136,6 @@ importers:
'@types/react': 18.0.26
'@types/react-dom': 18.0.10
'@types/semver': ^7.3.4
- '@types/webpack-env': ^1.16.0
'@typescript-eslint/eslint-plugin': 5.18.0
'@typescript-eslint/parser': 5.18.0
better-opn: 2.1.1
@@ -180,7 +177,6 @@ importers:
'@types/react': 18.0.26
'@types/react-dom': 18.0.10
'@types/semver': 7.3.8
- '@types/webpack-env': 1.16.2
'@typescript-eslint/eslint-plugin': 5.18.0_sjor5xiixfpkviiagy75ykktdy
'@typescript-eslint/parser': 5.18.0_t54va6jeo5lf3vtqih5uiyrm5e
eslint: 8.25.0
@@ -223,7 +219,6 @@ importers:
'@types/node': ^16.7.5
'@types/react': 18.0.26
'@types/react-dom': 18.0.10
- '@types/webpack-env': ^1.16.0
'@vitejs/plugin-react': ^2.0.0
eslint: 8.25.0
eslint-plugin-require-extensions: ^0.1.2
@@ -243,7 +238,6 @@ importers:
'@types/node': 16.10.2
'@types/react': 18.0.26
'@types/react-dom': 18.0.10
- '@types/webpack-env': 1.16.2
'@vitejs/plugin-react': [email protected]
eslint: 8.25.0
eslint-plugin-require-extensions: [email protected]
@@ -9817,10 +9811,6 @@ packages:
/@types/web/0.0.78:
resolution: {integrity: sha512-1Apkpr0zolMDECgtokp9ztApWkdlg4Ey4UJ3sKHAGjCHoiUq1/kBd0lR+JxeHlQVqAC6tike7tUJ9uLbVNwsxQ==}
- /@types/webpack-env/1.16.2:
- resolution: {integrity: sha512-vKx7WNQNZDyJveYcHAm9ZxhqSGLYwoyLhrHjLBOkw3a7cT76sTdjgtwyijhk1MaHyRIuSztcVwrUOO/NEu68Dw==}
- dev: true
-
/@types/ws/8.5.3:
resolution: {integrity: sha512-6YOoWjruKj1uLf3INHH7D3qTXwFfEsg1kf3c0uDdSBJwfa/llkwIjrAGV7j7mVgGNbzTQ3HiHKKDXl6bJPD97w==}
dependencies: