Skip to content

🎥 Make videos programmatically with React

`@remotion/bundler`: Allow importing a script that declares the variables _a or _b

Jonny Burger299f1b6a3b3a
diff --git a/packages/bundler/src/fast-refresh/loader.ts b/packages/bundler/src/fast-refresh/loader.ts
index 51c39b9f66..1f086ff59b 100644
--- a/packages/bundler/src/fast-refresh/loader.ts
+++ b/packages/bundler/src/fast-refresh/loader.ts
@@ -221,7 +221,13 @@ refreshModuleRuntime = refreshModuleRuntime.slice(
);
const ReactRefreshLoader: LoaderDefinition = function (source, inputSourceMap) {
- this.callback(null, `${source}\n\n;${refreshModuleRuntime}`, inputSourceMap);
+ // Importing a module that declares the global variables _a and _b
+ // will conflict with the global variables that React Fast Refresh uses.
+ const renamedSymbols = refreshModuleRuntime
+ .replace(/_a/g, '_remotion_globalVariableA')
+ .replace(/_b/g, '_remotion_globalVariableB');
+
+ this.callback(null, `${source}\n;${renamedSymbols}`, inputSourceMap);
};
export default ReactRefreshLoader;
diff --git a/packages/bundler/src/test/warrant-underscore-a-hack.test.ts b/packages/bundler/src/test/warrant-underscore-a-hack.test.ts
new file mode 100644
index 0000000000..1b811bdb18
--- /dev/null
+++ b/packages/bundler/src/test/warrant-underscore-a-hack.test.ts
@@ -0,0 +1,9 @@
+import {readFileSync} from 'fs';
+import {join} from 'path';
+import {expect, test} from 'vitest';
+
+test('Warrant the hack we have in loader.ts ', () => {
+ const path = join(process.cwd(), 'dist', 'fast-refresh', 'loader.js');
+ const contents = readFileSync(path, 'utf-8');
+ expect(contents).toContain('var _a, _b');
+});