From e1f51eaa55e5f0b2c95cff4ea51e8ae05e15c276 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Wed, 17 Apr 2019 11:43:31 +0100 Subject: [PATCH] feat(compiler-cli): export tooling definitions (#29929) PR Close #29929 --- integration/terser/.gitignore | 1 + integration/terser/package.json | 16 ++++++++++++ integration/terser/test.js | 25 +++++++++++++++++++ packages/compiler-cli/index.ts | 1 + packages/compiler-cli/src/tooling.ts | 22 ++++++++++++++++ packages/core/src/util/ng_dev_mode.ts | 3 +-- .../core/src/util/ng_i18n_closure_mode.ts | 3 +-- 7 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 integration/terser/.gitignore create mode 100644 integration/terser/package.json create mode 100644 integration/terser/test.js create mode 100644 packages/compiler-cli/src/tooling.ts diff --git a/integration/terser/.gitignore b/integration/terser/.gitignore new file mode 100644 index 0000000000..8d10b0cb9a --- /dev/null +++ b/integration/terser/.gitignore @@ -0,0 +1 @@ +core.min.js diff --git a/integration/terser/package.json b/integration/terser/package.json new file mode 100644 index 0000000000..955bea9ca4 --- /dev/null +++ b/integration/terser/package.json @@ -0,0 +1,16 @@ +{ + "name": "terser", + "version": "0.0.0", + "scripts": { + "test": "node test.js" + }, + "private": true, + "dependencies": { + "@angular/core": "file:../../dist/packages-dist/core", + "@angular/compiler": "file:../../dist/packages-dist/compiler", + "@angular/compiler-cli": "file:../../dist/packages-dist/compiler-cli", + "rxjs": "file:../../node_modules/rxjs", + "terser": "3.17.0", + "zone.js": "file:../../node_modules/zone.js" + } +} diff --git a/integration/terser/test.js b/integration/terser/test.js new file mode 100644 index 0000000000..f45ffd83ea --- /dev/null +++ b/integration/terser/test.js @@ -0,0 +1,25 @@ +const readFileSync = require('fs').readFileSync; +const writeFileSync = require('fs').writeFileSync; +const resolve = require('path').resolve; +const Terser = require('terser'); +const GLOBAL_DEFS_FOR_TERSER = require('@angular/compiler-cli').GLOBAL_DEFS_FOR_TERSER; + +const outputPath = resolve(__dirname, './core.min.js'); +const pathToCoreFesm5 = resolve(__dirname, './node_modules/@angular/core/fesm5/core.js'); +const coreFesm5Content = readFileSync(pathToCoreFesm5, 'utf8'); +// Ensure that Terser global_defs exported by compiler-cli work. +const terserOpts = { + compress: { + module: true, + global_defs: GLOBAL_DEFS_FOR_TERSER + } +}; +const result = Terser.minify(coreFesm5Content, terserOpts); +writeFileSync(outputPath, result.code); + +for (const def of Object.keys(GLOBAL_DEFS_FOR_TERSER)) { + if (result.code.includes(def)) { + throw `'${def}' should have been removed from core bundle, but was still there.\n` + + `See output at ${outputPath}.`; + } +} \ No newline at end of file diff --git a/packages/compiler-cli/index.ts b/packages/compiler-cli/index.ts index 444b421eb3..990747d90c 100644 --- a/packages/compiler-cli/index.ts +++ b/packages/compiler-cli/index.ts @@ -17,6 +17,7 @@ export * from './src/transformers/api'; export * from './src/transformers/entry_points'; export * from './src/perform_compile'; +export * from './src/tooling'; // TODO(tbosch): remove this once cli 1.5 is fully released, // and usages in G3 are changed to `CompilerOptions`. diff --git a/packages/compiler-cli/src/tooling.ts b/packages/compiler-cli/src/tooling.ts new file mode 100644 index 0000000000..0bad1cab1a --- /dev/null +++ b/packages/compiler-cli/src/tooling.ts @@ -0,0 +1,22 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +/** + * @module + * @description + * Tooling support helpers. + */ + +/** + * Known values for global variables in `@angular/core` that Terser should set using + * https://github.com/terser-js/terser#conditional-compilation + */ +export const GLOBAL_DEFS_FOR_TERSER = { + ngDevMode: false, + ngI18nClosureMode: false, +}; diff --git a/packages/core/src/util/ng_dev_mode.ts b/packages/core/src/util/ng_dev_mode.ts index 10bc750635..cde6949f0f 100644 --- a/packages/core/src/util/ng_dev_mode.ts +++ b/packages/core/src/util/ng_dev_mode.ts @@ -83,8 +83,7 @@ export function ngDevModeResetPerfCounters(): NgDevModePerfCounters { * set `ngDevMode == false` we should be helping the developer by providing * as much early warning and errors as possible. * - * NOTE: changes to the `ngDevMode` name must be synced with the CLI and - * possibly other third party tooling. Check with them before altering it. + * NOTE: changes to the `ngDevMode` name must be synced with `compiler-cli/src/tooling.ts`. */ if (typeof ngDevMode === 'undefined' || ngDevMode) { ngDevModeResetPerfCounters(); diff --git a/packages/core/src/util/ng_i18n_closure_mode.ts b/packages/core/src/util/ng_i18n_closure_mode.ts index 752e4f24bf..be1231060d 100644 --- a/packages/core/src/util/ng_i18n_closure_mode.ts +++ b/packages/core/src/util/ng_i18n_closure_mode.ts @@ -13,8 +13,7 @@ declare global { } /** - * NOTE: changes to the `ngI18nClosureMode` name must be synced with the CLI and - * possibly other third party tooling. Check with them before altering it. + * NOTE: changes to the `ngI18nClosureMode` name must be synced with `compiler-cli/src/tooling.ts`. */ if (typeof ngI18nClosureMode === 'undefined') { // Make sure to refer to ngI18nClosureMode as ['ngI18nClosureMode'] for closure.