Skip to content

Commit ed5d8be

Browse files
filipesilvaBioPhoton
authored andcommitted
feat(compiler-cli): export tooling definitions (angular#29929)
PR Close angular#29929
1 parent 9b048cc commit ed5d8be

File tree

7 files changed

+67
-4
lines changed

7 files changed

+67
-4
lines changed

integration/terser/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
core.min.js

integration/terser/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "terser",
3+
"version": "0.0.0",
4+
"scripts": {
5+
"test": "node test.js"
6+
},
7+
"private": true,
8+
"dependencies": {
9+
"@angular/core": "file:../../dist/packages-dist/core",
10+
"@angular/compiler": "file:../../dist/packages-dist/compiler",
11+
"@angular/compiler-cli": "file:../../dist/packages-dist/compiler-cli",
12+
"rxjs": "file:../../node_modules/rxjs",
13+
"terser": "3.17.0",
14+
"zone.js": "file:../../node_modules/zone.js"
15+
}
16+
}

integration/terser/test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const readFileSync = require('fs').readFileSync;
2+
const writeFileSync = require('fs').writeFileSync;
3+
const resolve = require('path').resolve;
4+
const Terser = require('terser');
5+
const GLOBAL_DEFS_FOR_TERSER = require('@angular/compiler-cli').GLOBAL_DEFS_FOR_TERSER;
6+
7+
const outputPath = resolve(__dirname, './core.min.js');
8+
const pathToCoreFesm5 = resolve(__dirname, './node_modules/@angular/core/fesm5/core.js');
9+
const coreFesm5Content = readFileSync(pathToCoreFesm5, 'utf8');
10+
// Ensure that Terser global_defs exported by compiler-cli work.
11+
const terserOpts = {
12+
compress: {
13+
module: true,
14+
global_defs: GLOBAL_DEFS_FOR_TERSER
15+
}
16+
};
17+
const result = Terser.minify(coreFesm5Content, terserOpts);
18+
writeFileSync(outputPath, result.code);
19+
20+
for (const def of Object.keys(GLOBAL_DEFS_FOR_TERSER)) {
21+
if (result.code.includes(def)) {
22+
throw `'${def}' should have been removed from core bundle, but was still there.\n` +
23+
`See output at ${outputPath}.`;
24+
}
25+
}

packages/compiler-cli/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export * from './src/transformers/api';
1717
export * from './src/transformers/entry_points';
1818

1919
export * from './src/perform_compile';
20+
export * from './src/tooling';
2021

2122
// TODO(tbosch): remove this once cli 1.5 is fully released,
2223
// and usages in G3 are changed to `CompilerOptions`.

packages/compiler-cli/src/tooling.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/**
10+
* @module
11+
* @description
12+
* Tooling support helpers.
13+
*/
14+
15+
/**
16+
* Known values for global variables in `@angular/core` that Terser should set using
17+
* https://github.com/terser-js/terser#conditional-compilation
18+
*/
19+
export const GLOBAL_DEFS_FOR_TERSER = {
20+
ngDevMode: false,
21+
ngI18nClosureMode: false,
22+
};

packages/core/src/util/ng_dev_mode.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ export function ngDevModeResetPerfCounters(): NgDevModePerfCounters {
8383
* set `ngDevMode == false` we should be helping the developer by providing
8484
* as much early warning and errors as possible.
8585
*
86-
* NOTE: changes to the `ngDevMode` name must be synced with the CLI and
87-
* possibly other third party tooling. Check with them before altering it.
86+
* NOTE: changes to the `ngDevMode` name must be synced with `compiler-cli/src/tooling.ts`.
8887
*/
8988
if (typeof ngDevMode === 'undefined' || ngDevMode) {
9089
ngDevModeResetPerfCounters();

packages/core/src/util/ng_i18n_closure_mode.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ declare global {
1313
}
1414

1515
/**
16-
* NOTE: changes to the `ngI18nClosureMode` name must be synced with the CLI and
17-
* possibly other third party tooling. Check with them before altering it.
16+
* NOTE: changes to the `ngI18nClosureMode` name must be synced with `compiler-cli/src/tooling.ts`.
1817
*/
1918
if (typeof ngI18nClosureMode === 'undefined') {
2019
// Make sure to refer to ngI18nClosureMode as ['ngI18nClosureMode'] for closure.

0 commit comments

Comments
 (0)