Skip to content

Commit 07c1f6d

Browse files
committed
Prevent webpack from running twice at a time
1 parent edbb6f6 commit 07c1f6d

File tree

3 files changed

+174
-1
lines changed

3 files changed

+174
-1
lines changed

lib/Compiler.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,36 @@ class Compiler extends Tapable {
129129
this.context = context;
130130

131131
this.requestShortener = new RequestShortener(context);
132+
133+
this.running = false;
132134
}
133135

134136
watch(watchOptions, handler) {
137+
if (this.running)
138+
return handler(
139+
new Error(
140+
"You ran Webpack twice. Each instance only supports a single concurrent compilation at a time."
141+
)
142+
);
143+
144+
this.running = true;
135145
this.fileTimestamps = new Map();
136146
this.contextTimestamps = new Map();
137147
return new Watching(this, watchOptions, handler);
138148
}
139149

140150
run(callback) {
151+
if (this.running)
152+
return callback(
153+
new Error(
154+
"You ran Webpack twice. Each instance only supports a single concurrent compilation at a time."
155+
)
156+
);
157+
141158
const startTime = Date.now();
142159

160+
this.running = true;
161+
143162
const onCompiled = (err, compilation) => {
144163
if (err) return callback(err);
145164

@@ -148,6 +167,8 @@ class Compiler extends Tapable {
148167
stats.startTime = startTime;
149168
stats.endTime = Date.now();
150169
this.hooks.done.callAsync(stats, err => {
170+
this.running = false;
171+
151172
if (err) return callback(err);
152173
return callback(null, stats);
153174
});
@@ -181,6 +202,8 @@ class Compiler extends Tapable {
181202
stats.startTime = startTime;
182203
stats.endTime = Date.now();
183204
this.hooks.done.callAsync(stats, err => {
205+
this.running = false;
206+
184207
if (err) return callback(err);
185208
return callback(null, stats);
186209
});

lib/Watching.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ class Watching {
168168
close(callback) {
169169
if (callback === undefined) callback = () => {};
170170

171+
this.compiler.running = false;
171172
this.closed = true;
172173
if (this.watcher) {
173174
this.watcher.close();

test/Compiler.test.js

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ describe("Compiler", () => {
246246
});
247247
});
248248
});
249-
it("should not emit on errors", function(done) {
249+
it("should not emit on errors", function(done) {
250250
const compiler = webpack({
251251
context: __dirname,
252252
mode: "production",
@@ -282,5 +282,154 @@ describe("Compiler", () => {
282282
return done(new Error("Bundle should not be created on error"));
283283
done();
284284
});
285+
});
286+
it("should not be run twice at a time (run)", function(done) {
287+
const compiler = webpack({
288+
context: __dirname,
289+
mode: "production",
290+
entry: "./c",
291+
output: {
292+
path: "/",
293+
filename: "bundle.js"
294+
}
295+
});
296+
compiler.outputFileSystem = new MemoryFs();
297+
compiler.run((err, stats) => {
298+
if (err) return done(err);
299+
});
300+
compiler.run((err, stats) => {
301+
if (err) return done();
302+
});
303+
});
304+
it("should not be run twice at a time (watch)", function(done) {
305+
const compiler = webpack({
306+
context: __dirname,
307+
mode: "production",
308+
entry: "./c",
309+
output: {
310+
path: "/",
311+
filename: "bundle.js"
312+
}
313+
});
314+
compiler.outputFileSystem = new MemoryFs();
315+
compiler.watch({}, (err, stats) => {
316+
if (err) return done(err);
317+
});
318+
compiler.watch({}, (err, stats) => {
319+
if (err) return done();
320+
});
321+
});
322+
it("should not be run twice at a time (run - watch)", function(done) {
323+
const compiler = webpack({
324+
context: __dirname,
325+
mode: "production",
326+
entry: "./c",
327+
output: {
328+
path: "/",
329+
filename: "bundle.js"
330+
}
331+
});
332+
compiler.outputFileSystem = new MemoryFs();
333+
compiler.run((err, stats) => {
334+
if (err) return done(err);
335+
});
336+
compiler.watch({}, (err, stats) => {
337+
if (err) return done();
338+
});
339+
});
340+
it("should not be run twice at a time (watch - run)", function(done) {
341+
const compiler = webpack({
342+
context: __dirname,
343+
mode: "production",
344+
entry: "./c",
345+
output: {
346+
path: "/",
347+
filename: "bundle.js"
348+
}
349+
});
350+
compiler.outputFileSystem = new MemoryFs();
351+
compiler.watch({}, (err, stats) => {
352+
if (err) return done(err);
353+
});
354+
compiler.run((err, stats) => {
355+
if (err) return done();
356+
});
357+
});
358+
it("should not be run twice at a time (instance cb)", function(done) {
359+
const compiler = webpack({
360+
context: __dirname,
361+
mode: "production",
362+
entry: "./c",
363+
output: {
364+
path: "/",
365+
filename: "bundle.js"
366+
}
367+
}, () => {});
368+
compiler.outputFileSystem = new MemoryFs();
369+
compiler.run((err, stats) => {
370+
if (err) return done();
371+
});
372+
});
373+
it("should run again correctly after first compilation", function(done) {
374+
const compiler = webpack({
375+
context: __dirname,
376+
mode: "production",
377+
entry: "./c",
378+
output: {
379+
path: "/",
380+
filename: "bundle.js"
381+
}
382+
});
383+
compiler.outputFileSystem = new MemoryFs();
384+
compiler.run((err, stats) => {
385+
if (err) return done(err);
386+
387+
compiler.run((err, stats) => {
388+
if (err) return done(err);
389+
done()
390+
});
391+
});
392+
});
393+
it("should watch again correctly after first compilation", function(done) {
394+
const compiler = webpack({
395+
context: __dirname,
396+
mode: "production",
397+
entry: "./c",
398+
output: {
399+
path: "/",
400+
filename: "bundle.js"
401+
}
402+
});
403+
compiler.outputFileSystem = new MemoryFs();
404+
compiler.run((err, stats) => {
405+
if (err) return done(err);
406+
407+
compiler.watch({}, (err, stats) => {
408+
if (err) return done(err);
409+
done()
410+
});
411+
});
412+
});
413+
it("should run again correctly after first closed watch", function(done) {
414+
const compiler = webpack({
415+
context: __dirname,
416+
mode: "production",
417+
entry: "./c",
418+
output: {
419+
path: "/",
420+
filename: "bundle.js"
421+
}
422+
});
423+
compiler.outputFileSystem = new MemoryFs();
424+
const watching = compiler.watch({}, (err, stats) => {
425+
if (err) return done(err);
426+
done()
427+
});
428+
watching.close(() => {
429+
compiler.run((err, stats) => {
430+
if (err) return done(err);
431+
done()
432+
});
433+
})
285434
});
286435
});

0 commit comments

Comments
 (0)