Skip to content

Commit b443f4d

Browse files
fix: support devServer: false
1 parent fdd8801 commit b443f4d

11 files changed

+282
-21
lines changed

src/middleware.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,9 @@ function wrapper(context) {
519519
headers = allHeaders;
520520
}
521521

522-
headers.forEach((header) => {
523-
setResponseHeader(res, header.key, header.value);
524-
});
522+
for (const { key, value } of headers) {
523+
setResponseHeader(res, key, value);
524+
}
525525
}
526526

527527
if (

src/utils/getPaths.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ function getPaths(context) {
2020
const publicPaths = [];
2121

2222
for (const { compilation } of childStats) {
23+
if (compilation.options.devServer === false) {
24+
// eslint-disable-next-line no-continue
25+
continue;
26+
}
27+
2328
// The `output.path` is always present and always absolute
2429
const outputPath = compilation.getPath(
2530
compilation.outputOptions.path || "",

src/utils/ready.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ function ready(context, callback, req) {
1919
const name = (req && req.url) || callback.name;
2020

2121
context.logger.info(`wait until bundle finished${name ? `: ${name}` : ""}`);
22-
2322
context.callbacks.push(callback);
2423
}
2524

src/utils/setupHooks.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,9 @@ function setupHooks(context) {
144144
context.callbacks = [];
145145

146146
// Execute callback that are delayed
147-
callbacks.forEach(
148-
/**
149-
* @param {(...args: any[]) => Stats | MultiStats} callback
150-
*/
151-
(callback) => {
152-
callback(stats);
153-
},
154-
);
147+
for (const callback of callbacks) {
148+
callback(stats);
149+
}
155150
});
156151
}
157152

src/utils/setupOutputFileSystem.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ function setupOutputFileSystem(context) {
3030
// TODO we need to support webpack-dev-server as a plugin or revisit it
3131
const compiler =
3232
/** @type {MultiCompiler} */
33-
(context.compiler).compilers.filter((item) =>
34-
Object.prototype.hasOwnProperty.call(item.options, "devServer"),
33+
(context.compiler).compilers.filter(
34+
(item) =>
35+
Object.prototype.hasOwnProperty.call(item.options, "devServer") &&
36+
item.options.devServer !== false,
3537
);
3638

3739
({ outputFileSystem } =
@@ -48,6 +50,11 @@ function setupOutputFileSystem(context) {
4850
(context.compiler).compilers || [context.compiler];
4951

5052
for (const compiler of compilers) {
53+
if (compiler.options.devServer === false) {
54+
// eslint-disable-next-line no-continue
55+
continue;
56+
}
57+
5158
// @ts-ignore
5259
compiler.outputFileSystem = outputFileSystem;
5360
}

src/utils/setupWriteToDisk.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ function setupWriteToDisk(context) {
2121
(context.compiler).compilers || [context.compiler];
2222

2323
for (const compiler of compilers) {
24+
if (compiler.options.devServer === false) {
25+
// eslint-disable-next-line no-continue
26+
continue;
27+
}
28+
2429
compiler.hooks.emit.tap("DevMiddleware", () => {
2530
// @ts-ignore
2631
if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) {

test/__snapshots__/logging.test.js.snap.webpack5

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ success (webpack x.x.x) compiled successfully in x ms"
4747

4848
exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #3: stderr 1`] = `""`;
4949

50-
exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #3: stderr 2`] = `""`;
51-
5250
exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #3: stdout 1`] = `
5351
"asset bundle.js x KiB [emitted] (name: main)
5452
./broken.js x bytes [built] [code generated] [1 error]
@@ -79,7 +77,9 @@ cacheable modules x bytes
7977
webpack x.x.x compiled successfully in x ms"
8078
`;
8179

82-
exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #3: stdout 2`] = `
80+
exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #4: stderr 1`] = `""`;
81+
82+
exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #4: stdout 1`] = `
8383
"asset bundle.js x KiB [emitted] (name: main)
8484
./broken.js x bytes [built] [code generated] [1 error]
8585

@@ -103,6 +103,24 @@ asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main)
103103
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)"
104104
`;
105105

106+
exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #5: stderr 1`] = `""`;
107+
108+
exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #5: stdout 1`] = `
109+
"asset bundle.js x KiB [emitted] (name: main)
110+
./bar.js x bytes [built] [code generated]
111+
webpack x.x.x compiled successfully in x ms
112+
113+
asset bundle.js x KiB [emitted] (name: main)
114+
asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main)
115+
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)
116+
runtime modules x bytes x modules
117+
cacheable modules x bytes
118+
./foo.js x bytes [built] [code generated]
119+
./svg.svg x bytes [built] [code generated]
120+
./index.html x bytes [built] [code generated]
121+
webpack x.x.x compiled successfully in x ms"
122+
`;
123+
106124
exports[`logging should logging in multi-compiler and respect the "stats" option from configuration: stderr 1`] = `""`;
107125

108126
exports[`logging should logging in multi-compiler and respect the "stats" option from configuration: stdout 1`] = `
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
module.exports = [
6+
{
7+
mode: 'development',
8+
context: path.resolve(__dirname),
9+
entry: './bar.js',
10+
output: {
11+
filename: 'bundle.js',
12+
path: path.resolve(__dirname, '../outputs/array/js3'),
13+
publicPath: '/static-two/',
14+
},
15+
infrastructureLogging: {
16+
level: 'none'
17+
},
18+
stats: 'normal',
19+
devServer: false,
20+
},
21+
{
22+
mode: 'development',
23+
context: path.resolve(__dirname),
24+
entry: './foo.js',
25+
output: {
26+
filename: 'bundle.js',
27+
path: path.resolve(__dirname, '../outputs/array/js4'),
28+
publicPath: '/static-one/',
29+
},
30+
module: {
31+
rules: [
32+
{
33+
test: /\.(svg|html)$/,
34+
loader: 'file-loader',
35+
options: { name: '[name].[ext]' },
36+
},
37+
],
38+
},
39+
infrastructureLogging: {
40+
level: 'none'
41+
},
42+
stats: 'normal'
43+
}
44+
];

test/logging.test.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ function stderrToSnapshot(stderr) {
6565
const runner = path.resolve(__dirname, "./helpers/runner.js");
6666

6767
describe("logging", () => {
68+
beforeEach(async () => {
69+
await fs.promises.rm(path.resolve(__dirname, "./outputs/"), {
70+
recursive: true,
71+
force: true,
72+
});
73+
});
74+
6875
it("should logging on successfully build", (done) => {
6976
let proc;
7077

@@ -854,7 +861,7 @@ describe("logging", () => {
854861
});
855862
});
856863

857-
it('should logging in multi-compiler and respect the "stats" option from configuration #3', (done) => {
864+
it('should logging in multi-compiler and respect the "stats" option from configuration #4', (done) => {
858865
let proc;
859866

860867
try {
@@ -898,6 +905,50 @@ describe("logging", () => {
898905
});
899906
});
900907

908+
it('should logging in multi-compiler and respect the "stats" option from configuration #5', (done) => {
909+
let proc;
910+
911+
try {
912+
proc = execa(runner, [], {
913+
stdio: "pipe",
914+
env: {
915+
WEBPACK_CONFIG: "webpack.array.dev-server-false",
916+
FORCE_COLOR: true,
917+
},
918+
});
919+
} catch (error) {
920+
throw error;
921+
}
922+
923+
let stdout = "";
924+
let stderr = "";
925+
926+
proc.stdout.on("data", (chunk) => {
927+
stdout += chunk.toString();
928+
929+
if (/compiled-for-tests/gi.test(stdout)) {
930+
proc.stdin.write("|exit|");
931+
}
932+
});
933+
934+
proc.stderr.on("data", (chunk) => {
935+
stderr += chunk.toString();
936+
proc.stdin.write("|exit|");
937+
});
938+
939+
proc.on("error", (error) => {
940+
done(error);
941+
});
942+
943+
proc.on("exit", () => {
944+
expect(stdout).toContain("\u001b[1m");
945+
expect(stdoutToSnapshot(stdout)).toMatchSnapshot("stdout");
946+
expect(stderrToSnapshot(stderr)).toMatchSnapshot("stderr");
947+
948+
done();
949+
});
950+
});
951+
901952
it('should logging an error in "watch" method', (done) => {
902953
let proc;
903954

0 commit comments

Comments
 (0)