Skip to content

Commit 6f806f6

Browse files
refactor: fix logic
1 parent 4b07573 commit 6f806f6

9 files changed

Lines changed: 31 additions & 28 deletions

File tree

packages/webpack-cli/src/webpack-cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,7 @@ class WebpackCLI {
22112211
}
22122212

22132213
try {
2214-
options = require(isFileURL ? fileURLToPath(configPath) : configPath);
2214+
options = require(isFileURL ? fileURLToPath(configPath) : path.resolve(configPath));
22152215
} catch (err) {
22162216
if (this.isValidationError(err)) {
22172217
throw err;

test/build/basic/basic.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use strict";
22

3-
const { resolve } = require("node:path");
43
const { run } = require("../../utils/test-utils");
54

65
describe("bundle command", () => {
@@ -181,11 +180,10 @@ describe("bundle command", () => {
181180

182181
it("should log supplied config when logging level is log", async () => {
183182
const { exitCode, stderr, stdout } = await run(__dirname, ["--config", "./log.config.js"]);
184-
const configPath = resolve(__dirname, "./log.config.js");
185183

186184
expect(exitCode).toBe(0);
187185
expect(stderr).toContain("Compiler starting...");
188-
expect(stderr).toContain(`Compiler is using config: '${configPath}'`);
186+
expect(stderr).toContain("Compiler is using config: './log.config.js'");
189187
expect(stderr).toContain("Compiler finished");
190188
expect(stdout).toBeTruthy();
191189
});
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
const path = require("node:path");
2-
31
const { run } = require("../../../utils/test-utils");
42

53
describe("failure", () => {
64
it("should log error on not installed registers", async () => {
75
const { exitCode, stderr, stdout } = await run(__dirname, ["-c", "webpack.config.iced"]);
86

97
expect(exitCode).toBe(2);
10-
expect(stderr).toContain(
11-
`Failed to load '${path.resolve(__dirname, "./webpack.config.iced")}'`,
12-
);
8+
expect(stderr).toContain("Failed to load 'webpack.config.iced'");
139
expect(stdout).toBeFalsy();
1410
});
1511
});

test/build/config-format/failure/webpack.config.json5

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/build/config/basic/basic-config.test.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@ const { pathToFileURL } = require("node:url");
55
const { run } = require("../../../utils/test-utils");
66

77
describe("basic config file", () => {
8-
it("should build and not throw error with a basic configuration file using relative path", async () => {
8+
it("should build and not throw error with a basic configuration file", async () => {
99
const { exitCode, stderr, stdout } = await run(__dirname, ["-c", "webpack.config.js"]);
10+
console.log(stdout);
11+
expect(exitCode).toBe(0);
12+
expect(stderr).toBeFalsy();
13+
expect(stdout).toBeTruthy();
14+
});
15+
16+
it("should build and not throw error with a basic configuration file using relative path", async () => {
17+
const { exitCode, stderr, stdout } = await run(__dirname, ["-c", "./webpack.config.js"]);
1018
expect(exitCode).toBe(0);
1119
expect(stderr).toBeFalsy();
1220
expect(stdout).toBeTruthy();
@@ -26,11 +34,20 @@ describe("basic config file", () => {
2634
const { exitCode, stderr, stdout } = await run(__dirname, [
2735
"-c",
2836
pathToFileURL(resolve(__dirname, "webpack.config.js")).toString(),
29-
"--output-path",
30-
"./binary",
3137
]);
3238
expect(exitCode).toBe(0);
3339
expect(stderr).toBeFalsy();
3440
expect(stdout).toBeTruthy();
3541
});
42+
43+
it("should build and not throw error with a basic using weird path", async () => {
44+
const { exitCode, stderr, stdout } = await run(__dirname, [
45+
"-c",
46+
"../basic/./webpack.config.js",
47+
]);
48+
console.log(stdout);
49+
expect(exitCode).toBe(0);
50+
expect(stderr).toBeFalsy();
51+
expect(stdout).toBeTruthy();
52+
});
3653
});

test/build/config/error-array/__snapshots__/config-array-error.test.js.snap.webpack5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`config with invalid array syntax should throw syntax error and exit with non-zero exit code when even 1 object has syntax error 1`] = `
4-
"[webpack-cli] Failed to load '<cwd>/test/build/config/error-array/webpack.config.js' config
4+
"[webpack-cli] Failed to load './webpack.config.js' config
55
▶ ESM (\`import\`) failed:
66
Unexpected token ';'
77

test/build/merge/config-absent/merge-config-absent.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"use strict";
22

3-
const path = require("node:path");
4-
53
const { run } = require("../../../utils/test-utils");
64

75
describe("merge flag configuration", () => {
@@ -19,6 +17,6 @@ describe("merge flag configuration", () => {
1917
// Since the process will exit, nothing on stdout
2018
expect(stdout).toBeFalsy();
2119
// Confirm that the user is notified
22-
expect(stderr).toContain(`Failed to load '${path.resolve(__dirname, "./2.js")}' config`);
20+
expect(stderr).toContain("Failed to load './2.js' config");
2321
});
2422
});

test/configtest/with-config-path/__snapshots__/with-config-path.test.js.snap.webpack5

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3-
exports[`'configtest' command with the configuration path option should throw error if configuration does not exist: stderr 1`] = `"[webpack-cli] Failed to load '<cwd>/test/configtest/with-config-path/a.js' config"`;
3+
exports[`'configtest' command with the configuration path option should throw error if configuration does not exist: stderr 1`] = `"[webpack-cli] Failed to load './a.js' config"`;
44

55
exports[`'configtest' command with the configuration path option should throw error if configuration does not exist: stdout 1`] = `""`;
66

77
exports[`'configtest' command with the configuration path option should throw syntax error: stderr 1`] = `
8-
"[webpack-cli] Failed to load '<cwd>/test/configtest/with-config-path/syntax-error.config.js' config
8+
"[webpack-cli] Failed to load './syntax-error.config.js' config
99
▶ ESM (\`import\`) failed:
1010
Unexpected token ';'
1111

@@ -22,7 +22,7 @@ exports[`'configtest' command with the configuration path option should throw va
2222
-> Enable production optimizations or development hints."
2323
`;
2424

25-
exports[`'configtest' command with the configuration path option should throw validation error: stdout 1`] = `"[webpack-cli] Validate '<cwd>/test/configtest/with-config-path/error.config.js'."`;
25+
exports[`'configtest' command with the configuration path option should throw validation error: stdout 1`] = `"[webpack-cli] Validate './error.config.js'."`;
2626

2727
exports[`'configtest' command with the configuration path option should validate the config with alias 't': stderr 1`] = `
2828
"[webpack-cli] Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
@@ -31,11 +31,11 @@ exports[`'configtest' command with the configuration path option should validate
3131
-> Enable production optimizations or development hints."
3232
`;
3333

34-
exports[`'configtest' command with the configuration path option should validate the config with alias 't': stdout 1`] = `"[webpack-cli] Validate '<cwd>/test/configtest/with-config-path/error.config.js'."`;
34+
exports[`'configtest' command with the configuration path option should validate the config with alias 't': stdout 1`] = `"[webpack-cli] Validate './error.config.js'."`;
3535

3636
exports[`'configtest' command with the configuration path option should validate webpack config successfully: stderr 1`] = `""`;
3737

3838
exports[`'configtest' command with the configuration path option should validate webpack config successfully: stdout 1`] = `
39-
"[webpack-cli] Validate '<cwd>/test/configtest/with-config-path/basic.config.js'.
39+
"[webpack-cli] Validate './basic.config.js'.
4040
[webpack-cli] There are no validation errors in the given webpack configuration."
4141
`;

test/watch/basic/basic.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,6 @@ describe("basic", () => {
227227
});
228228

229229
it("should log supplied config with watch", async () => {
230-
const configPath = resolve(__dirname, "./log.config.js");
231-
232230
let stderr = "";
233231

234232
await runWatch(__dirname, ["watch", "--config", "log.config.js"], {
@@ -240,7 +238,7 @@ describe("basic", () => {
240238

241239
if (/Compiler finished/.test(data)) {
242240
expect(stderr).toContain("Compiler starting...");
243-
expect(stderr).toContain(`Compiler is using config: '${configPath}'`);
241+
expect(stderr).toContain("Compiler is using config: 'log.config.js'");
244242
expect(stderr).toContain("Compiler finished");
245243

246244
processKill(proc);

0 commit comments

Comments
 (0)