10
const chalk = require('chalk');
11
const resolve = require('@rollup/plugin-node-resolve').nodeResolve;
12
const fs = require('fs');
13
-const path = require('path');
13
const argv = require('minimist')(process.argv.slice(2));
14
const Modules = require('./modules');
15
const Bundles = require('./bundles');
22
const {asyncRimRaf} = require('./utils');
23
const codeFrame = require('@babel/code-frame');
24
const Wrappers = require('./wrappers');
26
-const minify = require('terser').minify;
25
26
const RELEASE_CHANNEL = process.env.RELEASE_CHANNEL;
27
361
const isProduction = isProductionBundleType(bundleType);
362
const isProfiling = isProfilingBundleType(bundleType);
363
366
- const {shouldStayReadable} = getBundleTypeFlags(bundleType);
367
-
364
const needsMinifiedByClosure = isProduction && bundleType !== ESM_PROD;
365
370
- // Any other packages that should specifically _not_ have sourcemaps
371
- const sourcemapPackageExcludes = [
372
- // Having `//#sourceMappingUrl` for the `react-debug-tools` prod bundle breaks
373
- // `ReactDevToolsHooksIntegration-test.js`, because it changes Node's generated
374
- // stack traces and thus alters the hook name parsing behavior.
375
- // Also, this is an internal-only package that doesn't need sourcemaps anyway
376
- 'react-debug-tools',
377
- ];
378
-
379
- // Generate sourcemaps for true "production" build artifacts
380
- // that will be used by bundlers, such as `react-dom.production.min.js`.
381
- // Also include profiling builds as well.
382
- const needsSourcemaps =
383
- needsMinifiedByClosure &&
384
- // This will only exclude `unstable_server-external-runtime.js` artifact
385
- // To start generating sourcemaps for it, we should stop manually copying it to `facebook-www`
386
- // and force `react-dom` to include .map files in npm-package at the root level
387
- bundleType !== BROWSER_SCRIPT &&
388
- !sourcemapPackageExcludes.includes(entry) &&
389
- !shouldStayReadable;
390
-
366
return [
367
// Keep dynamic imports as externals
368
dynamicImports(),
469
// https://github.com/facebook/react/issues/10909
470
assume_function_wrapper: true,
471
497
- // Don't rename symbols (variable names, functions, etc). This will
498
- // be handled in a later step.
472
+ // Don't rename symbols (variable names, functions, etc). We leave
473
+ // this up to the application to handle, if they want. Otherwise gzip
474
+ // takes care of it.
475
renaming: false,
476
}),
477
needsMinifiedByClosure &&
494
);
495
},
496
},
521
- isProduction &&
522
- !shouldStayReadable && {
523
- name: 'mangle-symbol-names',
524
- async renderChunk(code, chunk, options, meta) {
525
- // Minify the code by mangling symbol names. We already ran Closure
526
- // on this code, so stuff like dead code elimination and inlining
527
- // has already happened. This step is purely to rename the symbols,
528
- // which we asked Closure to preserve.
529
- //
530
- // The only reason this is a separate step from Closure is so we
531
- // can publish non-mangled versions of the code for easier debugging
532
- // in production. We also publish sourcemaps that map back to the
533
- // non-mangled code (*not* the pre-Closure code).
534
-
535
- const outputFolder = path.dirname(options.file);
536
-
537
- // Represent the "original" bundle as a file with no `.min` in the name
538
- const filenameWithoutMin = filename.replace('.min', '');
539
- // There's _one_ artifact where the incoming filename actually contains
540
- // a folder name: "use-sync-external-store-shim/with-selector.production.js".
541
- // The output path already has the right structure, but we need to strip this
542
- // down to _just_ the JS filename.
543
- const preMinifiedFilename = path.basename(filenameWithoutMin);
544
- const preMinifiedBundlePath = path.join(
545
- outputFolder,
546
- preMinifiedFilename
547
- );
548
-
549
- // Use a path like `node_modules/react/cjs/react.production.min.js.map` for the sourcemap file
550
- const finalSourcemapPath = options.file.replace('.js', '.js.map');
551
- const finalSourcemapFilename = path.basename(finalSourcemapPath);
552
-
553
- const terserOptions = {
554
- // Don't bother compressing. Closure already did that.
555
- compress: false,
556
- toplevel: true,
557
- // Mangle the symbol names.
558
- mangle: {
559
- toplevel: true,
560
- },
561
- };
562
- if (needsSourcemaps) {
563
- terserOptions.sourceMap = {
564
- // Used to set the `file` field in the sourcemap
565
- filename: filename,
566
- // Used to set `# sourceMappingURL=` in the compiled code
567
- url: finalSourcemapFilename,
568
- };
569
- }
570
-
571
- const minifiedResult = await minify(
572
- {[preMinifiedFilename]: code},
573
- terserOptions
574
- );
575
-
576
- // Create the directory if it doesn't already exist
577
- fs.mkdirSync(outputFolder, {recursive: true});
578
-
579
- if (needsSourcemaps) {
580
- const sourcemapJSON = JSON.parse(minifiedResult.map);
581
-
582
- // All our code is considered "third-party" and should be ignored
583
- // by default
584
- sourcemapJSON.ignoreList = [0];
585
-
586
- // Write the sourcemap to disk
587
- fs.writeFileSync(
588
- finalSourcemapPath,
589
- JSON.stringify(sourcemapJSON)
590
- );
591
- }
592
-
593
- // Write the original source to disk as a separate file
594
- fs.writeFileSync(preMinifiedBundlePath, code);
595
-
596
- return {
597
- code: minifiedResult.code,
598
- // TODO: Maybe we should use Rollup's sourcemap feature instead
599
- // of writing it to disk manually?
600
- map: null,
601
- };
602
- },
603
- },
497
// Record bundle size.
498
sizes({
499
getSize: (size, gzip) => {