mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
chore: add @__PURE__ annotation to ES5 output for uglify (#4147)
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
|
||||
const iifeExpression =
|
||||
new RegExp('^(var (\\S+) = )(\\(function \\(\\) \\{\\n(?: ' +
|
||||
'(?:\\/\\*\\*| \\*|\\*\\/|\\/\\/)[^\\n]*\\n)* function \\2\\([^\\)]*\\) \\{\\n)', 'mg');
|
||||
|
||||
/**
|
||||
* Adds `@__PURE__` annotation comments to IIFEs containing ES5-downleveled classes generated by
|
||||
* TypeScript so that Uglify can tree-shake classes that are not referenced.
|
||||
*
|
||||
* @param fileContent The content of the file for which `@__PURE__` will be added.
|
||||
* @returns The content of the file with `@__PURE__` annotations added.
|
||||
*/
|
||||
export function addPureAnnotations(fileContent: string) {
|
||||
return fileContent
|
||||
// Prefix downleveled classes w/ the @__PURE__ annotation.
|
||||
.replace(iifeExpression, '$1/*@__PURE__*/$3')
|
||||
// Prefix downleveled classes that extend another class w/ the @__PURE__ annotation
|
||||
.replace(
|
||||
/^(var (\S+) = )(\(function \(_super\) \{\n __extends\(\2, _super\);\n)/mg,
|
||||
'$1/*@__PURE__*/$3'
|
||||
);
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
import {
|
||||
DIST_BUNDLES, DIST_ROOT, SOURCE_ROOT, PROJECT_ROOT, LICENSE_BANNER, MATERIAL_VERSION
|
||||
} from '../constants';
|
||||
import {addPureAnnotations} from './annotate-pure';
|
||||
|
||||
// There are no type definitions available for these imports.
|
||||
const uglify = require('uglify-js');
|
||||
@@ -38,6 +39,7 @@ export function composeRelease(packageName: string) {
|
||||
updatePackageVersion(releasePath);
|
||||
createTypingFile(releasePath, packageName);
|
||||
createMetadataFile(releasePath, packageName);
|
||||
addPureAnnotationCommentsToEs5Bundle(releasePath, packageName);
|
||||
}
|
||||
|
||||
/** Builds the bundles for the specified package. */
|
||||
@@ -156,3 +158,12 @@ function inlinePackageMetadataFiles(packagePath: string) {
|
||||
writeFileSync(path , JSON.stringify(metadata), 'utf-8');
|
||||
});
|
||||
}
|
||||
|
||||
/** Adds Uglify "@__PURE__" decorations to the generated ES5 bundle. */
|
||||
function addPureAnnotationCommentsToEs5Bundle(outputDir: string, entryName: string) {
|
||||
const es5BundlePath = join(outputDir, '@angular', `${entryName}.es5.js`);
|
||||
const originalContent = readFileSync(es5BundlePath, 'utf-8');
|
||||
const annotatedContent = addPureAnnotations(originalContent);
|
||||
|
||||
writeFileSync(es5BundlePath, annotatedContent, 'utf-8');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user