Skip to content

chore: add @__PURE__ annotation to ES5 output for uglify #4147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tools/gulp/util/annotate-pure.ts
Original file line number Diff line number Diff line change
@@ -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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move that RegExp to a constant as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only moved the first one because it's over 100 characters long and needed to be wrapped in a string.

Copy link
Member

@devversion devversion Apr 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine by me. Just think that it would be more clean if you have it like a stream (just running transformers)

But since its mostly temporary.. doesn't matter.

);
}
11 changes: 11 additions & 0 deletions tools/gulp/util/package-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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');
}