@@ -11,6 +11,7 @@ const stripBanner = require('rollup-plugin-strip-banner');
11
11
const chalk = require ( 'chalk' ) ;
12
12
const resolve = require ( '@rollup/plugin-node-resolve' ) . nodeResolve ;
13
13
const fs = require ( 'fs' ) ;
14
+ const path = require ( 'path' ) ;
14
15
const argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) ) ;
15
16
const Modules = require ( './modules' ) ;
16
17
const Bundles = require ( './bundles' ) ;
@@ -148,6 +149,7 @@ function getBabelConfig(
148
149
presets : [ ] ,
149
150
plugins : [ ...babelPlugins ] ,
150
151
babelHelpers : 'bundled' ,
152
+ sourcemap : false ,
151
153
} ;
152
154
if ( isDevelopment ) {
153
155
options . plugins . push (
@@ -386,6 +388,16 @@ function getPlugins(
386
388
387
389
const { isUMDBundle, shouldStayReadable} = getBundleTypeFlags ( bundleType ) ;
388
390
391
+ const needsMinifiedByClosure = isProduction && bundleType !== ESM_PROD ;
392
+ // Only generate sourcemaps for true "production" build artifacts
393
+ // that will be used by bundlers, such as `react-dom.production.min.js`.
394
+ // UMD and "profiling" builds are rarely used and not worth having sourcemaps.
395
+ const needsSourcemaps =
396
+ needsMinifiedByClosure &&
397
+ ! isProfiling &&
398
+ ! isUMDBundle &&
399
+ ! shouldStayReadable ;
400
+
389
401
return [
390
402
// Keep dynamic imports as externals
391
403
dynamicImports ( ) ,
@@ -395,7 +407,7 @@ function getPlugins(
395
407
const transformed = flowRemoveTypes ( code ) ;
396
408
return {
397
409
code : transformed . toString ( ) ,
398
- map : transformed . generateMap ( ) ,
410
+ map : null ,
399
411
} ;
400
412
} ,
401
413
} ,
@@ -424,6 +436,7 @@ function getPlugins(
424
436
) ,
425
437
// Remove 'use strict' from individual source files.
426
438
{
439
+ name : "remove 'use strict'" ,
427
440
transform ( source ) {
428
441
return source . replace ( / [ ' " ] u s e s t r i c t [ " ' ] / g, '' ) ;
429
442
} ,
@@ -443,47 +456,9 @@ function getPlugins(
443
456
// I'm going to port "art" to ES modules to avoid this problem.
444
457
// Please don't enable this for anything else!
445
458
isUMDBundle && entry === 'react-art' && commonjs ( ) ,
446
- // Apply dead code elimination and/or minification.
447
- // closure doesn't yet support leaving ESM imports intact
448
- isProduction &&
449
- bundleType !== ESM_PROD &&
450
- closure ( {
451
- compilation_level : 'SIMPLE' ,
452
- language_in : 'ECMASCRIPT_2020' ,
453
- language_out :
454
- bundleType === NODE_ES2015
455
- ? 'ECMASCRIPT_2020'
456
- : bundleType === BROWSER_SCRIPT
457
- ? 'ECMASCRIPT5'
458
- : 'ECMASCRIPT5_STRICT' ,
459
- emit_use_strict :
460
- bundleType !== BROWSER_SCRIPT &&
461
- bundleType !== ESM_PROD &&
462
- bundleType !== ESM_DEV ,
463
- env : 'CUSTOM' ,
464
- warning_level : 'QUIET' ,
465
- apply_input_source_maps : false ,
466
- use_types_for_optimization : false ,
467
- process_common_js_modules : false ,
468
- rewrite_polyfills : false ,
469
- inject_libraries : false ,
470
- allow_dynamic_import : true ,
471
-
472
- // Don't let it create global variables in the browser.
473
- // https://github.com/facebook/react/issues/10909
474
- assume_function_wrapper : ! isUMDBundle ,
475
- renaming : ! shouldStayReadable ,
476
- } ) ,
477
- // Add the whitespace back if necessary.
478
- shouldStayReadable &&
479
- prettier ( {
480
- parser : 'flow' ,
481
- singleQuote : false ,
482
- trailingComma : 'none' ,
483
- bracketSpacing : true ,
484
- } ) ,
485
459
// License and haste headers, top-level `if` blocks.
486
460
{
461
+ name : 'license-and-headers' ,
487
462
renderChunk ( source ) {
488
463
return Wrappers . wrapBundle (
489
464
source ,
@@ -495,6 +470,85 @@ function getPlugins(
495
470
) ;
496
471
} ,
497
472
} ,
473
+ // Apply dead code elimination and/or minification.
474
+ // closure doesn't yet support leaving ESM imports intact
475
+ needsMinifiedByClosure &&
476
+ closure (
477
+ {
478
+ compilation_level : 'SIMPLE' ,
479
+ language_in : 'ECMASCRIPT_2020' ,
480
+ language_out :
481
+ bundleType === NODE_ES2015
482
+ ? 'ECMASCRIPT_2020'
483
+ : bundleType === BROWSER_SCRIPT
484
+ ? 'ECMASCRIPT5'
485
+ : 'ECMASCRIPT5_STRICT' ,
486
+ emit_use_strict :
487
+ bundleType !== BROWSER_SCRIPT &&
488
+ bundleType !== ESM_PROD &&
489
+ bundleType !== ESM_DEV ,
490
+ env : 'CUSTOM' ,
491
+ warning_level : 'QUIET' ,
492
+ source_map_include_content : true ,
493
+ use_types_for_optimization : false ,
494
+ process_common_js_modules : false ,
495
+ rewrite_polyfills : false ,
496
+ inject_libraries : false ,
497
+ allow_dynamic_import : true ,
498
+
499
+ // Don't let it create global variables in the browser.
500
+ // https://github.com/facebook/react/issues/10909
501
+ assume_function_wrapper : ! isUMDBundle ,
502
+ renaming : ! shouldStayReadable ,
503
+ } ,
504
+ { needsSourcemaps}
505
+ ) ,
506
+ // Add the whitespace back if necessary.
507
+ shouldStayReadable &&
508
+ prettier ( {
509
+ parser : 'flow' ,
510
+ singleQuote : false ,
511
+ trailingComma : 'none' ,
512
+ bracketSpacing : true ,
513
+ } ) ,
514
+ needsSourcemaps && {
515
+ name : 'generate-prod-bundle-sourcemaps' ,
516
+ async renderChunk ( codeAfterLicense , chunk , options , meta ) {
517
+ // We want to generate a sourcemap that shows the production bundle source
518
+ // as it existed before Closure Compiler minified that chunk, rather than
519
+ // showing the "original" individual source files. This better shows
520
+ // what is actually running in the app.
521
+
522
+ // Use a path like `node_modules/react/cjs/react.production.min.js.map` for the sourcemap file
523
+ const finalSourcemapPath = options . file . replace ( '.js' , '.js.map' ) ;
524
+ const finalSourcemapFilename = path . basename ( finalSourcemapPath ) ;
525
+
526
+ // Read the sourcemap that Closure wrote to disk
527
+ const sourcemapAfterClosure = JSON . parse (
528
+ fs . readFileSync ( finalSourcemapPath , 'utf8' )
529
+ ) ;
530
+
531
+ // CC generated a file list that only contains the tempfile name.
532
+ // Replace that with a more meaningful "source" name for this bundle.
533
+ sourcemapAfterClosure . sources = [ filename ] ;
534
+ sourcemapAfterClosure . file = filename ;
535
+
536
+ // Overwrite the Closure-generated file with the final combined sourcemap
537
+ fs . writeFileSync (
538
+ finalSourcemapPath ,
539
+ JSON . stringify ( sourcemapAfterClosure )
540
+ ) ;
541
+
542
+ // Add the sourcemap URL to the actual bundle, so that tools pick it up
543
+ const sourceWithMappingUrl =
544
+ codeAfterLicense + `\n//# sourceMappingURL=${ finalSourcemapFilename } ` ;
545
+
546
+ return {
547
+ code : sourceWithMappingUrl ,
548
+ map : null ,
549
+ } ;
550
+ } ,
551
+ } ,
498
552
// Record bundle size.
499
553
sizes ( {
500
554
getSize : ( size , gzip ) => {
0 commit comments