From 8b9a354f0474894b9b0922a1d0fc468668b9cdfc Mon Sep 17 00:00:00 2001 From: Oleksandr Fediashov Date: Thu, 11 Jan 2024 11:30:27 +0100 Subject: [PATCH] chore: fix getOriginalPosition() to work in Storybook (#484) --- ...-a62e678a-424d-48c1-a910-c370523a4977.json | 7 ++++++ .../src/sourceMap/sourceMapConsumer.ts | 22 ++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 change/@griffel-devtools-a62e678a-424d-48c1-a910-c370523a4977.json diff --git a/change/@griffel-devtools-a62e678a-424d-48c1-a910-c370523a4977.json b/change/@griffel-devtools-a62e678a-424d-48c1-a910-c370523a4977.json new file mode 100644 index 000000000..d1f396d8a --- /dev/null +++ b/change/@griffel-devtools-a62e678a-424d-48c1-a910-c370523a4977.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "chore: fix getOriginalPosition() to work in Storybook", + "packageName": "@griffel/devtools", + "email": "olfedias@microsoft.com", + "dependentChangeType": "patch" +} diff --git a/packages/devtools/src/sourceMap/sourceMapConsumer.ts b/packages/devtools/src/sourceMap/sourceMapConsumer.ts index 09bb7f9de..eb4eadaca 100644 --- a/packages/devtools/src/sourceMap/sourceMapConsumer.ts +++ b/packages/devtools/src/sourceMap/sourceMapConsumer.ts @@ -1,19 +1,25 @@ -import type { MappedPosition, RawSourceMap } from 'source-map-js'; import { SourceMapConsumer } from 'source-map-js'; +import type { MappedPosition, RawSourceMap } from 'source-map-js'; export const resources: Promise = new Promise(resolve => { - chrome.devtools.inspectedWindow.getResources(currResources => { - resolve(currResources); - }); -}); -chrome.devtools.inspectedWindow.onResourceAdded.addListener(async resource => { - if (!resource.url.startsWith('debugger')) { - (await resources).push(resource); + if (chrome.devtools) { + chrome.devtools.inspectedWindow.getResources(currResources => { + resolve(currResources); + }); } }); +if (chrome.devtools) { + chrome.devtools.inspectedWindow.onResourceAdded.addListener(async resource => { + if (!resource.url.startsWith('debugger')) { + (await resources).push(resource); + } + }); +} + export async function getOriginalPosition(sourceMapJSON: RawSourceMap, sourceLoc: MappedPosition) { const { source, line, column } = sourceLoc; + try { const sourceMapConsumer = new SourceMapConsumer(sourceMapJSON); const result = sourceMapConsumer.originalPositionFor({ line, column });