7
7
*/
8
8
import { BuilderContext } from '@angular-devkit/architect' ;
9
9
import {
10
- experimental ,
11
10
getSystemPath ,
12
11
logging ,
13
12
normalize ,
14
13
resolve ,
15
- schema ,
16
14
virtualFs ,
17
15
} from '@angular-devkit/core' ;
18
16
import { NodeJsSyncHost } from '@angular-devkit/core/node' ;
@@ -60,9 +58,10 @@ export async function generateWebpackConfig(
60
58
// However this config generation is used by multiple builders such as dev-server
61
59
const scriptTarget = tsConfig . options . target || ts . ScriptTarget . ES5 ;
62
60
const buildBrowserFeatures = new BuildBrowserFeatures ( projectRoot , scriptTarget ) ;
63
- const differentialLoading = context . builder . builderName === 'browser'
64
- && ! options . watch
65
- && buildBrowserFeatures . isDifferentialLoadingNeeded ( ) ;
61
+ const differentialLoading =
62
+ context . builder . builderName === 'browser' &&
63
+ ! options . watch &&
64
+ buildBrowserFeatures . isDifferentialLoadingNeeded ( ) ;
66
65
67
66
const scriptTargets = [ scriptTarget ] ;
68
67
@@ -73,29 +72,32 @@ export async function generateWebpackConfig(
73
72
// For differential loading, we can have several targets
74
73
return scriptTargets . map ( scriptTarget => {
75
74
let buildOptions : NormalizedBrowserBuilderSchema = { ...options } ;
76
- const supportES2015
77
- = scriptTarget !== ts . ScriptTarget . ES3 && scriptTarget !== ts . ScriptTarget . ES5 ;
75
+ const supportES2015 =
76
+ scriptTarget !== ts . ScriptTarget . ES3 && scriptTarget !== ts . ScriptTarget . ES5 ;
78
77
79
78
if ( differentialLoading && fullDifferential ) {
80
79
buildOptions = {
81
80
...options ,
82
- ...(
83
- // FIXME: we do create better webpack config composition to achieve the below
84
- // When DL is enabled and supportES2015 is true it means that we are on the second build
85
- // This also means that we don't need to include styles and assets multiple times
86
- supportES2015
87
- ? { }
88
- : {
81
+ ...// FIXME: we do create better webpack config composition to achieve the below
82
+ // When DL is enabled and supportES2015 is true it means that we are on the second build
83
+ // This also means that we don't need to include styles and assets multiple times
84
+ ( supportES2015
85
+ ? { }
86
+ : {
89
87
styles : options . extractCss ? [ ] : options . styles ,
90
88
assets : [ ] ,
91
- }
92
- ) ,
89
+ } ) ,
93
90
es5BrowserSupport : undefined ,
94
91
esVersionInFileName : true ,
95
92
scriptTargetOverride : scriptTarget ,
96
93
} ;
97
94
} else if ( differentialLoading && ! fullDifferential ) {
98
- buildOptions = { ...options , esVersionInFileName : true , scriptTargetOverride : ts . ScriptTarget . ES5 , es5BrowserSupport : undefined } ;
95
+ buildOptions = {
96
+ ...options ,
97
+ esVersionInFileName : true ,
98
+ scriptTargetOverride : ts . ScriptTarget . ES5 ,
99
+ es5BrowserSupport : undefined ,
100
+ } ;
99
101
}
100
102
101
103
const wco : BrowserWebpackConfigOptions = {
@@ -145,75 +147,48 @@ export async function generateWebpackConfig(
145
147
} ) ;
146
148
}
147
149
148
-
149
- export async function generateBrowserWebpackConfigFromWorkspace (
150
+ export async function generateBrowserWebpackConfigFromContext (
150
151
options : BrowserBuilderSchema ,
151
152
context : BuilderContext ,
152
- projectName : string ,
153
- workspace : experimental . workspace . Workspace ,
154
- host : virtualFs . Host < fs . Stats > ,
155
153
webpackPartialGenerator : ( wco : BrowserWebpackConfigOptions ) => webpack . Configuration [ ] ,
156
- logger : logging . LoggerApi ,
157
- ) : Promise < webpack . Configuration [ ] > {
158
- // TODO: Use a better interface for workspace access.
159
- const projectRoot = resolve ( workspace . root , normalize ( workspace . getProject ( projectName ) . root ) ) ;
160
- const projectSourceRoot = workspace . getProject ( projectName ) . sourceRoot ;
154
+ host : virtualFs . Host < fs . Stats > = new NodeJsSyncHost ( ) ,
155
+ ) : Promise < { config : webpack . Configuration [ ] ; projectRoot : string ; projectSourceRoot ?: string } > {
156
+ const projectName = context . target && context . target . project ;
157
+ if ( ! projectName ) {
158
+ throw new Error ( 'The builder requires a target.' ) ;
159
+ }
160
+
161
+ const workspaceRoot = normalize ( context . workspaceRoot ) ;
162
+ const projectMetadata = await context . getProjectMetadata ( projectName ) ;
163
+ const projectRoot = resolve ( workspaceRoot , normalize ( ( projectMetadata . root as string ) || '' ) ) ;
164
+ const projectSourceRoot = projectMetadata . sourceRoot as string | undefined ;
161
165
const sourceRoot = projectSourceRoot
162
- ? resolve ( workspace . root , normalize ( projectSourceRoot ) )
166
+ ? resolve ( workspaceRoot , normalize ( projectSourceRoot ) )
163
167
: undefined ;
164
168
165
169
const normalizedOptions = normalizeBrowserSchema (
166
170
host ,
167
- workspace . root ,
171
+ workspaceRoot ,
168
172
projectRoot ,
169
173
sourceRoot ,
170
174
options ,
171
175
) ;
172
176
173
- return generateWebpackConfig (
177
+ const config = await generateWebpackConfig (
174
178
context ,
175
- getSystemPath ( workspace . root ) ,
179
+ getSystemPath ( workspaceRoot ) ,
176
180
getSystemPath ( projectRoot ) ,
177
181
sourceRoot && getSystemPath ( sourceRoot ) ,
178
182
normalizedOptions ,
179
183
webpackPartialGenerator ,
180
- logger ,
181
- ) ;
182
- }
183
-
184
-
185
- export async function generateBrowserWebpackConfigFromContext (
186
- options : BrowserBuilderSchema ,
187
- context : BuilderContext ,
188
- webpackPartialGenerator : ( wco : BrowserWebpackConfigOptions ) => webpack . Configuration [ ] ,
189
- host : virtualFs . Host < fs . Stats > = new NodeJsSyncHost ( ) ,
190
- ) : Promise < { workspace : experimental . workspace . Workspace , config : webpack . Configuration [ ] } > {
191
- const registry = new schema . CoreSchemaRegistry ( ) ;
192
- registry . addPostTransform ( schema . transforms . addUndefinedDefaults ) ;
193
-
194
- const workspace = await experimental . workspace . Workspace . fromPath (
195
- host ,
196
- normalize ( context . workspaceRoot ) ,
197
- registry ,
198
- ) ;
199
-
200
- const projectName = context . target ? context . target . project : workspace . getDefaultProjectName ( ) ;
201
-
202
- if ( ! projectName ) {
203
- throw new Error ( 'Must either have a target from the context or a default project.' ) ;
204
- }
205
-
206
- const config = await generateBrowserWebpackConfigFromWorkspace (
207
- options ,
208
- context ,
209
- projectName ,
210
- workspace ,
211
- host ,
212
- webpackPartialGenerator ,
213
184
context . logger ,
214
185
) ;
215
186
216
- return { workspace, config } ;
187
+ return {
188
+ config,
189
+ projectRoot : getSystemPath ( projectRoot ) ,
190
+ projectSourceRoot : sourceRoot && getSystemPath ( sourceRoot ) ,
191
+ } ;
217
192
}
218
193
219
194
export function getIndexOutputFile ( options : BrowserBuilderSchema ) : string {
0 commit comments