Skip to content

🎥 Make videos programmatically with React

restructure bundler package

Jonny Burgerfffaabcf965e
diff --git a/packages/bundler/src/index.ts b/packages/bundler/src/index.ts
index 08dcf7a05a..fe944e07ac 100644
--- a/packages/bundler/src/index.ts
+++ b/packages/bundler/src/index.ts
@@ -1,75 +1,12 @@
-// @ts-expect-error
-import webpackDevMiddleware from '@jonny/webpack-dev-middleware';
-import express from 'express';
-import fs from 'fs';
-import getPort from 'get-port';
-import os from 'os';
-import path from 'path';
-import {Internals, WebpackOverrideFn} from 'remotion';
-import webpack from 'webpack';
-import webpackHotMiddleware from 'webpack-hot-middleware';
import {overrideWebpackConfig} from './override-webpack';
-import {isUpdateAvailableWithTimeout} from './update-available';
-import {webpackConfig} from './webpack-config';
+import {startServer} from './start-server';
+import {cacheExists, clearCache} from './webpack-cache';
-export const startServer = async (
- entry: string,
- userDefinedComponent: string,
- options?: {
- webpackOverride?: WebpackOverrideFn;
- inputProps?: object;
- }
-): Promise<number> => {
- const app = express();
- const tmpDir = await fs.promises.mkdtemp(
- path.join(os.tmpdir(), 'react-motion-graphics')
- );
-
- const config = webpackConfig({
- entry,
- userDefinedComponent,
- outDir: tmpDir,
- environment: 'development',
- webpackOverride:
- options?.webpackOverride ?? Internals.getWebpackOverrideFn(),
- inputProps: options?.inputProps ?? {},
- });
- const compiler = webpack(config);
-
- app.use(express.static(path.join(__dirname, '..', 'web')));
- app.use(webpackDevMiddleware(compiler));
- app.use(
- webpackHotMiddleware(compiler, {
- path: '/__webpack_hmr',
- heartbeat: 10 * 1000,
- })
- );
-
- app.get('/update', (req, res) => {
- isUpdateAvailableWithTimeout()
- .then((data) => {
- res.json(data);
- })
- .catch((err) => {
- res.status(500).json({
- err: err.message,
- });
- });
- });
-
- app.use('favicon.png', (req, res) => {
- res.sendFile(path.join(__dirname, '..', 'web', 'favicon.png'));
- });
-
- app.use('*', (req, res) => {
- res.sendFile(path.join(__dirname, '..', 'web', 'index.html'));
- });
-
- const port = await getPort({port: getPort.makeRange(3000, 3100)});
- app.listen(port);
- return port;
+export const BundlerInternals = {
+ startServer,
+ cacheExists,
+ clearCache,
};
-export * from './bundler';
-export {cacheExists, clearCache} from './webpack-cache';
+export {bundle} from './bundler';
export {overrideWebpackConfig};
diff --git a/packages/bundler/src/start-server.ts b/packages/bundler/src/start-server.ts
new file mode 100644
index 0000000000..1526827cf7
--- /dev/null
+++ b/packages/bundler/src/start-server.ts
@@ -0,0 +1,70 @@
+// @ts-expect-error
+import webpackDevMiddleware from '@jonny/webpack-dev-middleware';
+import express from 'express';
+import fs from 'fs';
+import getPort from 'get-port';
+import os from 'os';
+import path from 'path';
+import {Internals, WebpackOverrideFn} from 'remotion';
+import webpack from 'webpack';
+import webpackHotMiddleware from 'webpack-hot-middleware';
+import {isUpdateAvailableWithTimeout} from './update-available';
+import {webpackConfig} from './webpack-config';
+
+export const startServer = async (
+ entry: string,
+ userDefinedComponent: string,
+ options?: {
+ webpackOverride?: WebpackOverrideFn;
+ inputProps?: object;
+ }
+): Promise<number> => {
+ const app = express();
+ const tmpDir = await fs.promises.mkdtemp(
+ path.join(os.tmpdir(), 'react-motion-graphics')
+ );
+
+ const config = webpackConfig({
+ entry,
+ userDefinedComponent,
+ outDir: tmpDir,
+ environment: 'development',
+ webpackOverride:
+ options?.webpackOverride ?? Internals.getWebpackOverrideFn(),
+ inputProps: options?.inputProps ?? {},
+ });
+ const compiler = webpack(config);
+
+ app.use(express.static(path.join(__dirname, '..', 'web')));
+ app.use(webpackDevMiddleware(compiler));
+ app.use(
+ webpackHotMiddleware(compiler, {
+ path: '/__webpack_hmr',
+ heartbeat: 10 * 1000,
+ })
+ );
+
+ app.get('/update', (req, res) => {
+ isUpdateAvailableWithTimeout()
+ .then((data) => {
+ res.json(data);
+ })
+ .catch((err) => {
+ res.status(500).json({
+ err: err.message,
+ });
+ });
+ });
+
+ app.use('favicon.png', (req, res) => {
+ res.sendFile(path.join(__dirname, '..', 'web', 'favicon.png'));
+ });
+
+ app.use('*', (req, res) => {
+ res.sendFile(path.join(__dirname, '..', 'web', 'index.html'));
+ });
+
+ const port = await getPort({port: getPort.makeRange(3000, 3100)});
+ app.listen(port);
+ return port;
+};
diff --git a/packages/cli/src/preview.ts b/packages/cli/src/preview.ts
index 56b375ae22..6cec03133a 100644
--- a/packages/cli/src/preview.ts
+++ b/packages/cli/src/preview.ts
@@ -1,6 +1,6 @@
-import {startServer} from '@remotion/bundler';
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
// @ts-ignore
+import {BundlerInternals} from '@remotion/bundler';
import betterOpn from 'better-opn';
import path from 'path';
import xns from 'xns';
@@ -17,7 +17,7 @@ export const previewCommand = xns(async () => {
const inputProps = getInputProps();
- const port = await startServer(
+ const port = await BundlerInternals.startServer(
path.resolve(__dirname, 'previewEntry.js'),
fullPath,
{
diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsx
index 85d5fa4672..f69debae63 100644
--- a/packages/cli/src/render.tsx
+++ b/packages/cli/src/render.tsx
@@ -1,4 +1,4 @@
-import {bundle, cacheExists, clearCache} from '@remotion/bundler';
+import {bundle, BundlerInternals} from '@remotion/bundler';
import {
getCompositions,
renderFrames,
@@ -148,10 +148,10 @@ export const render = async () => {
);
const shouldCache = Internals.getWebpackCaching();
- const cacheExistedBefore = cacheExists('production', null);
+ const cacheExistedBefore = BundlerInternals.cacheExists('production', null);
if (cacheExistedBefore && !shouldCache) {
process.stdout.write('🧹 Cache disabled but found. Deleting... ');
- await clearCache('production', null);
+ await BundlerInternals.clearCache('production', null);
process.stdout.write('done. \n');
}
bundlingProgress.start(100, 0);
@@ -166,7 +166,7 @@ export const render = async () => {
);
bundlingProgress.stop();
Log.Verbose('Bundled under', bundled);
- const cacheExistedAfter = cacheExists('production', null);
+ const cacheExistedAfter = BundlerInternals.cacheExists('production', null);
if (cacheExistedAfter && !cacheExistedBefore) {
Log.Info('⚡️ Cached bundle. Subsequent builds will be faster.');
}