Skip to content

Commit b34fb8b

Browse files
committed
refactor[ci/build]: preserve header format in artifacts
1 parent 52d542a commit b34fb8b

File tree

2 files changed

+270
-119
lines changed

2 files changed

+270
-119
lines changed

scripts/rollup/build.js

Lines changed: 52 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,19 @@ function getPlugins(
484483
);
485484
},
486485
},
486+
// License and haste headers
487+
{
488+
name: 'license-and-signature-header',
489+
renderChunk(source) {
490+
return Wrappers.wrapWithLicenseHeader(
491+
source,
492+
bundleType,
493+
globalName,
494+
filename,
495+
moduleType
496+
);
497+
},
498+
},
487499
// Apply dead code elimination and/or minification.
488500
// closure doesn't yet support leaving ESM imports intact
489501
needsMinifiedByClosure &&
@@ -527,7 +539,7 @@ function getPlugins(
527539
}),
528540
needsSourcemaps && {
529541
name: 'generate-prod-bundle-sourcemaps',
530-
async renderChunk(codeAfterLicense, chunk, options, meta) {
542+
async renderChunk(minifiedCodeWithChangedHeader, chunk, options, meta) {
531543
// We want to generate a sourcemap that shows the production bundle source
532544
// as it existed before Closure Compiler minified that chunk, rather than
533545
// showing the "original" individual source files. This better shows
@@ -583,7 +595,7 @@ function getPlugins(
583595

584596
// Add the sourcemap URL to the actual bundle, so that tools pick it up
585597
const sourceWithMappingUrl =
586-
codeAfterLicense +
598+
minifiedCodeWithChangedHeader +
587599
`\n//# sourceMappingURL=${finalSourcemapFilename}`;
588600

589601
return {
@@ -592,6 +604,41 @@ function getPlugins(
592604
};
593605
},
594606
},
607+
// After minification with Closure, the header can be changed.
608+
needsMinifiedByClosure && {
609+
name: 'format-header',
610+
renderChunk(source) {
611+
if (source.startsWith('/**')) {
612+
return;
613+
}
614+
615+
// Update the header to follow this format:
616+
// /**
617+
// *
618+
// * ...
619+
// */
620+
// We can shift the lines to the right, but don't add / remove lines
621+
// It can break mappings in sourcemaps
622+
const lines = source.split(/\r\n|\r|\n/);
623+
for (let lineIndex = 0; lineIndex < lines.length; ++lineIndex) {
624+
const currentLine = lines[lineIndex];
625+
626+
if (currentLine === '*/') {
627+
lines[lineIndex] = ' */';
628+
break;
629+
}
630+
631+
if (currentLine === '/*') {
632+
lines[lineIndex] = '/**';
633+
continue;
634+
}
635+
636+
lines[lineIndex] = ` *${currentLine}`;
637+
}
638+
639+
return lines.join('\r\n');
640+
},
641+
},
595642
// Record bundle size.
596643
sizes({
597644
getSize: (size, gzip) => {

0 commit comments

Comments
 (0)