Skip to content

🎥 Make videos programmatically with React

get it right

Jonny Burger1b783dea16e9
diff --git a/packages/bundler/src/hot-middleware/client.ts b/packages/bundler/src/hot-middleware/client.ts
index 77e206074d..faaa85adae 100644
--- a/packages/bundler/src/hot-middleware/client.ts
+++ b/packages/bundler/src/hot-middleware/client.ts
@@ -17,10 +17,6 @@ if (typeof window === 'undefined') {
connect();
}
-function setOptionsAndConnect() {
- connect();
-}
-
function eventSourceWrapper() {
let source: EventSource;
let lastActivity = Date.now();
@@ -180,30 +176,14 @@ function createReporter() {
};
}
-let customHandler: ((msg: HotMiddlewareMessage) => void) | undefined;
-let subscribeAllHandler: ((msg: HotMiddlewareMessage) => void) | undefined;
-
function processMessage(obj: HotMiddlewareMessage) {
switch (obj.action) {
case 'building':
- console.log(
- '[Fast refresh] bundle ' +
- (obj.name ? "'" + obj.name + "' " : '') +
- 'rebuilding'
- );
+ window.remotion_isBuilding?.();
break;
- case 'built':
- console.log(
- '[Fast refresh] bundle ' +
- (obj.name ? "'" + obj.name + "' " : '') +
- 'rebuilt in ' +
- obj.time +
- 'ms'
- );
-
- // fall through
- case 'sync': {
+ case 'sync':
+ case 'built': {
let applyUpdate = true;
if (obj.errors.length > 0) {
if (reporter) reporter.problems('errors', obj);
@@ -219,6 +199,7 @@ function processMessage(obj: HotMiddlewareMessage) {
}
if (applyUpdate) {
+ window.remotion_finishedBuilding?.();
processUpdate(obj.hash, obj.modules, hotMiddlewareOptions);
}
@@ -226,22 +207,6 @@ function processMessage(obj: HotMiddlewareMessage) {
}
default:
- if (customHandler) {
- customHandler(obj);
- }
- }
-
- if (subscribeAllHandler) {
- subscribeAllHandler(obj);
+ break;
}
}
-
-module.exports = {
- subscribeAll(handler: (msg: HotMiddlewareMessage) => void) {
- subscribeAllHandler = handler;
- },
- subscribe(handler: (msg: HotMiddlewareMessage) => void) {
- customHandler = handler;
- },
- setOptionsAndConnect,
-};
diff --git a/packages/bundler/src/hot-middleware/index.tsx b/packages/bundler/src/hot-middleware/index.tsx
index fedc2a4c66..ef5d8caf20 100644
--- a/packages/bundler/src/hot-middleware/index.tsx
+++ b/packages/bundler/src/hot-middleware/index.tsx
@@ -23,17 +23,15 @@ const pathMatch = function (url: string, path: string) {
};
export const webpackHotMiddleware = (compiler: webpack.Compiler) => {
- let eventStream: EventStream | null = createEventStream(
+ const eventStream: EventStream | null = createEventStream(
hotMiddlewareOptions.heartbeat
);
let latestStats: webpack.Stats | null = null;
- let closed = false;
compiler.hooks.invalid.tap('remotion', onInvalid);
compiler.hooks.done.tap('remotion', onDone);
function onInvalid() {
- if (closed) return;
latestStats = null;
console.log('webpack building...');
eventStream?.publish({
@@ -42,9 +40,9 @@ export const webpackHotMiddleware = (compiler: webpack.Compiler) => {
}
function onDone(statsResult: webpack.Stats) {
- if (closed) return;
// Keep hold of latest stats so they can be propagated to new clients
latestStats = statsResult;
+
publishStats('built', latestStats, eventStream);
}
@@ -53,8 +51,6 @@ export const webpackHotMiddleware = (compiler: webpack.Compiler) => {
res: ServerResponse,
next: () => void
) {
- if (closed) return next();
-
if (!pathMatch(req.url as string, hotMiddlewareOptions.path)) return next();
eventStream?.handler(req, res);
if (latestStats) {
@@ -62,20 +58,6 @@ export const webpackHotMiddleware = (compiler: webpack.Compiler) => {
}
};
- middleware.publish = function (payload: HotMiddlewareMessage) {
- if (closed) return;
- eventStream?.publish(payload);
- };
-
- middleware.close = function () {
- if (closed) return;
- // Can't remove compiler plugins, so we just set a flag and noop if closed
- // https://github.com/webpack/tapable/issues/32#issuecomment-350644466
- closed = true;
- eventStream?.close();
- eventStream = null;
- };
-
return middleware;
};
@@ -109,9 +91,6 @@ function createEventStream(heartbeat: number) {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/event-stream;charset=utf-8',
'Cache-Control': 'no-cache, no-transform',
- // While behind nginx, event stream should not be buffered:
- // http://nginx.org/docs/http/ngx_http_proxy_module.html#proxy_buffering
- 'X-Accel-Buffering': 'no',
};
const isHttp1 = !(parseInt(req.httpVersion, 10) >= 2);
diff --git a/packages/bundler/src/hot-middleware/process-update.ts b/packages/bundler/src/hot-middleware/process-update.ts
index 53abbf0a78..e71e5017a6 100644
--- a/packages/bundler/src/hot-middleware/process-update.ts
+++ b/packages/bundler/src/hot-middleware/process-update.ts
@@ -61,7 +61,6 @@ export const processUpdate = function (
) {
const {reload} = options;
if (!upToDate(hash) && module.hot?.status() === 'idle') {
- console.log('[Fast refresh] Checking for updates on the server...');
check();
}
@@ -92,7 +91,9 @@ export const processUpdate = function (
) {
if (applyErr) return handleError(applyErr);
- if (!upToDate()) check();
+ if (!upToDate()) {
+ check();
+ }
logUpdates(updatedModules, renewedModules);
};
@@ -152,15 +153,12 @@ export const processUpdate = function (
if (!renewedModules || renewedModules.length === 0) {
console.log('[Fast refresh] Nothing hot updated.');
} else {
- console.log('[Fast refresh] Updated modules:');
renewedModules.forEach((moduleId) => {
- console.log('[Fast refresh] - ' + (moduleMap[moduleId] || moduleId));
+ console.log(
+ `[Fast refresh] ${moduleMap[moduleId] || moduleId} fast refreshed.`
+ );
});
}
-
- if (upToDate()) {
- console.log('[Fast refresh] App is up to date.');
- }
}
function handleError(err: Error) {
diff --git a/packages/cli/src/editor/components/MenuBuildIndicator.tsx b/packages/cli/src/editor/components/MenuBuildIndicator.tsx
new file mode 100644
index 0000000000..16deb2c017
--- /dev/null
+++ b/packages/cli/src/editor/components/MenuBuildIndicator.tsx
@@ -0,0 +1,31 @@
+import React, {useEffect, useState} from 'react';
+
+const cwd: React.CSSProperties = {
+ fontSize: 13,
+ opacity: 0.8,
+};
+
+export const MenuBuildIndicator: React.FC = () => {
+ const [isBuilding, setIsBuilding] = useState(false);
+
+ useEffect(() => {
+ window.remotion_isBuilding = () => {
+ setIsBuilding(true);
+ };
+
+ window.remotion_finishedBuilding = () => {
+ setIsBuilding(false);
+ };
+
+ return () => {
+ window.remotion_isBuilding = undefined;
+ window.remotion_finishedBuilding = undefined;
+ };
+ }, []);
+
+ return (
+ <div style={cwd} title={window.remotion_cwd}>
+ {isBuilding ? 'Building...' : window.remotion_projectName}
+ </div>
+ );
+};
diff --git a/packages/cli/src/editor/components/MenuToolbar.tsx b/packages/cli/src/editor/components/MenuToolbar.tsx
index fbe24f42ff..38e3fecf58 100644
--- a/packages/cli/src/editor/components/MenuToolbar.tsx
+++ b/packages/cli/src/editor/components/MenuToolbar.tsx
@@ -8,6 +8,7 @@ import {SidebarCollapsedState, SidebarContext} from '../state/sidebar';
import {timelineRef} from '../state/timeline-ref';
import {Row} from './layout';
import {Menu, MenuId, MenuItem} from './Menu/MenuItem';
+import {MenuBuildIndicator} from './MenuBuildIndicator';
import {commonPreviewSizes, getPreviewSizeLabel} from './SizeSelector';
import {inOutHandles} from './TimelineInOutToggle';
import {UpdateCheck} from './UpdateCheck';
@@ -28,11 +29,6 @@ const flex: React.CSSProperties = {
flex: 1,
};
-const cwd: React.CSSProperties = {
- fontSize: 13,
- opacity: 0.8,
-};
-
type Structure = Menu[];
const openExternal = (link: string) => {
@@ -555,9 +551,7 @@ export const MenuToolbar: React.FC = () => {
})}
<UpdateCheck />
<div style={flex} />
- <div style={cwd} title={window.remotion_cwd}>
- {window.remotion_projectName}
- </div>
+ <MenuBuildIndicator />
</Row>
);
};
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 0a29f71348..0c8a0211e1 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -20,6 +20,8 @@ declare global {
remotion_collectAssets: () => TAsset[];
remotion_isPlayer: boolean;
remotion_imported: boolean;
+ remotion_isBuilding: undefined | (() => void);
+ remotion_finishedBuilding: undefined | (() => void);
siteVersion: '3';
}
}
diff --git a/packages/example/src/VideoTesting/index.tsx b/packages/example/src/VideoTesting/index.tsx
index ff978dacbb..c6cb4f0ad2 100644
--- a/packages/example/src/VideoTesting/index.tsx
+++ b/packages/example/src/VideoTesting/index.tsx
@@ -18,7 +18,7 @@ export const VideoTesting: React.FC<{
return (
<div>
- <Sequence from={0} durationInFrames={durationInFrames}>
+ <Sequence from={20} durationInFrames={durationInFrames}>
<Comp src={codec === 'mp4' ? videoMp4 : videoWebm} />
</Sequence>
</div>