Skip to content

Commit c47c306

Browse files
authored
refactor[ci/build]: preserve header format in artifacts (#27671)
In order to make Haste work with React's artifacts, It is important to keep headers in this format: ``` /** * ... ... * ... */ ``` For optimization purposes, Closure compiler will actually modify these headers by removing * prefixes, which is expected. We should pass sources to the compiler without license headers, with these changes the current flow will be: 1. Apply top-level definitions. For UMD-bundles, for example, or DEV-only bundles (e. g. `if (__DEV__) { ...`) 2. Apply licence headers for artifacts with sourcemaps: oss-production and oss-profiling bundles, they don't need to preserve the header format to comply with Haste. We need to apply these headers before passing sources to Closure, so it can build correct mappings for sourcemaps. 3. Pass these sources to closure compiler for minification and sourcemaps building. 4. Apply licence headers for artifacts without sourcemaps: dev bundles, fb bundles. This way the header style will be preserved and not changed by Closure.
1 parent 7bdd7cc commit c47c306

File tree

2 files changed

+246
-113
lines changed

2 files changed

+246
-113
lines changed

scripts/rollup/build.js

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,10 @@ function getPlugins(
470470
// I'm going to port "art" to ES modules to avoid this problem.
471471
// Please don't enable this for anything else!
472472
isUMDBundle && entry === 'react-art' && commonjs(),
473-
// License and haste headers, top-level `if` blocks.
474473
{
475-
name: 'license-and-headers',
474+
name: 'top-level-definitions',
476475
renderChunk(source) {
477-
return Wrappers.wrapBundle(
476+
return Wrappers.wrapWithTopLevelDefinitions(
478477
source,
479478
bundleType,
480479
globalName,
@@ -484,6 +483,21 @@ function getPlugins(
484483
);
485484
},
486485
},
486+
// License and haste headers for artifacts with sourcemaps
487+
// For artifacts with sourcemaps we apply these headers
488+
// before passing sources to the Closure compiler, which will be building sourcemaps
489+
needsSourcemaps && {
490+
name: 'license-and-signature-header-for-artifacts-with-sourcemaps',
491+
renderChunk(source) {
492+
return Wrappers.wrapWithLicenseHeader(
493+
source,
494+
bundleType,
495+
globalName,
496+
filename,
497+
moduleType
498+
);
499+
},
500+
},
487501
// Apply dead code elimination and/or minification.
488502
// closure doesn't yet support leaving ESM imports intact
489503
needsMinifiedByClosure &&
@@ -527,7 +541,7 @@ function getPlugins(
527541
}),
528542
needsSourcemaps && {
529543
name: 'generate-prod-bundle-sourcemaps',
530-
async renderChunk(codeAfterLicense, chunk, options, meta) {
544+
async renderChunk(minifiedCodeWithChangedHeader, chunk, options, meta) {
531545
// We want to generate a sourcemap that shows the production bundle source
532546
// as it existed before Closure Compiler minified that chunk, rather than
533547
// showing the "original" individual source files. This better shows
@@ -583,7 +597,7 @@ function getPlugins(
583597

584598
// Add the sourcemap URL to the actual bundle, so that tools pick it up
585599
const sourceWithMappingUrl =
586-
codeAfterLicense +
600+
minifiedCodeWithChangedHeader +
587601
`\n//# sourceMappingURL=${finalSourcemapFilename}`;
588602

589603
return {
@@ -592,6 +606,21 @@ function getPlugins(
592606
};
593607
},
594608
},
609+
// License and haste headers for artifacts without sourcemaps
610+
// Primarily used for FB-artifacts, which should preserve specific format of the header
611+
// Which potentially can be changed by Closure minification
612+
!needsSourcemaps && {
613+
name: 'license-and-signature-header-for-artifacts-without-sourcemaps',
614+
renderChunk(source) {
615+
return Wrappers.wrapWithLicenseHeader(
616+
source,
617+
bundleType,
618+
globalName,
619+
filename,
620+
moduleType
621+
);
622+
},
623+
},
595624
// Record bundle size.
596625
sizes({
597626
getSize: (size, gzip) => {

0 commit comments

Comments
 (0)