55 * Use of this source code is governed by an MIT-style license that can be
66 * found in the LICENSE file at https://angular.io/license
77 */
8- import { BuilderOutput , createBuilder } from '@angular-devkit/architect' ;
8+ import { BuilderContext , BuilderOutput , createBuilder } from '@angular-devkit/architect' ;
99import { runWebpack } from '@angular-devkit/build-webpack' ;
10- import {
11- Path ,
12- experimental ,
13- getSystemPath ,
14- json ,
15- logging ,
16- normalize ,
17- resolve , schema , virtualFs ,
18- } from '@angular-devkit/core' ;
10+ import { json , normalize } from '@angular-devkit/core' ;
1911import { NodeJsSyncHost } from '@angular-devkit/core/node' ;
20- import { Stats } from 'fs' ;
2112import * as path from 'path' ;
22- import { from , of } from 'rxjs' ;
13+ import { Observable , from , of } from 'rxjs' ;
2314import { concatMap , map } from 'rxjs/operators' ;
24- import * as ts from 'typescript' ; // tslint:disable-line:no-implicit-dependencies
15+ import * as webpack from 'webpack' ;
2516import { WebpackConfigOptions } from '../angular-cli-files/models/build-options' ;
2617import {
2718 getAotConfig ,
@@ -31,66 +22,28 @@ import {
3122 getStatsConfig ,
3223 getStylesConfig ,
3324} from '../angular-cli-files/models/webpack-configs' ;
34- import { readTsconfig } from '../angular-cli-files/utilities/read-tsconfig' ;
35- import { requireProjectModule } from '../angular-cli-files/utilities/require-project-module' ;
36- import {
37- NormalizedWebpackServerBuilderSchema , defaultProgress ,
38- deleteOutputDir ,
39- normalizeWebpackServerSchema ,
40- } from '../utils' ;
41- import { Schema as BuildWebpackServerSchema } from './schema' ;
42- const webpackMerge = require ( 'webpack-merge' ) ;
43-
25+ import { ExecutionTransformer } from '../transforms' ;
26+ import { NormalizedBrowserBuilderSchema , deleteOutputDir } from '../utils' ;
27+ import { generateBrowserWebpackConfigFromContext } from '../utils/webpack-browser-config' ;
28+ import { Schema as BuildWebpackServerOptions } from './schema' ;
4429
4530// If success is true, outputPath should be set.
4631export type ServerBuilderOutput = json . JsonObject & BuilderOutput & {
4732 outputPath ?: string ;
4833} ;
4934
50-
51- export default createBuilder <
52- json . JsonObject & BuildWebpackServerSchema ,
53- ServerBuilderOutput
54- > ( ( options , context ) => {
55- const host = new virtualFs . AliasHost ( new NodeJsSyncHost ( ) ) ;
35+ export function execute (
36+ options : BuildWebpackServerOptions ,
37+ context : BuilderContext ,
38+ transforms : {
39+ webpackConfiguration ?: ExecutionTransformer < webpack . Configuration > ;
40+ } = { } ,
41+ ) : Observable < ServerBuilderOutput > {
42+ const host = new NodeJsSyncHost ( ) ;
5643 const root = context . workspaceRoot ;
5744
58- async function setup ( ) {
59- const registry = new schema . CoreSchemaRegistry ( ) ;
60- registry . addPostTransform ( schema . transforms . addUndefinedDefaults ) ;
61-
62- const workspace = await experimental . workspace . Workspace . fromPath (
63- host ,
64- normalize ( context . workspaceRoot ) ,
65- registry ,
66- ) ;
67- const projectName = context . target ? context . target . project : workspace . getDefaultProjectName ( ) ;
68-
69- if ( ! projectName ) {
70- throw new Error ( 'Must either have a target from the context or a default project.' ) ;
71- }
72- const projectRoot = resolve (
73- workspace . root ,
74- normalize ( workspace . getProject ( projectName ) . root ) ,
75- ) ;
76- const workspaceSourceRoot = workspace . getProject ( projectName ) . sourceRoot ;
77- const sourceRoot = workspaceSourceRoot !== undefined ? resolve (
78- workspace . root ,
79- normalize ( workspaceSourceRoot ) ,
80- ) : undefined ;
81-
82- const normalizedOptions = normalizeWebpackServerSchema (
83- host ,
84- normalize ( root ) ,
85- projectRoot ,
86- sourceRoot ,
87- options ,
88- ) ;
89-
90- return { normalizedOptions, projectRoot } ;
91- }
92-
93- return from ( setup ( ) ) . pipe (
45+ return from ( buildServerWebpackConfig ( options , context ) ) . pipe (
46+ concatMap ( async v => transforms . webpackConfiguration ? transforms . webpackConfiguration ( v ) : v ) ,
9447 concatMap ( v => {
9548 if ( options . deleteOutputPath ) {
9649 return deleteOutputDir ( normalize ( root ) , normalize ( options . outputPath ) , host ) . pipe (
@@ -100,17 +53,7 @@ export default createBuilder<
10053 return of ( v ) ;
10154 }
10255 } ) ,
103- concatMap ( ( { normalizedOptions, projectRoot } ) => {
104- const webpackConfig = buildServerWebpackConfig (
105- normalize ( root ) ,
106- projectRoot ,
107- host ,
108- normalizedOptions ,
109- context . logger . createChild ( 'webpack' ) ,
110- ) ;
111-
112- return runWebpack ( webpackConfig , context ) ;
113- } ) ,
56+ concatMap ( webpackConfig => runWebpack ( webpackConfig , context ) ) ,
11457 map ( output => {
11558 if ( output . success === false ) {
11659 return output as ServerBuilderOutput ;
@@ -122,65 +65,41 @@ export default createBuilder<
12265 } as ServerBuilderOutput ;
12366 } ) ,
12467 ) ;
125- } ) ;
126-
127- export function buildServerWebpackConfig (
128- root : Path ,
129- projectRoot : Path ,
130- _host : virtualFs . Host < Stats > ,
131- options : NormalizedWebpackServerBuilderSchema ,
132- logger : logging . Logger ,
133- ) {
134- let wco : WebpackConfigOptions ;
135-
136- // TODO: make target defaults into configurations instead
137- // options = this.addTargetDefaults(options);
138-
139- const tsConfigPath = getSystemPath ( normalize ( resolve ( root , normalize ( options . tsConfig ) ) ) ) ;
140- const tsConfig = readTsconfig ( tsConfigPath ) ;
68+ }
14169
142- const projectTs = requireProjectModule ( getSystemPath ( projectRoot ) , 'typescript' ) as typeof ts ;
70+ export default createBuilder < json . JsonObject & BuildWebpackServerOptions , ServerBuilderOutput > (
71+ execute ,
72+ ) ;
14373
144- const supportES2015 = tsConfig . options . target !== projectTs . ScriptTarget . ES3
145- && tsConfig . options . target !== projectTs . ScriptTarget . ES5 ;
74+ function getCompilerConfig ( wco : WebpackConfigOptions ) {
75+ if ( wco . buildOptions . main || wco . buildOptions . polyfills ) {
76+ return wco . buildOptions . aot ? getAotConfig ( wco ) : getNonAotConfig ( wco ) ;
77+ }
14678
147- const buildOptions : typeof wco [ 'buildOptions' ] = {
148- ...options as { } as typeof wco [ 'buildOptions' ] ,
149- } ;
79+ return { } ;
80+ }
15081
151- wco = {
152- root : getSystemPath ( root ) ,
153- projectRoot : getSystemPath ( projectRoot ) ,
154- // TODO: use only this.options, it contains all flags and configs items already.
155- buildOptions : {
156- ...buildOptions ,
82+ async function buildServerWebpackConfig (
83+ options : BuildWebpackServerOptions ,
84+ context : BuilderContext ,
85+ ) {
86+ const { config } = await generateBrowserWebpackConfigFromContext (
87+ {
88+ ...options ,
89+ index : '' ,
15790 buildOptimizer : false ,
15891 aot : true ,
15992 platform : 'server' ,
160- scripts : [ ] ,
161- styles : [ ] ,
162- } ,
163- tsConfig,
164- tsConfigPath,
165- supportES2015,
166- logger,
167- } ;
168-
169- wco . buildOptions . progress = defaultProgress ( wco . buildOptions . progress ) ;
170-
171- const webpackConfigs : { } [ ] = [
172- getCommonConfig ( wco ) ,
173- getServerConfig ( wco ) ,
174- getStylesConfig ( wco ) ,
175- getStatsConfig ( wco ) ,
176- ] ;
177-
178- if ( wco . buildOptions . main || wco . buildOptions . polyfills ) {
179- const typescriptConfigPartial = wco . buildOptions . aot
180- ? getAotConfig ( wco )
181- : getNonAotConfig ( wco ) ;
182- webpackConfigs . push ( typescriptConfigPartial ) ;
183- }
93+ } as NormalizedBrowserBuilderSchema ,
94+ context ,
95+ wco => [
96+ getCommonConfig ( wco ) ,
97+ getServerConfig ( wco ) ,
98+ getStylesConfig ( wco ) ,
99+ getStatsConfig ( wco ) ,
100+ getCompilerConfig ( wco ) ,
101+ ] ,
102+ ) ;
184103
185- return webpackMerge ( webpackConfigs ) ;
104+ return config [ 0 ] ;
186105}
0 commit comments