🎥 Make videos programmatically with React
`@remotion/studio-server`: Speed up source lookups for large compositions
Jonny Burger45059bffe6ef
diff --git a/packages/codemods/src/index.ts b/packages/codemods/src/index.tsindex 329cbf736f..19a415747c 100644--- a/packages/codemods/src/index.ts+++ b/packages/codemods/src/index.ts@@ -21,6 +21,7 @@ import {getFolderFile,getRootFileForProject,} from './internals';+import {resolveNodePathsFromLocations} from './resolve-node-paths-from-locations';import {computeSequencePropsSubscriptionFromContent} from './sequence-props';import {computeEffectPropsStatusesFromContent} from './sequence-props/can-update-effect-props';import {getDurationDependentKeyframesFromContent} from './sequence-props/duration-dependent-keyframes';@@ -197,6 +198,7 @@ export const CodemodsInternals = {pasteEffects,resolveCompositionComponent,resolveCompositionComponentWithFile,+ resolveNodePathsFromLocations,updateInlineCaptionPatches,};diff --git a/packages/codemods/src/resolve-node-paths-from-locations.ts b/packages/codemods/src/resolve-node-paths-from-locations.tsnew file mode 100644index 0000000000..45de43f2ee--- /dev/null+++ b/packages/codemods/src/resolve-node-paths-from-locations.ts@@ -0,0 +1,63 @@+import * as recast from 'recast';+import type {SequenceNodePath} from 'remotion';+import {recastLocToOffset} from './recast-loc-to-offset';+import {getNodePathForRecastPath} from './sequence-props';+import {getReadOnlySourceSnapshot} from './sequence-props-snapshot';++export const resolveNodePathsFromLocations = ({+ fileContents,+ targets,+}: {+ fileContents: string;+ targets: {line: number; column: number}[];+}): (SequenceNodePath | null)[] => {+ const {ast} = getReadOnlySourceSnapshot(fileContents);+ const matchesByLine = new Map<+ number,+ {+ lastNodePath: SequenceNodePath | null;+ byColumn: Map<number, {nodePath: SequenceNodePath; count: number}>;+ }+ >();+ for (const {line} of targets) {+ if (!matchesByLine.has(line)) {+ matchesByLine.set(line, {lastNodePath: null, byColumn: new Map()});+ }+ }++ // Recast normalizes whitespace and line endings. Convert its locations back+ // to source columns without splitting the entire source prefix for each node.+ const lineStarts = [0];+ for (const match of fileContents.matchAll(/\r\n|[\r\n\u2028\u2029]/g)) {+ lineStarts.push(match.index + match[0].length);+ }++ recast.visit(ast, {+ visitJSXOpeningElement(p) {+ const location = p.node.loc?.start;+ const matches = location ? matchesByLine.get(location.line) : undefined;+ if (location && matches) {+ const nodePath = getNodePathForRecastPath(p, ast);+ const column =+ recastLocToOffset(fileContents, location) -+ lineStarts[location.line - 1];+ const previous = matches.byColumn.get(column);+ matches.lastNodePath = nodePath;+ matches.byColumn.set(column, {+ nodePath,+ count: (previous?.count ?? 0) + 1,+ });+ }++ return this.traverse(p);+ },+ });++ return targets.map(({line, column}) => {+ const matches = matchesByLine.get(line)!;+ const exact = matches.byColumn.get(column);+ // Preserve the last JSX element on the line as the fallback for stale+ // source-map columns, including when a column match is ambiguous.+ return exact?.count === 1 ? exact.nodePath : matches.lastNodePath;+ });+};diff --git a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.tsindex 52c496e247..fd1d9c8396 100644--- a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts+++ b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts@@ -13,7 +13,7 @@ import type {TSAsExpression,UnaryExpression,} from '@babel/types';-import {CodemodsInternals, getNodeProps, getNodes} from '@remotion/codemods';+import {CodemodsInternals, getNodeProps} from '@remotion/codemods';import {RenderInternals} from '@remotion/renderer';import type {SubscribeToSequencePropsResponse} from '@remotion/studio-shared';import {LINEAR_KEYFRAME_EASING} from '@remotion/studio-shared';@@ -1353,19 +1353,9 @@ export const resolveSequencePropsNodePathsFromFilename = ({action: 'read',});const fileContents = readFileSync(absolutePath, 'utf-8');- const nodes = getNodes({- project: {rootDir: remotionRoot, files: {[absolutePath]: fileContents}},- filePath: absolutePath,- });- return targets.map(({line, column}) => {- const lineMatches = nodes.filter((node) => node.location?.line === line);- const exactMatches = lineMatches.filter(- (node) => node.location?.column === column,- );- return (- (exactMatches.length === 1 ? exactMatches[0] : lineMatches.at(-1))- ?.nodePath ?? null- );+ return CodemodsInternals.resolveNodePathsFromLocations({+ fileContents,+ targets,});};