Skip to content

🎥 Make videos programmatically with React

`@remotion/studio-server`: Resolve composition source locations (#9894)

Jonny Burgerdd553ff55a65
diff --git a/packages/studio-server/src/helpers/resolve-composition-component.ts b/packages/studio-server/src/helpers/resolve-composition-component.ts
index 6d9752e6f9..c05e7c2f1f 100644
--- a/packages/studio-server/src/helpers/resolve-composition-component.ts
+++ b/packages/studio-server/src/helpers/resolve-composition-component.ts
@@ -361,9 +361,6 @@ const findReExportTargets = ({
recast.types.visit(ast, {
visitExportNamedDeclaration(astPath) {
const node = astPath.node as ExportNamedDeclaration;
- if (typeof node.source?.value !== 'string') {
- return false;
- }
for (const specifier of node.specifiers) {
if (specifier.type !== 'ExportSpecifier') {
@@ -380,6 +377,20 @@ const findReExportTargets = ({
continue;
}
+ // Support barrel files that import a component and export it in a
+ // separate declaration. See https://github.com/remotion-dev/remotion/issues/9172.
+ if (typeof node.source?.value !== 'string') {
+ const importTarget = findImportTarget({
+ ast,
+ componentName: localName,
+ });
+ if (importTarget) {
+ targets.push(importTarget);
+ }
+
+ continue;
+ }
+
targets.push({
importPath: node.source.value,
exportName: localName === 'default' ? 'default' : localName,
@@ -457,6 +468,9 @@ const findLocalSymbolLocation = ({
}): SourceLocation | null => {
let location: SourceLocation | null = null;
+ // Recast can omit the declaration location for exported functions and
+ // classes, including components resolved through barrel files. The identifier
+ // keeps its location. See https://github.com/remotion-dev/remotion/issues/9172.
recast.types.visit(ast, {
visitVariableDeclarator(astPath) {
if (location) {
@@ -465,7 +479,7 @@ const findLocalSymbolLocation = ({
const {node} = astPath;
if (node.id.type === 'Identifier' && node.id.name === name) {
- location = locationFromNode(node);
+ location = locationFromNode(node.id);
return false;
}
@@ -479,7 +493,7 @@ const findLocalSymbolLocation = ({
const {node} = astPath;
if (node.id?.name === name) {
- location = locationFromNode(node);
+ location = locationFromNode(node.id);
return false;
}
@@ -493,7 +507,7 @@ const findLocalSymbolLocation = ({
const {node} = astPath;
if (node.id?.name === name) {
- location = locationFromNode(node);
+ location = locationFromNode(node.id);
return false;
}
diff --git a/packages/studio-server/src/test/resolve-composition-component.test.ts b/packages/studio-server/src/test/resolve-composition-component.test.ts
index 65bba7b42c..524f5ef53f 100644
--- a/packages/studio-server/src/test/resolve-composition-component.test.ts
+++ b/packages/studio-server/src/test/resolve-composition-component.test.ts
@@ -85,6 +85,86 @@ test('resolves recursively through re-exported composition components', async ()
}
});
+test('resolves composition components that are imported and then exported', async () => {
+ const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'remotion-resolve-'));
+ try {
+ await fs.writeFile(
+ path.join(tempDir, 'Root.tsx'),
+ [
+ "import {Composition} from 'remotion';",
+ "import {ImportedOne, ImportedTwo, LocalComp} from './Barrel';",
+ 'export const RemotionRoot = () => {',
+ '\treturn <>',
+ '\t\t<Composition id="imported-one" component={ImportedOne} />',
+ '\t\t<Composition id="imported-two" component={ImportedTwo} />',
+ '\t\t<Composition id="local" component={LocalComp} />',
+ '\t</>;',
+ '};',
+ '',
+ ].join('\n'),
+ );
+ await fs.writeFile(
+ path.join(tempDir, 'Barrel.tsx'),
+ [
+ "import {ImportedOne} from './ImportedOne';",
+ "import {ImportedTwo as ImportedTwoLocal} from './ImportedTwo';",
+ '',
+ 'export function LocalComp() {',
+ '\treturn <div>local</div>;',
+ '}',
+ '',
+ 'export {ImportedOne, ImportedTwoLocal as ImportedTwo};',
+ '',
+ ].join('\n'),
+ );
+ await fs.writeFile(
+ path.join(tempDir, 'ImportedOne.tsx'),
+ [
+ '',
+ 'export function ImportedOne() {',
+ '\treturn <div>one</div>;',
+ '}',
+ '',
+ ].join('\n'),
+ );
+ await fs.writeFile(
+ path.join(tempDir, 'ImportedTwo.tsx'),
+ [
+ '',
+ '',
+ 'export function ImportedTwo() {',
+ '\treturn <div>two</div>;',
+ '}',
+ '',
+ ].join('\n'),
+ );
+
+ const locations = await Promise.all(
+ ['imported-one', 'imported-two', 'local'].map((compositionId) =>
+ resolveCompositionComponent({
+ remotionRoot: tempDir,
+ compositionFile: 'Root.tsx',
+ compositionId,
+ }),
+ ),
+ );
+
+ expect(
+ locations.map(({source, line, canAddSequence}) => ({
+ source,
+ line,
+ canAddSequence,
+ })),
+ ).toEqual([
+ {source: 'ImportedOne.tsx', line: 2, canAddSequence: true},
+ {source: 'ImportedTwo.tsx', line: 3, canAddSequence: true},
+ {source: 'Barrel.tsx', line: 4, canAddSequence: true},
+ ]);
+ } finally {
+ await fs.rm(tempDir, {recursive: true, force: true});
+ }
+});
+
test('resolves through fallback re-export branch after cycle', async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'remotion-resolve-'));
try {