From 2320532516a113d39a1e1970d88c587156cbb914 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Tue, 1 Aug 2017 21:48:08 +0200 Subject: [PATCH] build: no longer add pure annotations to es5 bundles (#6107) * The pure annotations are no longer added to ES5 bundles of Angular Material or the CDK. The pure annotations should be added by the new build optimizer which is part of the CLI. --- tools/package-tools/build-release.ts | 3 -- tools/package-tools/copy-files.ts | 2 +- .../package-tools/entry-point-package-json.ts | 2 +- tools/package-tools/metadata-reexport.ts | 16 +++++---- tools/package-tools/pure-annotations.ts | 33 ------------------- 5 files changed, 11 insertions(+), 45 deletions(-) delete mode 100644 tools/package-tools/pure-annotations.ts diff --git a/tools/package-tools/build-release.ts b/tools/package-tools/build-release.ts index 09c9d718cd3..93a1dde89cf 100644 --- a/tools/package-tools/build-release.ts +++ b/tools/package-tools/build-release.ts @@ -1,7 +1,6 @@ import {join} from 'path'; import {mkdirpSync} from 'fs-extra'; import {copyFiles} from './copy-files'; -import {addPureAnnotationsToFile} from './pure-annotations'; import {replaceVersionPlaceholders} from './version-placeholders'; import {inlinePackageMetadataFiles} from './metadata-inlining'; import {createTypingsReexportFile} from './typings-reexport'; @@ -43,8 +42,6 @@ export function composeRelease(packageName: string, options: ComposeReleaseOptio if (options.useSecondaryEntryPoints) { createFilesForSecondaryEntryPoint(packageName, packagePath, releasePath); } - - addPureAnnotationsToFile(join(releasePath, '@angular', `${packageName}.es5.js`)); } /** Creates files necessary for a secondary entry-point. */ diff --git a/tools/package-tools/copy-files.ts b/tools/package-tools/copy-files.ts index 48412d3ef9b..c9baf5fe02b 100644 --- a/tools/package-tools/copy-files.ts +++ b/tools/package-tools/copy-files.ts @@ -5,7 +5,7 @@ import {join, dirname} from 'path'; /** Function to copy files that match a glob to another directory. */ export function copyFiles(fromPath: string, fileGlob: string, outDir: string) { glob(fileGlob, {cwd: fromPath}).forEach(filePath => { - let fileDestPath = join(outDir, filePath); + const fileDestPath = join(outDir, filePath); mkdirpSync(dirname(fileDestPath)); copySync(join(fromPath, filePath), fileDestPath); }); diff --git a/tools/package-tools/entry-point-package-json.ts b/tools/package-tools/entry-point-package-json.ts index 60026c172f1..f804be61719 100644 --- a/tools/package-tools/entry-point-package-json.ts +++ b/tools/package-tools/entry-point-package-json.ts @@ -12,5 +12,5 @@ export function createEntryPointPackageJson(destDir: string, packageName: string es2015: `../@angular/${packageName}/${entryPointName}.js`, }; - writeFileSync(join(destDir, 'package.json'), JSON.stringify(content), 'utf-8'); + writeFileSync(join(destDir, 'package.json'), JSON.stringify(content, null, 2), 'utf-8'); } diff --git a/tools/package-tools/metadata-reexport.ts b/tools/package-tools/metadata-reexport.ts index 60836016bbd..cd79e86c6ca 100644 --- a/tools/package-tools/metadata-reexport.ts +++ b/tools/package-tools/metadata-reexport.ts @@ -3,11 +3,13 @@ import {join} from 'path'; /** Creates a metadata file that re-exports the metadata bundle inside of the typings. */ export function createMetadataReexportFile(destDir: string, from: string, name: string) { - const metadataReExport = `{ - "__symbolic":"module", - "version":3,"metadata":{}, - "exports":[{"from":"${from}"}], - "flatModuleIndexRedirect": true - }`; - writeFileSync(join(destDir, `${name}.metadata.json`), metadataReExport, 'utf-8'); + const metadataJsonContent = JSON.stringify({ + __symbolic: 'module', + version: 3, + metadata: {}, + exports: [{from}], + flatModuleIndexRedirect: true + }, null, 2); + + writeFileSync(join(destDir, `${name}.metadata.json`), metadataJsonContent, 'utf-8'); } diff --git a/tools/package-tools/pure-annotations.ts b/tools/package-tools/pure-annotations.ts deleted file mode 100644 index 9fa971e4c01..00000000000 --- a/tools/package-tools/pure-annotations.ts +++ /dev/null @@ -1,33 +0,0 @@ -import {readFileSync, writeFileSync} from 'fs'; - -/** Regex that matches downleveled class IIFE expressions. Used to add the pure annotations. */ -const classIfeeRegex = - new RegExp('^(var (\\S+) = )(\\(function \\(\\) \\{[\\n\\r]*(?: (?:\\/\\*\\*| \\*|\\*\\/|' + - '\\/\\/)[^\\n\\r]*[\\n\\r]*)* function \\2\\([^\\)]*\\) \\{[\\n\\r]*)', 'mg'); - -/** Regex that matches downleveled class IIFE expressions with _extends statements */ -const classExtendsIfeeRegex = - /^(var (\S+) = )(\(function \(_super\) {[\n\r]* tslib.*\.__extends\(\2, _super\);[\n\r]*)/gm; - -/** - * 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(classIfeeRegex, '$1/*@__PURE__*/$3') - // Prefix downleveled classes that extend another class w/ the @__PURE__ annotation - .replace(classExtendsIfeeRegex, '$1/*@__PURE__*/$3'); -} - -/** Adds Uglify "@__PURE__" decorations to the specified file. */ -export function addPureAnnotationsToFile(inputFile: string) { - const originalContent = readFileSync(inputFile, 'utf-8'); - const annotatedContent = addPureAnnotations(originalContent); - - writeFileSync(inputFile, annotatedContent, 'utf-8'); -}