fix problems with compiling twice

better handle test errors
This commit is contained in:
Tobias Koppers 2021-07-22 11:11:18 +02:00
parent cdc9efefbe
commit c23e8ce5c7
6 changed files with 101 additions and 62 deletions

View File

@ -572,16 +572,20 @@ const describeCases = config => {
.catch(done);
};
if (config.cache) {
const compiler = require("..")(options);
compiler.run(err => {
if (err) return handleFatalError(err, done);
compiler.run((error, stats) => {
compiler.close(err => {
if (err) return handleFatalError(err, done);
onCompiled(error, stats);
try {
const compiler = require("..")(options);
compiler.run(err => {
if (err) return handleFatalError(err, done);
compiler.run((error, stats) => {
compiler.close(err => {
if (err) return handleFatalError(err, done);
onCompiled(error, stats);
});
});
});
});
} catch (e) {
handleFatalError(e, done);
}
} else {
require("..")(options, onCompiled);
}

View File

@ -6,7 +6,10 @@ const fs = require("fs");
* @returns {Promise<import("http").Server>} server instance
*/
function createServer(port) {
const file = fs.readFileSync("./test/configCases/asset-modules/http-url/server/index.css").toString().trim();
const file = fs
.readFileSync("./test/configCases/asset-modules/http-url/server/index.css")
.toString()
.trim();
const server = http.createServer((req, res) => {
if (req.url !== "/index.css") {
@ -18,7 +21,7 @@ function createServer(port) {
});
return new Promise((resolve, reject) => {
server.listen(port, (err) => {
server.listen(port, err => {
if (err) {
reject(err);
} else {
@ -40,21 +43,25 @@ class ServerPlugin {
* @param {import("../../../../../").Compiler} compiler
*/
apply(compiler) {
const serverPromise = createServer(this.port);
let server;
serverPromise
.then(server => server.unref());
compiler.hooks.beforeRun.tapPromise(
"ServerPlugin",
async (compiler, callback) => {
if (!server) {
server = await createServer(this.port);
server.unref();
}
}
);
compiler.hooks.done.tapAsync("ServerPlugin", (stats, callback) => {
serverPromise
.then(server => server.close(callback))
.catch(callback)
});
compiler.hooks.beforeRun.tapAsync("ServerPlugin", (compiler, callback) => {
serverPromise
.then(() => callback())
.catch(callback)
if (server) {
server.close(callback);
server = undefined;
} else {
callback();
}
});
}
}

View File

@ -10,12 +10,19 @@ module.exports = {
},
plugins: [
compiler => {
let once = true;
compiler.hooks.thisCompilation.tap("Test", compilation => {
compilation.hooks.processAssets.tap("Test", assets => {
const outputPath = compilation.getPath(compiler.outputPath, {});
const customDir = path.join(outputPath, "this/dir/should/be/removed");
fs.mkdirSync(customDir, { recursive: true });
fs.writeFileSync(path.join(customDir, "file.ext"), "");
if (once) {
const outputPath = compilation.getPath(compiler.outputPath, {});
const customDir = path.join(
outputPath,
"this/dir/should/be/removed"
);
fs.mkdirSync(customDir, { recursive: true });
fs.writeFileSync(path.join(customDir, "file.ext"), "");
once = false;
}
assets["this/dir/should/not/be/removed/file.ext"] = new RawSource("");
});
});

View File

@ -14,18 +14,25 @@ module.exports = {
},
plugins: [
compiler => {
let once = true;
compiler.hooks.thisCompilation.tap("Test", compilation => {
compilation.hooks.processAssets.tap("Test", assets => {
const outputPath = compilation.getPath(compiler.outputPath, {});
const customDir = path.join(outputPath, "this/dir/should/be/removed");
const ignoredDir = path.join(
outputPath,
"this/is/ignored/dir/that/should/not/be/removed"
);
fs.mkdirSync(customDir, { recursive: true });
fs.writeFileSync(path.join(customDir, "file.ext"), "");
fs.mkdirSync(ignoredDir, { recursive: true });
fs.writeFileSync(path.join(ignoredDir, "file.ext"), "");
if (once) {
const outputPath = compilation.getPath(compiler.outputPath, {});
const customDir = path.join(
outputPath,
"this/dir/should/be/removed"
);
const ignoredDir = path.join(
outputPath,
"this/is/ignored/dir/that/should/not/be/removed"
);
fs.mkdirSync(customDir, { recursive: true });
fs.writeFileSync(path.join(customDir, "file.ext"), "");
fs.mkdirSync(ignoredDir, { recursive: true });
fs.writeFileSync(path.join(ignoredDir, "file.ext"), "");
once = false;
}
assets["this/dir/should/not/be/removed/file.ext"] = new RawSource("");
});
});

View File

@ -11,6 +11,7 @@ module.exports = {
},
plugins: [
compiler => {
let once = true;
compiler.hooks.thisCompilation.tap("Test", compilation => {
webpack.CleanPlugin.getCompilationHooks(compilation).keep.tap(
"Test",
@ -20,22 +21,28 @@ module.exports = {
}
);
compilation.hooks.processAssets.tap("Test", assets => {
const outputPath = compilation.getPath(compiler.outputPath, {});
const customDir = path.join(outputPath, "this/dir/should/be/removed");
const ignoredDir = path.join(
outputPath,
"this/is/ignored/dir/that/should/not/be/removed"
);
const ignoredTooDir = path.join(
outputPath,
"this/is/ignored/too/dir/that/should/not/be/removed"
);
fs.mkdirSync(customDir, { recursive: true });
fs.writeFileSync(path.join(customDir, "file.ext"), "");
fs.mkdirSync(ignoredDir, { recursive: true });
fs.writeFileSync(path.join(ignoredDir, "file.ext"), "");
fs.mkdirSync(ignoredTooDir, { recursive: true });
fs.writeFileSync(path.join(ignoredTooDir, "file.ext"), "");
if (once) {
const outputPath = compilation.getPath(compiler.outputPath, {});
const customDir = path.join(
outputPath,
"this/dir/should/be/removed"
);
const ignoredDir = path.join(
outputPath,
"this/is/ignored/dir/that/should/not/be/removed"
);
const ignoredTooDir = path.join(
outputPath,
"this/is/ignored/too/dir/that/should/not/be/removed"
);
fs.mkdirSync(customDir, { recursive: true });
fs.writeFileSync(path.join(customDir, "file.ext"), "");
fs.mkdirSync(ignoredDir, { recursive: true });
fs.writeFileSync(path.join(ignoredDir, "file.ext"), "");
fs.mkdirSync(ignoredTooDir, { recursive: true });
fs.writeFileSync(path.join(ignoredTooDir, "file.ext"), "");
once = false;
}
assets["this/dir/should/not/be/removed/file.ext"] = new RawSource("");
});
});

View File

@ -12,18 +12,25 @@ module.exports = {
},
plugins: [
compiler => {
let once = true;
compiler.hooks.thisCompilation.tap("Test", compilation => {
compilation.hooks.processAssets.tap("Test", assets => {
const outputPath = compilation.getPath(compiler.outputPath, {});
const customDir = path.join(outputPath, "this/dir/should/be/removed");
const ignoredDir = path.join(
outputPath,
"this/is/ignored/dir/that/should/not/be/removed"
);
fs.mkdirSync(customDir, { recursive: true });
fs.writeFileSync(path.join(customDir, "file.ext"), "");
fs.mkdirSync(ignoredDir, { recursive: true });
fs.writeFileSync(path.join(ignoredDir, "file.ext"), "");
if (once) {
const outputPath = compilation.getPath(compiler.outputPath, {});
const customDir = path.join(
outputPath,
"this/dir/should/be/removed"
);
const ignoredDir = path.join(
outputPath,
"this/is/ignored/dir/that/should/not/be/removed"
);
fs.mkdirSync(customDir, { recursive: true });
fs.writeFileSync(path.join(customDir, "file.ext"), "");
fs.mkdirSync(ignoredDir, { recursive: true });
fs.writeFileSync(path.join(ignoredDir, "file.ext"), "");
once = false;
}
assets["this/dir/should/not/be/removed/file.ext"] = new RawSource("");
});
});