2017-12-29 18:23:14 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const path = require("path");
|
2020-02-13 00:13:53 +08:00
|
|
|
const { createFsFromVolume, Volume } = require("memfs");
|
2018-12-09 21:26:35 +08:00
|
|
|
const webpack = require("..");
|
2017-12-29 18:23:14 +08:00
|
|
|
|
|
|
|
const createMultiCompiler = () => {
|
2018-02-25 18:46:17 +08:00
|
|
|
const compiler = webpack([
|
|
|
|
{
|
|
|
|
context: path.join(__dirname, "fixtures"),
|
|
|
|
entry: "./a.js"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
context: path.join(__dirname, "fixtures"),
|
|
|
|
entry: "./b.js"
|
|
|
|
}
|
|
|
|
]);
|
2020-02-15 13:54:52 +08:00
|
|
|
compiler.outputFileSystem = createFsFromVolume(new Volume());
|
2020-07-08 16:21:31 +08:00
|
|
|
compiler.watchFileSystem = {
|
|
|
|
watch(a, b, c, d, e, f, g) {}
|
|
|
|
};
|
2017-12-29 18:23:14 +08:00
|
|
|
return compiler;
|
|
|
|
};
|
|
|
|
|
2020-03-29 06:10:15 +08:00
|
|
|
describe("MultiCompiler", function () {
|
2018-05-18 22:34:01 +08:00
|
|
|
jest.setTimeout(20000);
|
|
|
|
|
2017-12-29 18:23:14 +08:00
|
|
|
it("should trigger 'run' for each child compiler", done => {
|
|
|
|
const compiler = createMultiCompiler();
|
|
|
|
let called = 0;
|
|
|
|
|
|
|
|
compiler.hooks.run.tap("MultiCompiler test", () => called++);
|
|
|
|
compiler.run(err => {
|
2018-02-25 18:46:17 +08:00
|
|
|
if (err) {
|
2017-12-29 18:23:14 +08:00
|
|
|
throw err;
|
|
|
|
} else {
|
2018-01-24 23:00:43 +08:00
|
|
|
expect(called).toBe(2);
|
2017-12-29 18:23:14 +08:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should trigger 'watchRun' for each child compiler", done => {
|
|
|
|
const compiler = createMultiCompiler();
|
|
|
|
let called = 0;
|
|
|
|
|
|
|
|
compiler.hooks.watchRun.tap("MultiCompiler test", () => called++);
|
|
|
|
const watcher = compiler.watch(1000, err => {
|
2018-02-25 18:46:17 +08:00
|
|
|
if (err) {
|
2017-12-29 18:23:14 +08:00
|
|
|
throw err;
|
|
|
|
} else {
|
|
|
|
watcher.close();
|
2018-01-24 23:00:43 +08:00
|
|
|
expect(called).toBe(2);
|
2017-12-29 18:23:14 +08:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-03-07 18:45:38 +08:00
|
|
|
|
2020-05-07 00:53:06 +08:00
|
|
|
it("should not be running twice at a time (run)", function (done) {
|
2018-03-07 18:45:38 +08:00
|
|
|
const compiler = createMultiCompiler();
|
|
|
|
compiler.run((err, stats) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
});
|
|
|
|
compiler.run((err, stats) => {
|
|
|
|
if (err) return done();
|
|
|
|
});
|
|
|
|
});
|
2020-05-07 00:53:06 +08:00
|
|
|
it("should not be running twice at a time (watch)", function (done) {
|
2018-03-07 18:45:38 +08:00
|
|
|
const compiler = createMultiCompiler();
|
2018-04-19 16:36:13 +08:00
|
|
|
const watcher = compiler.watch({}, (err, stats) => {
|
2018-03-07 18:45:38 +08:00
|
|
|
if (err) return done(err);
|
|
|
|
});
|
|
|
|
compiler.watch({}, (err, stats) => {
|
2018-04-19 16:36:13 +08:00
|
|
|
if (err) return watcher.close(done);
|
2018-03-07 18:45:38 +08:00
|
|
|
});
|
|
|
|
});
|
2020-05-07 00:53:06 +08:00
|
|
|
it("should not be running twice at a time (run - watch)", function (done) {
|
2018-03-07 18:45:38 +08:00
|
|
|
const compiler = createMultiCompiler();
|
|
|
|
compiler.run((err, stats) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
});
|
|
|
|
compiler.watch({}, (err, stats) => {
|
|
|
|
if (err) return done();
|
|
|
|
});
|
|
|
|
});
|
2020-05-07 00:53:06 +08:00
|
|
|
it("should not be running twice at a time (watch - run)", function (done) {
|
2018-03-07 18:45:38 +08:00
|
|
|
const compiler = createMultiCompiler();
|
2018-04-19 16:36:13 +08:00
|
|
|
let watcher;
|
|
|
|
watcher = compiler.watch({}, (err, stats) => {
|
2018-03-07 18:45:38 +08:00
|
|
|
if (err) return done(err);
|
|
|
|
});
|
|
|
|
compiler.run((err, stats) => {
|
2018-04-19 16:36:13 +08:00
|
|
|
if (err) return watcher.close(done);
|
2018-03-07 18:45:38 +08:00
|
|
|
});
|
|
|
|
});
|
2020-05-07 00:53:06 +08:00
|
|
|
it("should not be running twice at a time (instance cb)", function (done) {
|
2018-03-07 18:45:38 +08:00
|
|
|
const compiler = webpack(
|
|
|
|
{
|
|
|
|
context: __dirname,
|
|
|
|
mode: "production",
|
|
|
|
entry: "./c",
|
|
|
|
output: {
|
|
|
|
path: "/",
|
|
|
|
filename: "bundle.js"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
() => {}
|
|
|
|
);
|
2020-02-15 13:54:52 +08:00
|
|
|
compiler.outputFileSystem = createFsFromVolume(new Volume());
|
2018-03-07 18:45:38 +08:00
|
|
|
compiler.run((err, stats) => {
|
|
|
|
if (err) return done();
|
|
|
|
});
|
|
|
|
});
|
2020-03-29 06:10:15 +08:00
|
|
|
it("should run again correctly after first compilation", function (done) {
|
2018-03-07 18:45:38 +08:00
|
|
|
const compiler = createMultiCompiler();
|
|
|
|
compiler.run((err, stats) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
|
|
|
compiler.run((err, stats) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-03-29 06:10:15 +08:00
|
|
|
it("should watch again correctly after first compilation", function (done) {
|
2018-03-07 18:45:38 +08:00
|
|
|
const compiler = createMultiCompiler();
|
|
|
|
compiler.run((err, stats) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
|
2018-04-19 16:36:13 +08:00
|
|
|
let watcher;
|
|
|
|
watcher = compiler.watch({}, (err, stats) => {
|
2018-03-07 18:45:38 +08:00
|
|
|
if (err) return done(err);
|
2018-04-19 16:36:13 +08:00
|
|
|
watcher.close(done);
|
2018-03-07 18:45:38 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-03-29 06:10:15 +08:00
|
|
|
it("should run again correctly after first closed watch", function (done) {
|
2018-03-07 18:45:38 +08:00
|
|
|
const compiler = createMultiCompiler();
|
|
|
|
const watching = compiler.watch({}, (err, stats) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
});
|
|
|
|
watching.close(() => {
|
|
|
|
compiler.run((err, stats) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-03-29 06:10:15 +08:00
|
|
|
it("should watch again correctly after first closed watch", function (done) {
|
2018-03-07 18:45:38 +08:00
|
|
|
const compiler = createMultiCompiler();
|
|
|
|
const watching = compiler.watch({}, (err, stats) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
});
|
|
|
|
watching.close(() => {
|
2018-04-19 16:36:13 +08:00
|
|
|
let watcher;
|
|
|
|
watcher = compiler.watch({}, (err, stats) => {
|
2018-03-07 18:45:38 +08:00
|
|
|
if (err) return done(err);
|
2018-04-19 16:36:13 +08:00
|
|
|
watcher.close(done);
|
2018-03-07 18:45:38 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-12-29 18:23:14 +08:00
|
|
|
});
|