Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support dev server false #1921

Merged
merged 9 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,9 @@ function wrapper(context) {
headers = allHeaders;
}

headers.forEach((header) => {
setResponseHeader(res, header.key, header.value);
});
for (const { key, value } of headers) {
setResponseHeader(res, key, value);
}
}

if (
Expand Down
5 changes: 5 additions & 0 deletions src/utils/getPaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ function getPaths(context) {
const publicPaths = [];

for (const { compilation } of childStats) {
if (compilation.options.devServer === false) {
// eslint-disable-next-line no-continue
continue;
}

// The `output.path` is always present and always absolute
const outputPath = compilation.getPath(
compilation.outputOptions.path || "",
Expand Down
1 change: 0 additions & 1 deletion src/utils/ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ function ready(context, callback, req) {
const name = (req && req.url) || callback.name;

context.logger.info(`wait until bundle finished${name ? `: ${name}` : ""}`);

context.callbacks.push(callback);
}

Expand Down
11 changes: 3 additions & 8 deletions src/utils/setupHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,9 @@ function setupHooks(context) {
context.callbacks = [];

// Execute callback that are delayed
callbacks.forEach(
/**
* @param {(...args: any[]) => Stats | MultiStats} callback
*/
(callback) => {
callback(stats);
},
);
for (const callback of callbacks) {
callback(stats);
}
});
}

Expand Down
11 changes: 9 additions & 2 deletions src/utils/setupOutputFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ function setupOutputFileSystem(context) {
// TODO we need to support webpack-dev-server as a plugin or revisit it
const compiler =
/** @type {MultiCompiler} */
(context.compiler).compilers.filter((item) =>
Object.prototype.hasOwnProperty.call(item.options, "devServer"),
(context.compiler).compilers.filter(
(item) =>
Object.prototype.hasOwnProperty.call(item.options, "devServer") &&
item.options.devServer !== false,
);

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

for (const compiler of compilers) {
if (compiler.options.devServer === false) {
// eslint-disable-next-line no-continue
continue;
}

// @ts-ignore
compiler.outputFileSystem = outputFileSystem;
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/setupWriteToDisk.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
(context.compiler).compilers || [context.compiler];

for (const compiler of compilers) {
if (compiler.options.devServer === false) {
// eslint-disable-next-line no-continue
continue;

Check warning on line 26 in src/utils/setupWriteToDisk.js

View check run for this annotation

Codecov / codecov/patch

src/utils/setupWriteToDisk.js#L26

Added line #L26 was not covered by tests
}

compiler.hooks.emit.tap("DevMiddleware", () => {
// @ts-ignore
if (compiler.hasWebpackDevMiddlewareAssetEmittedCallback) {
Expand Down
24 changes: 21 additions & 3 deletions test/__snapshots__/logging.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ success (webpack x.x.x) compiled successfully in x ms"

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

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

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

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

exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #4: stdout 1`] = `
"asset bundle.js x KiB [emitted] (name: main)
./broken.js x bytes [built] [code generated] [1 error]

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

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

exports[`logging should logging in multi-compiler and respect the "stats" option from configuration #5: stdout 1`] = `
"asset bundle.js x KiB [emitted] (name: main)
./bar.js x bytes [built] [code generated]
webpack x.x.x compiled successfully in x ms

asset bundle.js x KiB [emitted] (name: main)
asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main)
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)
runtime modules x bytes x modules
cacheable modules x bytes
./foo.js x bytes [built] [code generated]
./svg.svg x bytes [built] [code generated]
./index.html x bytes [built] [code generated]
webpack x.x.x compiled successfully in x ms"
`;

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

exports[`logging should logging in multi-compiler and respect the "stats" option from configuration: stdout 1`] = `
Expand Down
44 changes: 44 additions & 0 deletions test/fixtures/webpack.array.dev-server-false.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

const path = require('path');

module.exports = [
{
mode: 'development',
context: path.resolve(__dirname),
entry: './bar.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, '../outputs/array/js3'),
publicPath: '/static-two/',
},
infrastructureLogging: {
level: 'none'
},
stats: 'normal',
devServer: false,
},
{
mode: 'development',
context: path.resolve(__dirname),
entry: './foo.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, '../outputs/array/js4'),
publicPath: '/static-one/',
},
module: {
rules: [
{
test: /\.(svg|html)$/,
loader: 'file-loader',
options: { name: '[name].[ext]' },
},
],
},
infrastructureLogging: {
level: 'none'
},
stats: 'normal'
}
];
53 changes: 52 additions & 1 deletion test/logging.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ function stderrToSnapshot(stderr) {
const runner = path.resolve(__dirname, "./helpers/runner.js");

describe("logging", () => {
beforeEach(async () => {
await fs.promises.rm(path.resolve(__dirname, "./outputs/"), {
recursive: true,
force: true,
});
});

it("should logging on successfully build", (done) => {
let proc;

Expand Down Expand Up @@ -854,7 +861,7 @@ describe("logging", () => {
});
});

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

try {
Expand Down Expand Up @@ -898,6 +905,50 @@ describe("logging", () => {
});
});

it('should logging in multi-compiler and respect the "stats" option from configuration #5', (done) => {
let proc;

try {
proc = execa(runner, [], {
stdio: "pipe",
env: {
WEBPACK_CONFIG: "webpack.array.dev-server-false",
FORCE_COLOR: true,
},
});
} catch (error) {
throw error;
}

let stdout = "";
let stderr = "";

proc.stdout.on("data", (chunk) => {
stdout += chunk.toString();

if (/compiled-for-tests/gi.test(stdout)) {
proc.stdin.write("|exit|");
}
});

proc.stderr.on("data", (chunk) => {
stderr += chunk.toString();
proc.stdin.write("|exit|");
});

proc.on("error", (error) => {
done(error);
});

proc.on("exit", () => {
expect(stdout).toContain("\u001b[1m");
expect(stdoutToSnapshot(stdout)).toMatchSnapshot("stdout");
expect(stderrToSnapshot(stderr)).toMatchSnapshot("stderr");

done();
});
});

it('should logging an error in "watch" method', (done) => {
let proc;

Expand Down
Loading
Loading