66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { dirname , join , normalize , strings , tags } from '@angular-devkit/core' ;
9+ import { join , normalize , strings , tags } from '@angular-devkit/core' ;
1010import {
1111 Rule ,
1212 SchematicContext ,
@@ -20,42 +20,11 @@ import {
2020 noop ,
2121 url ,
2222} from '@angular-devkit/schematics' ;
23- import { JSONFile } from '../utility/json-file' ;
2423import { parseName } from '../utility/parse-name' ;
2524import { relativePathToWorkspaceRoot } from '../utility/paths' ;
2625import { buildDefaultPath , getWorkspace , updateWorkspace } from '../utility/workspace' ;
27- import { BrowserBuilderOptions } from '../utility/workspace-models' ;
2826import { Schema as WebWorkerOptions } from './schema' ;
2927
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-
5928function addSnippet ( options : WebWorkerOptions ) : Rule {
6029 return ( host : Tree , context : SchematicContext ) => {
6130 context . logger . debug ( 'Updating appmodule' ) ;
@@ -109,63 +78,60 @@ export default function (options: WebWorkerOptions): Rule {
10978 if ( ! options . project ) {
11079 throw new SchematicsException ( 'Option "project" is required.' ) ;
11180 }
112- if ( ! options . target ) {
113- throw new SchematicsException ( 'Option "target" is required.' ) ;
114- }
81+
11582 const project = workspace . projects . get ( options . project ) ;
11683 if ( ! project ) {
11784 throw new SchematicsException ( `Invalid project name (${ options . project } )` ) ;
11885 }
86+
11987 const projectType = project . extensions [ 'projectType' ] ;
12088 if ( projectType !== 'application' ) {
12189 throw new SchematicsException ( `Web Worker requires a project type of "application".` ) ;
12290 }
12391
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-
13092 if ( options . path === undefined ) {
13193 options . path = buildDefaultPath ( project ) ;
13294 }
13395 const parsedPath = parseName ( options . path , options . name ) ;
13496 options . name = parsedPath . name ;
13597 options . path = parsedPath . path ;
136- const root = project . root || '' ;
13798
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' ) , [
157100 applyTemplates ( { ...options , ...strings } ) ,
158101 move ( parsedPath . path ) ,
159102 ] ) ;
160103
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+
161113 return chain ( [
162114 // 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+ } ) ,
165130 // Create the worker in a sibling module.
166131 options . snippet ? addSnippet ( options ) : noop ( ) ,
167132 // Add the worker.
168- mergeWith ( templateSource ) ,
133+ mergeWith ( templateSourceWorkerCode ) ,
134+ mergeWith ( templateSourceWorkerConfig ) ,
169135 ] ) ;
170136 } ;
171137}
0 commit comments