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

Code before:
          assume_function_wrapper: !isUMDBundle,
          renaming: !shouldStayReadable,
        },
        {needsSourcemaps}
      ),
    needsSourcemaps && {
      name: 'chunk-after-closure',
      renderChunk(code, config, options) {
        // Side effect - grab the code as Closure mangled it
        chunkCodeAfterClosureCompiler = code;
      },
    },
    // Add the whitespace back if necessary.
    shouldStayReadable &&
      prettier({
        parser: 'flow',
        singleQuote: false,
        trailingComma: 'none',
        bracketSpacing: true,
      }),
    // License and haste headers, top-level `if` blocks.
    {
      name: 'license-and-headers',
      renderChunk(source) {
        return Wrappers.wrapBundle(
          source,
          bundleType,
          globalName,
          filename,
          moduleType,
          bundle.wrapWithModuleBoundaries
        );
      },
    },
    needsSourcemaps && {
      name: 'generate-prod-bundle-sourcemaps',
      async renderChunk(codeAfterLicense, chunk, options, meta) {
        // We want to generate a sourcemap that shows the production bundle source
        // as it existed before Closure Compiler minified that chunk.
        // We also need to apply any license/wrapper text adjustments to that
        // sourcemap, so that the mapped locations line up correctly.

        // We can split the final chunk code to figure out what got added around
        // the code from the Closure step.
        const [licensePrefix, licensePostfix] = codeAfterLicense.split(
          chunkCodeAfterClosureCompiler
        );

        const transformedSource = new MagicString(
          chunkCodeAfterClosureCompiler
        );

Review comment (suggestion):
See my notes/exploration/proposal on a previous PR for how this `renderChunk` and the related `Wrappers.wrapBundle` code could make use of `magic-string` for sourcemap support:

- https://github.com/facebook/react/pull/21946#discussion_r917339710 (and following comments)

Code after:
                ? 'ECMASCRIPT5'
                : 'ECMASCRIPT5_STRICT',
            emit_use_strict:
              bundleType !== BROWSER_SCRIPT &&
              bundleType !== ESM_PROD &&
              bundleType !== ESM_DEV,
            env: 'CUSTOM',
            warning_level: 'QUIET',
            source_map_include_content: true,
            use_types_for_optimization: false,
            process_common_js_modules: false,
            rewrite_polyfills: false,
            inject_libraries: false,
            allow_dynamic_import: true,

            // Don't let it create global variables in the browser.
            // https://github.com/facebook/react/issues/10909
            assume_function_wrapper: !isUMDBundle,
            renaming: !shouldStayReadable,
          },
          {needsSourcemaps}
        ),
      // Add the whitespace back if necessary.
      shouldStayReadable &&
        prettier({
          parser: 'flow',
          singleQuote: false,
          trailingComma: 'none',
          bracketSpacing: true,
        }),
      needsSourcemaps && {
        name: 'generate-prod-bundle-sourcemaps',
        async renderChunk(codeAfterLicense, chunk, options, meta) {
          // We want to generate a sourcemap that shows the production bundle source
          // as it existed before Closure Compiler minified that chunk, rather than
          // showing the "original" individual source files. This better shows
          // what is actually running in the app.

          // Use a path like `node_modules/react/cjs/react.production.min.js.map` for the sourcemap file
          const finalSourcemapPath = options.file.replace('.js', '.js.map');
          const finalSourcemapFilename = path.basename(finalSourcemapPath);
          const outputFolder = path.dirname(options.file);

          // Read the sourcemap that Closure wrote to disk
          const sourcemapAfterClosure = JSON.parse(
            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