6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { dirname , join , normalize , strings , tags } from '@angular-devkit/core' ;
9
+ import { join , normalize , strings , tags } from '@angular-devkit/core' ;
10
10
import {
11
11
Rule ,
12
12
SchematicContext ,
@@ -20,42 +20,11 @@ import {
20
20
noop ,
21
21
url ,
22
22
} from '@angular-devkit/schematics' ;
23
- import { JSONFile } from '../utility/json-file' ;
24
23
import { parseName } from '../utility/parse-name' ;
25
24
import { relativePathToWorkspaceRoot } from '../utility/paths' ;
26
25
import { buildDefaultPath , getWorkspace , updateWorkspace } from '../utility/workspace' ;
27
- import { BrowserBuilderOptions } from '../utility/workspace-models' ;
28
26
import { Schema as WebWorkerOptions } from './schema' ;
29
27
30
- function addConfig ( options : WebWorkerOptions , root : string , tsConfigPath : string ) : Rule {
31
- return ( host : Tree , context : SchematicContext ) => {
32
- context . logger . debug ( 'updating project configuration.' ) ;
33
-
34
- // Add worker glob exclusion to tsconfig.app.json.
35
- // Projects pre version 8 should to have tsconfig.app.json inside their application
36
- const isInSrc = dirname ( normalize ( tsConfigPath ) ) . endsWith ( 'src' ) ;
37
- const workerGlob = `${ isInSrc ? '' : 'src/' } **/*.worker.ts` ;
38
-
39
- try {
40
- const json = new JSONFile ( host , tsConfigPath ) ;
41
- const exclude = json . get ( [ 'exclude' ] ) ;
42
- if ( exclude && Array . isArray ( exclude ) && ! exclude . includes ( workerGlob ) ) {
43
- json . modify ( [ 'exclude' ] , [ ...exclude , workerGlob ] ) ;
44
- }
45
- } catch { }
46
-
47
- return mergeWith (
48
- apply ( url ( './files/worker-tsconfig' ) , [
49
- applyTemplates ( {
50
- ...options ,
51
- relativePathToWorkspaceRoot : relativePathToWorkspaceRoot ( root ) ,
52
- } ) ,
53
- move ( root ) ,
54
- ] ) ,
55
- ) ;
56
- } ;
57
- }
58
-
59
28
function addSnippet ( options : WebWorkerOptions ) : Rule {
60
29
return ( host : Tree , context : SchematicContext ) => {
61
30
context . logger . debug ( 'Updating appmodule' ) ;
@@ -109,63 +78,60 @@ export default function (options: WebWorkerOptions): Rule {
109
78
if ( ! options . project ) {
110
79
throw new SchematicsException ( 'Option "project" is required.' ) ;
111
80
}
112
- if ( ! options . target ) {
113
- throw new SchematicsException ( 'Option "target" is required.' ) ;
114
- }
81
+
115
82
const project = workspace . projects . get ( options . project ) ;
116
83
if ( ! project ) {
117
84
throw new SchematicsException ( `Invalid project name (${ options . project } )` ) ;
118
85
}
86
+
119
87
const projectType = project . extensions [ 'projectType' ] ;
120
88
if ( projectType !== 'application' ) {
121
89
throw new SchematicsException ( `Web Worker requires a project type of "application".` ) ;
122
90
}
123
91
124
- const projectTarget = project . targets . get ( options . target ) ;
125
- if ( ! projectTarget ) {
126
- throw new Error ( `Target is not defined for this project.` ) ;
127
- }
128
- const projectTargetOptions = ( projectTarget . options || { } ) as unknown as BrowserBuilderOptions ;
129
-
130
92
if ( options . path === undefined ) {
131
93
options . path = buildDefaultPath ( project ) ;
132
94
}
133
95
const parsedPath = parseName ( options . path , options . name ) ;
134
96
options . name = parsedPath . name ;
135
97
options . path = parsedPath . path ;
136
- const root = project . root || '' ;
137
98
138
- const needWebWorkerConfig = ! projectTargetOptions . webWorkerTsConfig ;
139
- if ( needWebWorkerConfig ) {
140
- const workerConfigPath = join ( normalize ( root ) , 'tsconfig.worker.json' ) ;
141
- projectTargetOptions . webWorkerTsConfig = workerConfigPath ;
142
- }
143
-
144
- const projectTestTarget = project . targets . get ( 'test' ) ;
145
- if ( projectTestTarget ) {
146
- const projectTestTargetOptions = ( projectTestTarget . options ||
147
- { } ) as unknown as BrowserBuilderOptions ;
148
-
149
- const needWebWorkerConfig = ! projectTestTargetOptions . webWorkerTsConfig ;
150
- if ( needWebWorkerConfig ) {
151
- const workerConfigPath = join ( normalize ( root ) , 'tsconfig.worker.json' ) ;
152
- projectTestTargetOptions . webWorkerTsConfig = workerConfigPath ;
153
- }
154
- }
155
-
156
- const templateSource = apply ( url ( './files/worker' ) , [
99
+ const templateSourceWorkerCode = apply ( url ( './files/worker' ) , [
157
100
applyTemplates ( { ...options , ...strings } ) ,
158
101
move ( parsedPath . path ) ,
159
102
] ) ;
160
103
104
+ const root = project . root || '' ;
105
+ const templateSourceWorkerConfig = apply ( url ( './files/worker-tsconfig' ) , [
106
+ applyTemplates ( {
107
+ ...options ,
108
+ relativePathToWorkspaceRoot : relativePathToWorkspaceRoot ( root ) ,
109
+ } ) ,
110
+ move ( root ) ,
111
+ ] ) ;
112
+
161
113
return chain ( [
162
114
// Add project configuration.
163
- needWebWorkerConfig ? addConfig ( options , root , projectTargetOptions . tsConfig ) : noop ( ) ,
164
- needWebWorkerConfig ? updateWorkspace ( workspace ) : noop ( ) ,
115
+ updateWorkspace ( ( workspace ) => {
116
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
117
+ const project = workspace . projects . get ( options . project ) ! ;
118
+ const buildTarget = project . targets . get ( 'build' ) ;
119
+ const testTarget = project . targets . get ( 'test' ) ;
120
+ if ( ! buildTarget ) {
121
+ throw new Error ( `Build target is not defined for this project.` ) ;
122
+ }
123
+
124
+ const workerConfigPath = join ( normalize ( root ) , 'tsconfig.worker.json' ) ;
125
+ ( buildTarget . options ??= { } ) . webWorkerTsConfig ??= workerConfigPath ;
126
+ if ( testTarget ) {
127
+ ( testTarget . options ??= { } ) . webWorkerTsConfig ??= workerConfigPath ;
128
+ }
129
+ } ) ,
165
130
// Create the worker in a sibling module.
166
131
options . snippet ? addSnippet ( options ) : noop ( ) ,
167
132
// Add the worker.
168
- mergeWith ( templateSource ) ,
133
+ mergeWith ( templateSourceWorkerCode ) ,
134
+ mergeWith ( templateSourceWorkerConfig ) ,
169
135
] ) ;
170
136
} ;
171
137
}
0 commit comments