-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
devserver.config.ts
137 lines (122 loc) · 3.98 KB
/
devserver.config.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
131
132
133
134
135
136
137
import { logger } from '@nrwl/devkit';
import type { Configuration as WebpackConfiguration } from 'webpack';
import type { Configuration as WebpackDevServerConfiguration } from 'webpack-dev-server';
import * as path from 'path';
import { basename, resolve } from 'path';
import { getWebConfig } from './web.config';
import { WebWebpackExecutorOptions } from '../executors/webpack/webpack.impl';
import { WebDevServerOptions } from '../executors/dev-server/dev-server.impl';
import { buildServePath } from './serve-path';
import { OptimizationOptions } from './shared-models';
import { readFileSync } from 'fs-extra';
import { IndexHtmlWebpackPlugin } from './/webpack/plugins/index-html-webpack-plugin';
import { generateEntryPoints } from './webpack/package-chunk-sort';
export function getDevServerConfig(
workspaceRoot: string,
projectRoot: string,
sourceRoot: string,
buildOptions: WebWebpackExecutorOptions,
serveOptions: WebDevServerOptions
): Partial<WebpackConfiguration> {
const webpackConfig = getWebConfig(
workspaceRoot,
projectRoot,
sourceRoot,
buildOptions,
true,
typeof buildOptions.optimization === 'boolean'
? buildOptions.optimization
: buildOptions.optimization?.scripts
);
(webpackConfig as any).devServer = getDevServerPartial(
workspaceRoot,
serveOptions,
buildOptions
);
const {
deployUrl,
subresourceIntegrity,
scripts = [],
styles = [],
index,
baseHref,
} = buildOptions;
webpackConfig.plugins.push(
new IndexHtmlWebpackPlugin({
indexPath: resolve(workspaceRoot, index),
outputPath: basename(index),
baseHref,
entrypoints: generateEntryPoints({ scripts, styles }),
deployUrl,
sri: subresourceIntegrity,
moduleEntrypoints: [],
noModuleEntrypoints: ['polyfills-es5'],
})
);
return webpackConfig as WebpackConfiguration;
}
function getDevServerPartial(
root: string,
options: WebDevServerOptions,
buildOptions: WebWebpackExecutorOptions
): WebpackDevServerConfiguration {
const servePath = buildServePath(buildOptions);
const { scripts: scriptsOptimization, styles: stylesOptimization } =
(buildOptions.optimization || {}) as OptimizationOptions;
const config: WebpackDevServerConfiguration = {
host: options.host,
port: options.port,
headers: { 'Access-Control-Allow-Origin': '*' },
historyApiFallback: {
index: `${servePath}${path.basename(buildOptions.index)}`,
disableDotRule: true,
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
},
onListening(server: any) {
logger.info(
`NX Web Development Server is listening at ${
server.options.https ? 'https' : 'http'
}://${server.options.host}:${server.options.port}${buildServePath(
buildOptions
)}`
);
},
open: options.open,
static: false,
compress: scriptsOptimization || stylesOptimization,
https: options.ssl,
devMiddleware: {
publicPath: servePath,
stats: false,
},
client: {
webSocketURL: options.publicHost,
overlay: {
errors: !(scriptsOptimization || stylesOptimization),
warnings: false,
},
},
liveReload: options.hmr ? false : options.liveReload, // disable liveReload if hmr is enabled
hot: options.hmr,
};
if (options.ssl && options.sslKey && options.sslCert) {
config.https = getSslConfig(root, options);
}
if (options.proxyConfig) {
config.proxy = getProxyConfig(root, options);
}
if (options.allowedHosts) {
config.allowedHosts = options.allowedHosts.split(',');
}
return config;
}
function getSslConfig(root: string, options: WebDevServerOptions) {
return {
key: readFileSync(path.resolve(root, options.sslKey), 'utf-8'),
cert: readFileSync(path.resolve(root, options.sslCert), 'utf-8'),
};
}
function getProxyConfig(root: string, options: WebDevServerOptions) {
const proxyPath = path.resolve(root, options.proxyConfig as string);
return require(proxyPath);
}