Skip to content

Commit

Permalink
fix problems with compiling twice
Browse files Browse the repository at this point in the history
better handle test errors
  • Loading branch information
sokra committed Jul 22, 2021
1 parent cdc9efe commit c23e8ce
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 62 deletions.
20 changes: 12 additions & 8 deletions test/ConfigTestCases.template.js
Expand Up @@ -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);
}
Expand Down
35 changes: 21 additions & 14 deletions test/configCases/asset-modules/http-url/server/index.js
Expand Up @@ -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") {
Expand All @@ -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 {
Expand All @@ -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();
}
});
}
}
Expand Down
15 changes: 11 additions & 4 deletions test/configCases/clean/enabled/webpack.config.js
Expand Up @@ -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("");
});
});
Expand Down
27 changes: 17 additions & 10 deletions test/configCases/clean/ignore-fn/webpack.config.js
Expand Up @@ -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("");
});
});
Expand Down
39 changes: 23 additions & 16 deletions test/configCases/clean/ignore-hook/webpack.config.js
Expand Up @@ -11,6 +11,7 @@ module.exports = {
},
plugins: [
compiler => {
let once = true;
compiler.hooks.thisCompilation.tap("Test", compilation => {
webpack.CleanPlugin.getCompilationHooks(compilation).keep.tap(
"Test",
Expand All @@ -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("");
});
});
Expand Down
27 changes: 17 additions & 10 deletions test/configCases/clean/ignore-rx/webpack.config.js
Expand Up @@ -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("");
});
});
Expand Down

0 comments on commit c23e8ce

Please sign in to comment.