react-native-windows icon indicating copy to clipboard operation
react-native-windows copied to clipboard

Sourcemaps broken on vnext template

Open thristhart opened this issue 7 years ago • 6 comments

Repro steps

  • Follow steps in Getting Started/Simple install method
    • react-native windows --template vnext
    • react-native run-windows
  • Open Chrome Dev Tools on React Native Debugger UI
  • Click Sources tab

Observed behavior

The only source available is the bundle. image

Expected behavior

The sources for the app are visible. image

Notes

I believe this is happening because the sourceMappingURL provided in the bundle is a protocol-relative URL. It looks like:

//# sourceMappingURL=//localhost:8081/App.windows.map?platform=uwp&dev=true&hot=false

Protocol-relative URLs use the current protocol of the file being parsed. Because of the black magic React Native is doing to get the bundle running in a worker, the URL for the bundled file is a blob like: blob:http://localhost:8081/4528c459-25c4-4db5-a23a-96c78738bf71. Therefore, the protocol is blob and the source maps it attempts to load are blob://localhost:8081/App.windows.map?platform=uwp&dev=true&hot=false, which doesn't work.

It's not obvious to me why this is happening. It doesn't occur on the Android react native template -- that has sourceMappingUrls with an http protocol specified. I tried to figure out what the difference is, but I don't have the metro experience to identify it.

Workaround

Manually add the source map by right clicking on the bundle file: image and put in the same sourceMappingUrl, but with the specific http protocol: image This will work for this current bundle, but because the filename includes a hash it will change and break every time the file changes, which is inconvenient.

thristhart avatar May 16 '19 01:05 thristhart

Update: I've figured out the source of the problem. It's this code in metro:

        // The remote chrome debugger loads bundles via Blob urls, whose
        // protocol is blob:http. This breaks loading source maps through
        // protocol-relative URLs, which is why we must force the HTTP protocol
        // when loading the bundle for either iOS or Android.
        protocol:
          platform != null && platform.match(/^(android|ios)$/) ? 'http' : '',

They correctly identified that it's an issue, but only fixed it for Android and iOS. Given that the vnext platform is uwp, we still get the empty protocol.

Opened issue on metro: facebook/metro#406 but I'd like to know if we can work around this on our side.

thristhart avatar May 20 '19 22:05 thristhart

Thanks for filing this.

Hey @acoates-ms - can you see if there is a workaround on our side that can be done for this while we wait for the metro issue to be fixed?

harinikmsft avatar May 22 '19 19:05 harinikmsft

@harinikmsft , I'm less familiar with the metro config's since we primarily use haul. It might be worth pinging in the discord to see if this hits the other out of tree platforms.

acoates-ms avatar May 22 '19 19:05 acoates-ms

A potential workaround is to modify BundleUrlFormat in DevServerHelper to enable inline source maps:

private const string BundleUrlFormat = "http://{0}/{1}.bundle?platform=windows&dev={2}&hot={3}&inlineSourceMaps=true";

(or some more configurable way of doing that.) Inline source maps are less desirable, I think, but a significant improvement over no sourcemaps.

thristhart avatar May 22 '19 20:05 thristhart

@acoates-ms , @Thristhart

This does affect other out of tree platforms. I posted about this in discord and found this issue shortly afterwards.

CaptainNic avatar May 22 '19 20:05 CaptainNic

Closing this for now as the issue has been worked around in #2553 and the source of the issue is upstream in metro anyway. Hopefully the metro issue will be resolved and we can undo the inline sourcemaps workaround.

thristhart avatar Jun 01 '19 04:06 thristhart