Skip to content

Commit c23e8ce

Browse files
committed
fix problems with compiling twice
better handle test errors
1 parent cdc9efe commit c23e8ce

File tree

6 files changed

+101
-62
lines changed

6 files changed

+101
-62
lines changed

test/ConfigTestCases.template.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -572,16 +572,20 @@ const describeCases = config => {
572572
.catch(done);
573573
};
574574
if (config.cache) {
575-
const compiler = require("..")(options);
576-
compiler.run(err => {
577-
if (err) return handleFatalError(err, done);
578-
compiler.run((error, stats) => {
579-
compiler.close(err => {
580-
if (err) return handleFatalError(err, done);
581-
onCompiled(error, stats);
575+
try {
576+
const compiler = require("..")(options);
577+
compiler.run(err => {
578+
if (err) return handleFatalError(err, done);
579+
compiler.run((error, stats) => {
580+
compiler.close(err => {
581+
if (err) return handleFatalError(err, done);
582+
onCompiled(error, stats);
583+
});
582584
});
583585
});
584-
});
586+
} catch (e) {
587+
handleFatalError(e, done);
588+
}
585589
} else {
586590
require("..")(options, onCompiled);
587591
}

test/configCases/asset-modules/http-url/server/index.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ const fs = require("fs");
66
* @returns {Promise<import("http").Server>} server instance
77
*/
88
function createServer(port) {
9-
const file = fs.readFileSync("./test/configCases/asset-modules/http-url/server/index.css").toString().trim();
9+
const file = fs
10+
.readFileSync("./test/configCases/asset-modules/http-url/server/index.css")
11+
.toString()
12+
.trim();
1013

1114
const server = http.createServer((req, res) => {
1215
if (req.url !== "/index.css") {
@@ -18,7 +21,7 @@ function createServer(port) {
1821
});
1922

2023
return new Promise((resolve, reject) => {
21-
server.listen(port, (err) => {
24+
server.listen(port, err => {
2225
if (err) {
2326
reject(err);
2427
} else {
@@ -40,21 +43,25 @@ class ServerPlugin {
4043
* @param {import("../../../../../").Compiler} compiler
4144
*/
4245
apply(compiler) {
43-
const serverPromise = createServer(this.port);
46+
let server;
4447

45-
serverPromise
46-
.then(server => server.unref());
48+
compiler.hooks.beforeRun.tapPromise(
49+
"ServerPlugin",
50+
async (compiler, callback) => {
51+
if (!server) {
52+
server = await createServer(this.port);
53+
server.unref();
54+
}
55+
}
56+
);
4757

4858
compiler.hooks.done.tapAsync("ServerPlugin", (stats, callback) => {
49-
serverPromise
50-
.then(server => server.close(callback))
51-
.catch(callback)
52-
});
53-
54-
compiler.hooks.beforeRun.tapAsync("ServerPlugin", (compiler, callback) => {
55-
serverPromise
56-
.then(() => callback())
57-
.catch(callback)
59+
if (server) {
60+
server.close(callback);
61+
server = undefined;
62+
} else {
63+
callback();
64+
}
5865
});
5966
}
6067
}

test/configCases/clean/enabled/webpack.config.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ module.exports = {
1010
},
1111
plugins: [
1212
compiler => {
13+
let once = true;
1314
compiler.hooks.thisCompilation.tap("Test", compilation => {
1415
compilation.hooks.processAssets.tap("Test", assets => {
15-
const outputPath = compilation.getPath(compiler.outputPath, {});
16-
const customDir = path.join(outputPath, "this/dir/should/be/removed");
17-
fs.mkdirSync(customDir, { recursive: true });
18-
fs.writeFileSync(path.join(customDir, "file.ext"), "");
16+
if (once) {
17+
const outputPath = compilation.getPath(compiler.outputPath, {});
18+
const customDir = path.join(
19+
outputPath,
20+
"this/dir/should/be/removed"
21+
);
22+
fs.mkdirSync(customDir, { recursive: true });
23+
fs.writeFileSync(path.join(customDir, "file.ext"), "");
24+
once = false;
25+
}
1926
assets["this/dir/should/not/be/removed/file.ext"] = new RawSource("");
2027
});
2128
});

test/configCases/clean/ignore-fn/webpack.config.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,25 @@ module.exports = {
1414
},
1515
plugins: [
1616
compiler => {
17+
let once = true;
1718
compiler.hooks.thisCompilation.tap("Test", compilation => {
1819
compilation.hooks.processAssets.tap("Test", assets => {
19-
const outputPath = compilation.getPath(compiler.outputPath, {});
20-
const customDir = path.join(outputPath, "this/dir/should/be/removed");
21-
const ignoredDir = path.join(
22-
outputPath,
23-
"this/is/ignored/dir/that/should/not/be/removed"
24-
);
25-
fs.mkdirSync(customDir, { recursive: true });
26-
fs.writeFileSync(path.join(customDir, "file.ext"), "");
27-
fs.mkdirSync(ignoredDir, { recursive: true });
28-
fs.writeFileSync(path.join(ignoredDir, "file.ext"), "");
20+
if (once) {
21+
const outputPath = compilation.getPath(compiler.outputPath, {});
22+
const customDir = path.join(
23+
outputPath,
24+
"this/dir/should/be/removed"
25+
);
26+
const ignoredDir = path.join(
27+
outputPath,
28+
"this/is/ignored/dir/that/should/not/be/removed"
29+
);
30+
fs.mkdirSync(customDir, { recursive: true });
31+
fs.writeFileSync(path.join(customDir, "file.ext"), "");
32+
fs.mkdirSync(ignoredDir, { recursive: true });
33+
fs.writeFileSync(path.join(ignoredDir, "file.ext"), "");
34+
once = false;
35+
}
2936
assets["this/dir/should/not/be/removed/file.ext"] = new RawSource("");
3037
});
3138
});

test/configCases/clean/ignore-hook/webpack.config.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
},
1212
plugins: [
1313
compiler => {
14+
let once = true;
1415
compiler.hooks.thisCompilation.tap("Test", compilation => {
1516
webpack.CleanPlugin.getCompilationHooks(compilation).keep.tap(
1617
"Test",
@@ -20,22 +21,28 @@ module.exports = {
2021
}
2122
);
2223
compilation.hooks.processAssets.tap("Test", assets => {
23-
const outputPath = compilation.getPath(compiler.outputPath, {});
24-
const customDir = path.join(outputPath, "this/dir/should/be/removed");
25-
const ignoredDir = path.join(
26-
outputPath,
27-
"this/is/ignored/dir/that/should/not/be/removed"
28-
);
29-
const ignoredTooDir = path.join(
30-
outputPath,
31-
"this/is/ignored/too/dir/that/should/not/be/removed"
32-
);
33-
fs.mkdirSync(customDir, { recursive: true });
34-
fs.writeFileSync(path.join(customDir, "file.ext"), "");
35-
fs.mkdirSync(ignoredDir, { recursive: true });
36-
fs.writeFileSync(path.join(ignoredDir, "file.ext"), "");
37-
fs.mkdirSync(ignoredTooDir, { recursive: true });
38-
fs.writeFileSync(path.join(ignoredTooDir, "file.ext"), "");
24+
if (once) {
25+
const outputPath = compilation.getPath(compiler.outputPath, {});
26+
const customDir = path.join(
27+
outputPath,
28+
"this/dir/should/be/removed"
29+
);
30+
const ignoredDir = path.join(
31+
outputPath,
32+
"this/is/ignored/dir/that/should/not/be/removed"
33+
);
34+
const ignoredTooDir = path.join(
35+
outputPath,
36+
"this/is/ignored/too/dir/that/should/not/be/removed"
37+
);
38+
fs.mkdirSync(customDir, { recursive: true });
39+
fs.writeFileSync(path.join(customDir, "file.ext"), "");
40+
fs.mkdirSync(ignoredDir, { recursive: true });
41+
fs.writeFileSync(path.join(ignoredDir, "file.ext"), "");
42+
fs.mkdirSync(ignoredTooDir, { recursive: true });
43+
fs.writeFileSync(path.join(ignoredTooDir, "file.ext"), "");
44+
once = false;
45+
}
3946
assets["this/dir/should/not/be/removed/file.ext"] = new RawSource("");
4047
});
4148
});

test/configCases/clean/ignore-rx/webpack.config.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,25 @@ module.exports = {
1212
},
1313
plugins: [
1414
compiler => {
15+
let once = true;
1516
compiler.hooks.thisCompilation.tap("Test", compilation => {
1617
compilation.hooks.processAssets.tap("Test", assets => {
17-
const outputPath = compilation.getPath(compiler.outputPath, {});
18-
const customDir = path.join(outputPath, "this/dir/should/be/removed");
19-
const ignoredDir = path.join(
20-
outputPath,
21-
"this/is/ignored/dir/that/should/not/be/removed"
22-
);
23-
fs.mkdirSync(customDir, { recursive: true });
24-
fs.writeFileSync(path.join(customDir, "file.ext"), "");
25-
fs.mkdirSync(ignoredDir, { recursive: true });
26-
fs.writeFileSync(path.join(ignoredDir, "file.ext"), "");
18+
if (once) {
19+
const outputPath = compilation.getPath(compiler.outputPath, {});
20+
const customDir = path.join(
21+
outputPath,
22+
"this/dir/should/be/removed"
23+
);
24+
const ignoredDir = path.join(
25+
outputPath,
26+
"this/is/ignored/dir/that/should/not/be/removed"
27+
);
28+
fs.mkdirSync(customDir, { recursive: true });
29+
fs.writeFileSync(path.join(customDir, "file.ext"), "");
30+
fs.mkdirSync(ignoredDir, { recursive: true });
31+
fs.writeFileSync(path.join(ignoredDir, "file.ext"), "");
32+
once = false;
33+
}
2734
assets["this/dir/should/not/be/removed/file.ext"] = new RawSource("");
2835
});
2936
});

0 commit comments

Comments
 (0)