Home / Function/ extractAndLoadSourceMapJSON() — react Function Reference

extractAndLoadSourceMapJSON() — react Function Reference

Architecture documentation for the extractAndLoadSourceMapJSON() function in loadSourceAndMetadata.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  af50a88a_0828_5d3a_feb9_e7a9a1f805c9["extractAndLoadSourceMapJSON()"]
  c387cbce_0129_194c_6a44_3db01fd37956["loadSourceAndMetadata.js"]
  af50a88a_0828_5d3a_feb9_e7a9a1f805c9 -->|defined in| c387cbce_0129_194c_6a44_3db01fd37956
  c1ede884_eff1_5699_b978_8ab7d7207919["loadSourceAndMetadata()"]
  c1ede884_eff1_5699_b978_8ab7d7207919 -->|calls| af50a88a_0828_5d3a_feb9_e7a9a1f805c9
  c7187c20_83d4_233c_bb9f_4f534fe940fb["decodeBase64String()"]
  af50a88a_0828_5d3a_feb9_e7a9a1f805c9 -->|calls| c7187c20_83d4_233c_bb9f_4f534fe940fb
  fd9af6fe_0d02_805e_69cc_273e539a9202["sourceMapIncludesSource()"]
  af50a88a_0828_5d3a_feb9_e7a9a1f805c9 -->|calls| fd9af6fe_0d02_805e_69cc_273e539a9202
  1dae5451_3f6a_7ec3_9bf0_bffbac42a252["fetchFile()"]
  af50a88a_0828_5d3a_feb9_e7a9a1f805c9 -->|calls| 1dae5451_3f6a_7ec3_9bf0_bffbac42a252
  style af50a88a_0828_5d3a_feb9_e7a9a1f805c9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/react-devtools-shared/src/hooks/parseHookNames/loadSourceAndMetadata.js lines 125–304

function extractAndLoadSourceMapJSON(
  locationKeyToHookSourceAndMetadata: LocationKeyToHookSourceAndMetadata,
): Promise<mixed> {
  // Deduplicate fetches, since there can be multiple location keys per source map.
  const dedupedFetchPromises = new Map<string, Promise<$FlowFixMe>>();

  if (__DEBUG__) {
    console.log(
      'extractAndLoadSourceMapJSON() load',
      locationKeyToHookSourceAndMetadata.size,
      'source maps',
    );
  }

  const setterPromises = [];
  locationKeyToHookSourceAndMetadata.forEach(hookSourceAndMetadata => {
    const sourceMapRegex = / ?sourceMappingURL=([^\s'"]+)/gm;
    const runtimeSourceCode =
      ((hookSourceAndMetadata.runtimeSourceCode: any): string);

    // TODO (named hooks) Search for our custom metadata first.
    // If it's found, we should use it rather than source maps.

    // TODO (named hooks) If this RegExp search is slow, we could try breaking it up
    // first using an indexOf(' sourceMappingURL=') to find the start of the comment
    // (probably at the end of the file) and then running the RegExp on the remaining substring.
    let sourceMappingURLMatch = withSyncPerfMeasurements(
      'sourceMapRegex.exec(runtimeSourceCode)',
      () => sourceMapRegex.exec(runtimeSourceCode),
    );

    if (sourceMappingURLMatch == null) {
      if (__DEBUG__) {
        console.log('extractAndLoadSourceMapJSON() No source map found');
      }

      // Maybe file has not been transformed; we'll try to parse it as-is in parseSourceAST().
    } else {
      const externalSourceMapURLs = [];
      while (sourceMappingURLMatch != null) {
        const {runtimeSourceURL} = hookSourceAndMetadata;
        const sourceMappingURL = sourceMappingURLMatch[1];
        const hasInlineSourceMap = sourceMappingURL.indexOf('base64,') >= 0;
        if (hasInlineSourceMap) {
          try {
            // TODO (named hooks) deduplicate parsing in this branch (similar to fetching in the other branch)
            // since there can be multiple location keys per source map.

            // Web apps like Code Sandbox embed multiple inline source maps.
            // In this case, we need to loop through and find the right one.
            // We may also need to trim any part of this string that isn't based64 encoded data.
            const trimmed = ((sourceMappingURL.match(
              /base64,([a-zA-Z0-9+\/=]+)/,
            ): any): Array<string>)[1];
            const decoded = withSyncPerfMeasurements(
              'decodeBase64String()',
              () => decodeBase64String(trimmed),
            );

            const sourceMapJSON = withSyncPerfMeasurements(
              'JSON.parse(decoded)',
              () => JSON.parse(decoded),
            );

            if (__DEBUG__) {
              console.groupCollapsed(
                'extractAndLoadSourceMapJSON() Inline source map',
              );
              console.log(sourceMapJSON);
              console.groupEnd();
            }

            // Hook source might be a URL like "https://4syus.csb.app/src/App.js"
            // Parsed source map might be a partial path like "src/App.js"
            if (sourceMapIncludesSource(sourceMapJSON, runtimeSourceURL)) {
              hookSourceAndMetadata.sourceMapJSON = sourceMapJSON;

              // OPTIMIZATION If we've located a source map for this source,
              // we'll use it to retrieve the original source (to extract hook names).
              // We only fall back to parsing the full source code is when there's no source map.
              // The source is (potentially) very large,

Domain

Subdomains

Frequently Asked Questions

What does extractAndLoadSourceMapJSON() do?
extractAndLoadSourceMapJSON() is a function in the react codebase, defined in packages/react-devtools-shared/src/hooks/parseHookNames/loadSourceAndMetadata.js.
Where is extractAndLoadSourceMapJSON() defined?
extractAndLoadSourceMapJSON() is defined in packages/react-devtools-shared/src/hooks/parseHookNames/loadSourceAndMetadata.js at line 125.
What does extractAndLoadSourceMapJSON() call?
extractAndLoadSourceMapJSON() calls 3 function(s): decodeBase64String, fetchFile, sourceMapIncludesSource.
What calls extractAndLoadSourceMapJSON()?
extractAndLoadSourceMapJSON() is called by 1 function(s): loadSourceAndMetadata.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free