Skip to content

🎥 Make videos programmatically with React

improve source maps

Jonny Burgerb6911f4f4646
diff --git a/packages/bundler/package.json b/packages/bundler/package.json
index 36c68b5877..9ad0bbc28a 100644
--- a/packages/bundler/package.json
+++ b/packages/bundler/package.json
@@ -31,7 +31,7 @@
"react-refresh": "0.9.0",
"remotion": "2.6.11",
"semver": "7.3.4",
- "source-map": "0.5.7",
+ "source-map": "0.6.1",
"source-map-loader": "3.0.0",
"style-loader": "2.0.0",
"webpack": "5.60.0"
@@ -49,7 +49,6 @@
"@types/react": "17.0.39",
"@types/react-dom": "^17.0.11",
"@types/semver": "^7.3.4",
- "@types/source-map": "0.5.2",
"@types/webpack-env": "^1.16.0",
"eslint": "^7.25.0",
"jest": "^27.2.4",
diff --git a/packages/bundler/src/error-overlay/react-overlay/utils/get-source-map.ts b/packages/bundler/src/error-overlay/react-overlay/utils/get-source-map.ts
index 6fa9bcc4d5..8365f83787 100644
--- a/packages/bundler/src/error-overlay/react-overlay/utils/get-source-map.ts
+++ b/packages/bundler/src/error-overlay/react-overlay/utils/get-source-map.ts
@@ -11,44 +11,23 @@
* LICENSE file in the root directory of this source tree.
*/
-import {SourceMapConsumer} from 'source-map';
+import {RawSourceMap, SourceMapConsumer} from 'source-map';
-/**
- * A wrapped instance of a <code>{@link https://github.com/mozilla/source-map SourceMapConsumer}</code>.
- *
- * This exposes methods which will be indifferent to changes made in <code>{@link https://github.com/mozilla/source-map source-map}</code>.
- */
-export class SourceMap {
- __source_map: SourceMapConsumer;
-
- constructor(sourceMap: SourceMapConsumer) {
- this.__source_map = sourceMap;
- }
-
- /**
- * Returns the original code position for a generated code position.
- * @param {number} line The line of the generated code position.
- * @param {number} column The column of the generated code position.
- */
- getOriginalPosition(
- line: number,
- column: number
- ): {source: string; line: number; column: number} {
- const {
- line: l,
- column: c,
- source: s,
- } = this.__source_map.originalPositionFor({
- line,
- column,
- });
- return {line: l as number, column: c as number, source: s as string};
- }
-
- getSource(sourceName: string): string | null {
- return this.__source_map.sourceContentFor(sourceName);
- }
-}
+export const getOriginalPosition = (
+ source_map: SourceMapConsumer,
+ line: number,
+ column: number
+): {source: string; line: number; column: number} => {
+ const {
+ line: l,
+ column: c,
+ source: s,
+ } = source_map.originalPositionFor({
+ line,
+ column,
+ });
+ return {line: l as number, column: c as number, source: s as string};
+};
function extractSourceMapUrl(
fileUri: string,
@@ -74,16 +53,11 @@ function extractSourceMapUrl(
return Promise.resolve(match[1].toString());
}
-/**
- * Returns an instance of <code>{@link SourceMap}</code> for a given fileUri and fileContents.
- * @param {string} fileUri The URI of the source file.
- * @param {string} fileContents The contents of the source file.
- */
-async function getSourceMap(
+export async function getSourceMap(
fileUri: string,
fileContents: string
-): Promise<SourceMap> {
- let sm = await extractSourceMapUrl(fileUri, fileContents);
+): Promise<SourceMapConsumer> {
+ const sm = await extractSourceMapUrl(fileUri, fileContents);
if (sm.indexOf('data:') === 0) {
const base64 = /^data:application\/json;([\w=:"-]+;)*base64,/;
const match2 = sm.match(base64);
@@ -93,16 +67,12 @@ async function getSourceMap(
);
}
- sm = sm.substring(match2[0].length);
- sm = window.atob(sm);
- sm = JSON.parse(sm);
- return new SourceMap(new SourceMapConsumer(sm));
+ const converted = window.atob(sm.substring(match2[0].length));
+ return new SourceMapConsumer(JSON.parse(converted) as RawSourceMap);
}
const index = fileUri.lastIndexOf('/');
const url = fileUri.substring(0, index + 1) + sm;
const obj = await fetch(url).then((res) => res.json());
- return new SourceMap(new SourceMapConsumer(obj));
+ return new SourceMapConsumer(obj);
}
-
-export {getSourceMap};
diff --git a/packages/bundler/src/error-overlay/react-overlay/utils/unmapper.ts b/packages/bundler/src/error-overlay/react-overlay/utils/unmapper.ts
index 7f0e7f36a8..44c302e6d2 100644
--- a/packages/bundler/src/error-overlay/react-overlay/utils/unmapper.ts
+++ b/packages/bundler/src/error-overlay/react-overlay/utils/unmapper.ts
@@ -12,8 +12,9 @@
*/
import {Internals} from 'remotion';
+import {SourceMapConsumer} from 'source-map';
import {getLinesAround} from './get-lines-around';
-import {getSourceMap, SourceMap} from './get-source-map';
+import {getOriginalPosition, getSource, getSourceMap} from './get-source-map';
import {
SomeStackFrame,
StackFrame,
@@ -43,7 +44,7 @@ export const unmap = async (
return getSourceMap(fileName as string, fileContents as string);
})
);
- const mapValues: Record<string, SourceMap> = {};
+ const mapValues: Record<string, SourceMapConsumer> = {};
for (let i = 0; i < uniqueFileNames.length; i++) {
mapValues[uniqueFileNames[i]] = maps[i];
}
@@ -54,13 +55,16 @@ export const unmap = async (
}
const map = mapValues[frame.frame.fileName as string];
- const pos = map.getOriginalPosition(
+ const pos = getOriginalPosition(
+ map,
frame.frame.lineNumber as number,
frame.frame.columnNumber as number
);
const {functionName} = frame.frame;
- const hasSource = map.getSource(pos.source);
+ let hasSource: string | null = null;
+ hasSource = pos.source ? map.sourceContentFor(pos.source, false) : null;
+
const scriptCode = hasSource
? getLinesAround(pos.line, contextLines, hasSource.split('\n'))
: null;
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 2810972ea9..cd0d462ea9 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -60,7 +60,6 @@ importers:
'@types/react': 17.0.39
'@types/react-dom': ^17.0.11
'@types/semver': ^7.3.4
- '@types/source-map': 0.5.2
'@types/webpack-env': ^1.16.0
css-loader: 5.2.7
esbuild-loader: 2.15.1
@@ -78,7 +77,7 @@ importers:
react-refresh: 0.9.0
remotion: 2.6.11
semver: 7.3.4
- source-map: 0.5.7
+ source-map: 0.6.1
source-map-loader: 3.0.0
style-loader: 2.0.0
ts-jest: ^27.0.5
@@ -95,7 +94,7 @@ importers:
react-refresh: 0.9.0
remotion: link:../core
semver: 7.3.4
- source-map: 0.5.7
+ source-map: 0.6.1
source-map-loader: [email protected]
style-loader: [email protected]
webpack: 5.60.0
@@ -108,7 +107,6 @@ importers:
'@types/react': 17.0.39
'@types/react-dom': 17.0.11
'@types/semver': 7.3.8
- '@types/source-map': 0.5.2
'@types/webpack-env': 1.16.2
eslint: 7.32.0
jest: 27.2.4
@@ -7026,10 +7024,6 @@ packages:
/@types/source-list-map/0.1.2:
resolution: {integrity: sha512-K5K+yml8LTo9bWJI/rECfIPrGgxdpeNbj+d53lwN4QjW1MCwlkhUms+gtdzigTeUyBr09+u8BwOIY3MXvHdcsA==}
- /@types/source-map/0.5.2:
- resolution: {integrity: sha512-++w4WmMbk3dS3UeHGzAG+xJOSz5Xqtjys/TBkqG3qp3SeWE7Wwezqe5eB7B51cxUyh4PW7bwVotpsLdBK0D8cw==}
- dev: true
-
/@types/stack-utils/2.0.1:
resolution: {integrity: sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==}