Repository: facebook/react
File: scripts/rollup/build.js (JavaScript)

Code before:

          // Represent the "original" bundle as a file with no `.min` in the name
          const filenameWithoutMin = filename.replace('.min', '');
          // There's _one_ artifact where the incoming filename actually contains
          // a folder name: "use-sync-external-store-shim/with-selector.production.js".
          // The output path already has the right structure, but we need to strip this
          // down to _just_ the JS filename.
          const preMinifiedFilename = path.basename(filenameWithoutMin);

          // CC generated a file list that only contains the tempfile name.
          // Replace that with a more meaningful "source" name for this bundle
          // that represents "the bundled source before minification".
          sourcemapAfterClosure.sources = [preMinifiedFilename];
          sourcemapAfterClosure.file = filename;

          // We'll write the pre-minified source to disk as a separate file.
          // Because it sits on disk, there's no need to have it in the `sourcesContent` array.
          // That also makes the file easier to read, and available for use by scripts.
          // This should be the only file in the array.
          const [preMinifiedBundleSourceWithoutLicense] =
            sourcemapAfterClosure.sourcesContent;

          // We were passing sources without license headers to the Closure compiler.
          // The reason for it is its optimization for multiline comment:
          // it will change the format by removing * prefixes This doesn't work well with Haste.
          const preMinifiedBundleSource = Wrappers.wrapWithLicenseHeader(
            preMinifiedBundleSourceWithoutLicense,
            bundleType,
            globalName,
            filename,
            moduleType
          );

          // Update the lineCount field, since we've added the license header
          // to the minified source, but Closure didn't know about it.
          const sourceCodeLines = codeAfterLicense.split(/\r\n|\r|\n/);
          // There is probably an empty line at the end after the Prettier run
          while (
            sourceCodeLines.length > 0 &&
            sourceCodeLines[sourceCodeLines.length - 1] === ''
          ) {
            sourceCodeLines.pop();
          }
          sourcemapAfterClosure.lineCount = sourceCodeLines.length;

          // Remove this entirely - we're going to write the file to disk instead.
          delete sourcemapAfterClosure.sourcesContent;

          const preMinifiedBundlePath = path.join(
            outputFolder,
            preMinifiedFilename

Review comment (bug):
:question: Hmm. I _am_ curious if the sourcemap transforms are still going to be accurate vs the output file contents if we don't include the license contents before the sourcemap gets generated.

This is an area where my own knowledge is very hazy.  My coworker @hbenl _might_ have more knowledge here.

My concern is that sending one version of the source file through Closure, and writing a modified file to disk, might cause line numbers to get mismatched when it's used in a debugger (such that it thinks you're paused on line 37 of a file, but that turns out to be a `}` character or something instead of the actual meaningful line of code).

I'd recommend doing some checks with the artifacts from this PR to see if setting breakpoints and stepping still seems to result in correct lines and variables being handled.

Code after:
            fs.readFileSync(finalSourcemapPath, 'utf8')
          );

          // Represent the "original" bundle as a file with no `.min` in the name
          const filenameWithoutMin = filename.replace('.min', '');
          // There's _one_ artifact where the incoming filename actually contains
          // a folder name: "use-sync-external-store-shim/with-selector.production.js".
          // The output path already has the right structure, but we need to strip this
          // down to _just_ the JS filename.
          const preMinifiedFilename = path.basename(filenameWithoutMin);

          // CC generated a file list that only contains the tempfile name.
          // Replace that with a more meaningful "source" name for this bundle
          // that represents "the bundled source before minification".
          sourcemapAfterClosure.sources = [preMinifiedFilename];
          sourcemapAfterClosure.file = filename;

          // We'll write the pre-minified source to disk as a separate file.
          // Because it sits on disk, there's no need to have it in the `sourcesContent` array.
          // That also makes the file easier to read, and available for use by scripts.
          // This should be the only file in the array.
          const [preMinifiedBundleSource] =
            sourcemapAfterClosure.sourcesContent;

          // Remove this entirely - we're going to write the file to disk instead.
          delete sourcemapAfterClosure.sourcesContent;

          const preMinifiedBundlePath = path.join(
            outputFolder,
            preMinifiedFilename
          );

          // Write the original source to disk as a separate file
          fs.writeFileSync(preMinifiedBundlePath, preMinifiedBundleSource);

          // Overwrite the Closure-generated file with the final combined sourcemap
          fs.writeFileSync(
            finalSourcemapPath,
            JSON.stringify(sourcemapAfterClosure)
          );

          // Add the sourcemap URL to the actual bundle, so that tools pick it up
          const sourceWithMappingUrl =
            minifiedCodeWithChangedHeader +
            `\n//# sourceMappingURL=${finalSourcemapFilename}`;

          return {
            code: sourceWithMappingUrl,
            map: null,
          };
        },