-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathindex.ts
130 lines (110 loc) · 4.17 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { BuilderContext, createBuilder } from '@angular-devkit/architect';
import { resolve as pathResolve } from 'path';
import { Observable, from, isObservable, of, switchMap } from 'rxjs';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import { getEmittedFiles, getWebpackConfig } from '../utils';
import { BuildResult, WebpackFactory, WebpackLoggingCallback } from '../webpack';
import { Schema as WebpackDevServerBuilderSchema } from './schema';
export type WebpackDevServerFactory = typeof WebpackDevServer;
export type DevServerBuildOutput = BuildResult & {
port: number;
family: string;
address: string;
};
export function runWebpackDevServer(
config: webpack.Configuration,
context: BuilderContext,
options: {
shouldProvideStats?: boolean;
devServerConfig?: WebpackDevServer.Configuration;
logging?: WebpackLoggingCallback;
webpackFactory?: WebpackFactory;
webpackDevServerFactory?: WebpackDevServerFactory;
} = {},
): Observable<DevServerBuildOutput> {
const createWebpack = (c: webpack.Configuration) => {
if (options.webpackFactory) {
const result = options.webpackFactory(c);
if (isObservable(result)) {
return result;
} else {
return of(result);
}
} else {
return of(webpack(c));
}
};
const createWebpackDevServer = (
webpack: webpack.Compiler | webpack.MultiCompiler,
config: WebpackDevServer.Configuration,
) => {
if (options.webpackDevServerFactory) {
return new options.webpackDevServerFactory(config, webpack);
}
return new WebpackDevServer(config, webpack);
};
const log: WebpackLoggingCallback =
options.logging || ((stats, config) => context.logger.info(stats.toString(config.stats)));
const shouldProvideStats = options.shouldProvideStats ?? true;
return createWebpack({ ...config, watch: false }).pipe(
switchMap(
(webpackCompiler) =>
new Observable<DevServerBuildOutput>((obs) => {
const devServerConfig = options.devServerConfig || config.devServer || {};
devServerConfig.host ??= 'localhost';
let result: Partial<DevServerBuildOutput>;
const statsOptions = typeof config.stats === 'boolean' ? undefined : config.stats;
webpackCompiler.hooks.done.tap('build-webpack', (stats) => {
// Log stats.
log(stats, config);
obs.next({
...result,
webpackStats: shouldProvideStats ? stats.toJson(statsOptions) : undefined,
emittedFiles: getEmittedFiles(stats.compilation),
success: !stats.hasErrors(),
outputPath: stats.compilation.outputOptions.path,
} as unknown as DevServerBuildOutput);
});
const devServer = createWebpackDevServer(webpackCompiler, devServerConfig);
devServer.startCallback((err) => {
if (err) {
obs.error(err);
return;
}
const address = devServer.server?.address();
if (!address) {
obs.error(new Error(`Dev-server address info is not defined.`));
return;
}
result = {
success: true,
port: typeof address === 'string' ? 0 : address.port,
family: typeof address === 'string' ? '' : address.family,
address: typeof address === 'string' ? address : address.address,
};
});
// Teardown logic. Close the server when unsubscribed from.
return () => {
devServer.stopCallback(() => {});
webpackCompiler.close(() => {});
};
}),
),
);
}
export default createBuilder<WebpackDevServerBuilderSchema, DevServerBuildOutput>(
(options, context) => {
const configPath = pathResolve(context.workspaceRoot, options.webpackConfig);
return from(getWebpackConfig(configPath)).pipe(
switchMap((config) => runWebpackDevServer(config, context)),
);
},
);