5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- import { json } from '@angular-devkit/core' ;
8
+ import { JsonObject , JsonParseMode , JsonValue , parseJson } from '@angular-devkit/core' ;
9
9
import { Rule , Tree , chain , noop } from '@angular-devkit/schematics' ;
10
10
import * as ts from 'typescript' ;
11
11
12
+ function isJsonObject ( value : JsonValue ) : value is JsonObject {
13
+ return value != null && typeof value === 'object' && ! Array . isArray ( value ) ;
14
+ }
12
15
13
16
/**
14
17
* Remove the Reflect import from a polyfill file.
@@ -52,10 +55,10 @@ function _removeReflectFromPolyfills(tree: Tree, path: string) {
52
55
* @param targetObject The target information.
53
56
* @private
54
57
*/
55
- function _updateProjectTarget ( targetObject : json . JsonObject ) : Rule {
58
+ function _updateProjectTarget ( targetObject : JsonObject ) : Rule {
56
59
// Make sure we're using the correct builder.
57
60
if ( targetObject . builder !== '@angular-devkit/build-angular:browser'
58
- || ! json . isJsonObject ( targetObject . options ) ) {
61
+ || ! isJsonObject ( targetObject . options ) ) {
59
62
return noop ( ) ;
60
63
}
61
64
const options = targetObject . options ;
@@ -65,12 +68,12 @@ function _updateProjectTarget(targetObject: json.JsonObject): Rule {
65
68
66
69
const polyfillsToUpdate = [ options . polyfills ] ;
67
70
const configurations = targetObject . configurations ;
68
- if ( json . isJsonObject ( configurations ) ) {
71
+ if ( isJsonObject ( configurations ) ) {
69
72
for ( const configName of Object . keys ( configurations ) ) {
70
73
const config = configurations [ configName ] ;
71
74
72
75
// Just in case, only do non-AOT configurations.
73
- if ( json . isJsonObject ( config )
76
+ if ( isJsonObject ( config )
74
77
&& typeof config . polyfills == 'string'
75
78
&& config . aot !== true ) {
76
79
polyfillsToUpdate . push ( config . polyfills ) ;
@@ -100,31 +103,31 @@ export function polyfillMetadataRule(): Rule {
100
103
return ;
101
104
}
102
105
103
- const angularJson = json . parseJson ( angularConfigContent . toString ( ) , json . JsonParseMode . Loose ) ;
106
+ const angularJson = parseJson ( angularConfigContent . toString ( ) , JsonParseMode . Loose ) ;
104
107
105
- if ( ! json . isJsonObject ( angularJson ) || ! json . isJsonObject ( angularJson . projects ) ) {
108
+ if ( ! isJsonObject ( angularJson ) || ! isJsonObject ( angularJson . projects ) ) {
106
109
// If that field isn't there, no use...
107
110
return ;
108
111
}
109
112
110
113
// For all projects, for all targets, read the polyfill field, and read the environment.
111
114
for ( const projectName of Object . keys ( angularJson . projects ) ) {
112
115
const project = angularJson . projects [ projectName ] ;
113
- if ( ! json . isJsonObject ( project ) ) {
116
+ if ( ! isJsonObject ( project ) ) {
114
117
continue ;
115
118
}
116
119
if ( typeof project . root != 'string' ) {
117
120
continue ;
118
121
}
119
122
120
123
const targets = project . targets || project . architect ;
121
- if ( ! json . isJsonObject ( targets ) ) {
124
+ if ( ! isJsonObject ( targets ) ) {
122
125
continue ;
123
126
}
124
127
125
128
for ( const targetName of Object . keys ( targets ) ) {
126
129
const target = targets [ targetName ] ;
127
- if ( json . isJsonObject ( target ) ) {
130
+ if ( isJsonObject ( target ) ) {
128
131
rules . push ( _updateProjectTarget ( target ) ) ;
129
132
}
130
133
}
0 commit comments