-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
/
Copy pathwebpack.ts
179 lines (167 loc) · 4.78 KB
/
webpack.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import path from 'path';
import merge from 'webpack-merge';
import webpack from 'webpack';
import logger from '@docusaurus/logger';
import WebpackDevServer from 'webpack-dev-server';
import evalSourceMapMiddleware from 'react-dev-utils/evalSourceMapMiddleware';
import {createPollingOptions} from './watcher';
import {
executePluginsConfigurePostCss,
executePluginsConfigureWebpack,
formatStatsErrorMessage,
getHttpsConfig,
printStatsWarnings,
} from '../../webpack/utils';
import {createStartClientConfig} from '../../webpack/client';
import type {StartCLIOptions} from './start';
import type {Props} from '@docusaurus/types';
import type {Compiler} from 'webpack';
import type {OpenUrlContext} from './utils';
// E2E_TEST=true docusaurus start
// Makes "docusaurus start" exit immediately on success/error, for E2E test
function registerWebpackE2ETestHook(compiler: Compiler) {
compiler.hooks.done.tap('done', (stats) => {
const errorsWarnings = stats.toJson('errors-warnings');
const statsErrorMessage = formatStatsErrorMessage(errorsWarnings);
if (statsErrorMessage) {
console.error(statsErrorMessage);
}
printStatsWarnings(errorsWarnings);
if (process.env.E2E_TEST) {
if (stats.hasErrors()) {
logger.error('E2E_TEST: Project has compiler errors.');
process.exit(1);
}
logger.success('E2E_TEST: Project can compile.');
process.exit(0);
}
});
}
async function createDevServerConfig({
cliOptions,
props,
host,
port,
}: {
cliOptions: StartCLIOptions;
props: Props;
host: string;
port: number;
}): Promise<WebpackDevServer.Configuration> {
const {baseUrl, siteDir, siteConfig} = props;
const pollingOptions = createPollingOptions(cliOptions);
const httpsConfig = await getHttpsConfig();
// https://webpack.js.org/configuration/dev-server
return {
hot: cliOptions.hotOnly ? 'only' : true,
liveReload: false,
client: {
progress: true,
overlay: {
warnings: false,
errors: true,
},
webSocketURL: {
hostname: '0.0.0.0',
port: 0,
},
},
headers: {
'access-control-allow-origin': '*',
},
devMiddleware: {
publicPath: baseUrl,
// Reduce log verbosity, see https://github.com/facebook/docusaurus/pull/5420#issuecomment-906613105
stats: 'summary',
},
static: siteConfig.staticDirectories.map((dir) => ({
publicPath: baseUrl,
directory: path.resolve(siteDir, dir),
watch: {
// Useful options for our own monorepo using symlinks!
// See https://github.com/webpack/webpack/issues/11612#issuecomment-879259806
followSymlinks: true,
ignored: /node_modules\/(?!@docusaurus)/,
...{pollingOptions},
},
})),
...(httpsConfig && {
server:
typeof httpsConfig === 'object'
? {
type: 'https',
options: httpsConfig,
}
: 'https',
}),
historyApiFallback: {
rewrites: [{from: /\/*/, to: baseUrl}],
},
allowedHosts: 'all',
host,
port,
setupMiddlewares: (middlewares, devServer) => {
// This lets us fetch source contents from webpack for the error overlay.
middlewares.unshift(evalSourceMapMiddleware(devServer));
return middlewares;
},
};
}
async function getStartClientConfig({
props,
minify,
poll,
}: {
props: Props;
minify: boolean;
poll: number | boolean | undefined;
}) {
const {plugins, siteConfig} = props;
let {clientConfig: config} = await createStartClientConfig({
props,
minify,
poll,
});
config = executePluginsConfigurePostCss({plugins, config});
config = executePluginsConfigureWebpack({
plugins,
config,
isServer: false,
jsLoader: siteConfig.webpack?.jsLoader,
});
return config;
}
export async function createWebpackDevServer({
props,
cliOptions,
openUrlContext,
}: {
props: Props;
cliOptions: StartCLIOptions;
openUrlContext: OpenUrlContext;
}): Promise<WebpackDevServer> {
const config = await getStartClientConfig({
props,
minify: cliOptions.minify ?? true,
poll: cliOptions.poll,
});
const compiler = webpack(config);
registerWebpackE2ETestHook(compiler);
const defaultDevServerConfig = await createDevServerConfig({
cliOptions,
props,
host: openUrlContext.host,
port: openUrlContext.port,
});
// Allow plugin authors to customize/override devServer config
const devServerConfig: WebpackDevServer.Configuration = merge(
[defaultDevServerConfig, config.devServer].filter(Boolean),
);
return new WebpackDevServer(devServerConfig, compiler);
}