Skip to content

🎥 Make videos programmatically with React

fix duplicate remotionRoot logic

Jonny Burgerf65c488fbeb7
diff --git a/packages/bundler/src/bundle.ts b/packages/bundler/src/bundle.ts
index f02269b91d..c1e6714291 100644
--- a/packages/bundler/src/bundle.ts
+++ b/packages/bundler/src/bundle.ts
@@ -101,7 +101,7 @@ export const bundle = async (
}
const baseDir = options?.publicPath ?? '/';
- const publicDir =
+ const staticHash =
'/' +
[trimTrailingSlash(trimLeadingSlash(baseDir)), 'public']
.filter(Boolean)
@@ -114,7 +114,13 @@ export const bundle = async (
await copyDir(from, to);
}
- const html = indexHtml(publicDir, baseDir, null, null);
+ const html = indexHtml({
+ staticHash,
+ baseDir,
+ editorName: null,
+ inputProps: null,
+ remotionRoot: resolvedRemotionRoot,
+ });
fs.writeFileSync(path.join(outDir, 'index.html'), html);
return outDir;
diff --git a/packages/bundler/src/esbuild-loader/index.ts b/packages/bundler/src/esbuild-loader/index.ts
index 8a0e7e838b..62c9aa004c 100644
--- a/packages/bundler/src/esbuild-loader/index.ts
+++ b/packages/bundler/src/esbuild-loader/index.ts
@@ -3,8 +3,6 @@ import path from 'path';
import type webpack from 'webpack';
import type {LoaderOptions} from './interfaces';
-const tsConfigPath = path.join(process.cwd(), 'tsconfig.json');
-
const isTsExtensionPtrn = /\.ts$/i;
const isTypescriptInstalled = () => {
@@ -25,6 +23,8 @@ async function ESBuildLoader(
const options: LoaderOptions = this.getOptions();
const {implementation, ...esbuildTransformOptions} = options;
+ const tsConfigPath = path.join(this.context, 'tsconfig.json');
+
if (implementation && typeof implementation.transform !== 'function') {
done(
new TypeError(
diff --git a/packages/bundler/src/index-html.ts b/packages/bundler/src/index-html.ts
index fb863bcaa1..fbcf157d60 100644
--- a/packages/bundler/src/index-html.ts
+++ b/packages/bundler/src/index-html.ts
@@ -1,11 +1,18 @@
import path from 'path';
-export const indexHtml = (
- staticHash: string,
- baseDir: string,
- editorName: string | null,
- inputProps: object | null
-) =>
+export const indexHtml = ({
+ baseDir,
+ editorName,
+ inputProps,
+ staticHash,
+ remotionRoot,
+}: {
+ staticHash: string;
+ baseDir: string;
+ editorName: string | null;
+ inputProps: object | null;
+ remotionRoot: string;
+}) =>
`
<!DOCTYPE html>
<html lang="en">
@@ -26,9 +33,9 @@ export const indexHtml = (
: '<script>window.remotion_editorName = null;</script>'
}
<script>window.remotion_projectName = ${JSON.stringify(
- path.basename(process.cwd())
+ path.basename(remotionRoot)
)};</script>
- <script>window.remotion_cwd = ${JSON.stringify(process.cwd())};</script>
+ <script>window.remotion_cwd = ${JSON.stringify(remotionRoot)};</script>
${
inputProps
? ` <script>window.remotion_inputProps = ${JSON.stringify(
diff --git a/packages/bundler/src/webpack-cache.ts b/packages/bundler/src/webpack-cache.ts
index c469053afc..5816337f51 100644
--- a/packages/bundler/src/webpack-cache.ts
+++ b/packages/bundler/src/webpack-cache.ts
@@ -16,9 +16,8 @@ declare global {
// Inlined from https://github.com/webpack/webpack/blob/4c2ee7a4ddb8db2362ca83b6c4190523387ba7ee/lib/config/defaults.js#L265
// An algorithm to determine where Webpack will cache the depencies
-const getWebpackCacheDir = () => {
- const cwd = process.cwd();
- let dir: string | undefined = cwd;
+const getWebpackCacheDir = (remotionRoot: string) => {
+ let dir: string | undefined = remotionRoot;
for (;;) {
try {
if (fs.statSync(path.join(dir, 'package.json')).isFile()) {
@@ -37,7 +36,7 @@ const getWebpackCacheDir = () => {
}
if (!dir) {
- return path.resolve(cwd, '.cache/webpack');
+ return path.resolve(remotionRoot, '.cache/webpack');
}
if (process.versions.pnp === '1') {
@@ -51,17 +50,24 @@ const getWebpackCacheDir = () => {
return path.resolve(dir, 'node_modules/.cache/webpack');
};
-const remotionCacheLocation = (environment: Environment, hash: string) => {
+const remotionCacheLocation = (
+ remotionRoot: string,
+ environment: Environment,
+ hash: string
+) => {
return path.join(
- getWebpackCacheDir(),
+ getWebpackCacheDir(remotionRoot),
getWebpackCacheName(environment, hash)
);
};
-export const clearCache = () => {
- return (fs.promises.rm ?? fs.promises.rmdir)(getWebpackCacheDir(), {
- recursive: true,
- });
+export const clearCache = (remotionRoot: string) => {
+ return (fs.promises.rm ?? fs.promises.rmdir)(
+ getWebpackCacheDir(remotionRoot),
+ {
+ recursive: true,
+ }
+ );
};
const getPrefix = (environment: Environment) => {
@@ -78,8 +84,14 @@ export const getWebpackCacheName = (environment: Environment, hash: string) => {
return `${getPrefix(environment)}-${hash}`;
};
-const hasOtherCache = (environment: Environment) => {
- const cacheDir = fs.readdirSync(getWebpackCacheDir());
+const hasOtherCache = ({
+ remotionRoot,
+ environment,
+}: {
+ remotionRoot: string;
+ environment: Environment;
+}) => {
+ const cacheDir = fs.readdirSync(getWebpackCacheDir(remotionRoot));
if (
cacheDir.find((c) => {
return c.startsWith(getPrefix(environment));
@@ -92,18 +104,19 @@ const hasOtherCache = (environment: Environment) => {
};
export const cacheExists = (
+ remotionRoot: string,
environment: Environment,
hash: string
): CacheState => {
- if (fs.existsSync(remotionCacheLocation(environment, hash))) {
+ if (fs.existsSync(remotionCacheLocation(remotionRoot, environment, hash))) {
return 'exists';
}
- if (!fs.existsSync(getWebpackCacheDir())) {
+ if (!fs.existsSync(getWebpackCacheDir(remotionRoot))) {
return 'does-not-exist';
}
- if (hasOtherCache(environment)) {
+ if (hasOtherCache({remotionRoot, environment})) {
return 'other-exists';
}
diff --git a/packages/cli/src/preview-server/routes.ts b/packages/cli/src/preview-server/routes.ts
index 93acc214fc..372598590f 100644
--- a/packages/cli/src/preview-server/routes.ts
+++ b/packages/cli/src/preview-server/routes.ts
@@ -35,19 +35,30 @@ const static404 = (response: ServerResponse) => {
);
};
-const handleFallback = async (
- hash: string,
- _: IncomingMessage,
- response: ServerResponse,
- getCurrentInputProps: () => object
-) => {
+const handleFallback = async ({
+ remotionRoot,
+ hash,
+ response,
+ getCurrentInputProps,
+}: {
+ remotionRoot: string;
+ hash: string;
+ response: ServerResponse;
+ getCurrentInputProps: () => object;
+}) => {
const [edit] = await editorGuess;
const displayName = getDisplayNameForEditor(edit ? edit.command : null);
response.setHeader('content-type', 'text/html');
response.writeHead(200);
response.end(
- BundlerInternals.indexHtml(hash, '/', displayName, getCurrentInputProps())
+ BundlerInternals.indexHtml({
+ staticHash: hash,
+ baseDir: '/',
+ editorName: displayName,
+ inputProps: getCurrentInputProps(),
+ remotionRoot,
+ })
);
};
@@ -198,5 +209,10 @@ export const handleRoutes = ({
return static404(response);
}
- return handleFallback(hash, request, response, getCurrentInputProps);
+ return handleFallback({
+ remotionRoot,
+ hash,
+ response,
+ getCurrentInputProps,
+ });
};
diff --git a/packages/cli/src/setup-cache.ts b/packages/cli/src/setup-cache.ts
index beca30b049..7f0affc066 100644
--- a/packages/cli/src/setup-cache.ts
+++ b/packages/cli/src/setup-cache.ts
@@ -44,15 +44,19 @@ export const bundleOnCli = async ({
resolvedRemotionRoot: remotionRoot,
});
- const cacheExistedBefore = BundlerInternals.cacheExists('production', hash);
+ const cacheExistedBefore = BundlerInternals.cacheExists(
+ remotionRoot,
+ 'production',
+ hash
+ );
if (cacheExistedBefore !== 'does-not-exist' && !shouldCache) {
Log.info('🧹 Cache disabled but found. Deleting... ');
- await BundlerInternals.clearCache();
+ await BundlerInternals.clearCache(remotionRoot);
}
if (cacheExistedBefore === 'other-exists' && shouldCache) {
Log.info('🧹 Webpack config change detected. Clearing cache... ');
- await BundlerInternals.clearCache();
+ await BundlerInternals.clearCache(remotionRoot);
}
const bundleStartTime = Date.now();
@@ -80,7 +84,7 @@ export const bundleOnCli = async ({
);
Log.verbose('Bundled under', bundled);
const cacheExistedAfter =
- BundlerInternals.cacheExists('production', hash) === 'exists';
+ BundlerInternals.cacheExists(remotionRoot, 'production', hash) === 'exists';
if (cacheExistedAfter) {
if (
diff --git a/packages/cli/src/webpack-cache.ts b/packages/cli/src/webpack-cache.ts
deleted file mode 100644
index 3d85bb40db..0000000000
--- a/packages/cli/src/webpack-cache.ts
+++ /dev/null
@@ -1,101 +0,0 @@
-import fs from 'fs';
-import path from 'path';
-// eslint-disable-next-line no-restricted-imports
-import {random} from 'remotion';
-
-type Environment = 'development' | 'production';
-
-declare global {
- // eslint-disable-next-line @typescript-eslint/no-namespace
- namespace NodeJS {
- interface ProcessVersions {
- pnp?: string;
- }
- }
-}
-
-// Inlined from https://github.com/webpack/webpack/blob/4c2ee7a4ddb8db2362ca83b6c4190523387ba7ee/lib/config/defaults.js#L265
-// An algorithm to determine where Webpack will cache the depencies
-const getWebpackCacheDir = (remotionRoot: string) => {
- let dir: string | undefined = remotionRoot;
- for (;;) {
- try {
- if (fs.statSync(path.join(dir, 'package.json')).isFile()) {
- break;
- }
- // eslint-disable-next-line no-empty
- } catch (e) {}
-
- const parent = path.dirname(dir);
- if (dir === parent) {
- dir = undefined;
- break;
- }
-
- dir = parent;
- }
-
- if (!dir) {
- return path.resolve(remotionRoot, '.cache/webpack');
- }
-
- if (process.versions.pnp === '1') {
- return path.resolve(dir, '.pnp/.cache/webpack');
- }
-
- if (process.versions.pnp === '3') {
- return path.resolve(dir, '.yarn/.cache/webpack');
- }
-
- return path.resolve(dir, 'node_modules/.cache/webpack');
-};
-
-const remotionCacheLocation = (
- remotionRoot: string,
- environment: Environment,
- inputProps: object | null
-) => {
- return path.join(
- getWebpackCacheDir(remotionRoot),
- getWebpackCacheName(environment, inputProps)
- );
-};
-
-export const clearCache = (
- remotionRoot: string,
- environment: Environment,
- inputProps: object | null
-) => {
- return (fs.promises.rm ?? fs.promises.rmdir)(
- remotionCacheLocation(remotionRoot, environment, inputProps),
- {
- recursive: true,
- }
- );
-};
-
-export const getWebpackCacheName = (
- environment: Environment,
- inputProps: object | null
-) => {
- // In development, let's reset the cache when input props
- // are changing, because they are injected using Webpack and if changed,
- // it will get the cached version
- if (environment === 'development') {
- return `remotion-v3-${environment}-${random(JSON.stringify(inputProps))}`;
- }
-
- // 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}`;
-};
-
-export const cacheExists = (
- remotionRoot: string,
- environment: Environment,
- inputProps: object | null
-) => {
- return fs.existsSync(
- remotionCacheLocation(remotionRoot, environment, inputProps)
- );
-};