Skip to content

Commit 004ed38

Browse files
authored
fix(modern-compiler): dispose redundant compilers (#1245)
1 parent 262dc3d commit 004ed38

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

src/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,8 @@ function getCompileFn(loaderContext, implementation, apiType) {
768768
webpackCompiler.hooks.shutdown.tap("sass-loader", () => {
769769
compiler.dispose();
770770
});
771+
} else {
772+
compiler.dispose();
771773
}
772774
}
773775

test/__snapshots__/implementation-option.test.js.no-node-sass.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ exports[`implementation option not specify: errors 1`] = `[]`;
4848

4949
exports[`implementation option not specify: warnings 1`] = `[]`;
5050

51+
exports[`implementation option should dispose redundant compilers for \`modern-compiler\`: errors 1`] = `[]`;
52+
53+
exports[`implementation option should dispose redundant compilers for \`modern-compiler\`: warnings 1`] = `[]`;
54+
5155
exports[`implementation option should not swallow an error when trying to load a sass implementation: errors 1`] = `
5256
[
5357
"ModuleBuildError: Module build failed (from ../src/cjs.js):

test/__snapshots__/implementation-option.test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ exports[`implementation option not specify: errors 1`] = `[]`;
5252

5353
exports[`implementation option not specify: warnings 1`] = `[]`;
5454

55+
exports[`implementation option should dispose redundant compilers for \`modern-compiler\`: errors 1`] = `[]`;
56+
57+
exports[`implementation option should dispose redundant compilers for \`modern-compiler\`: warnings 1`] = `[]`;
58+
5559
exports[`implementation option should not swallow an error when trying to load a sass implementation: errors 1`] = `
5660
[
5761
"ModuleBuildError: Module build failed (from ../src/cjs.js):

test/implementation-option.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,70 @@ describe("implementation option", () => {
398398
await close(compiler);
399399
});
400400

401+
it("should dispose redundant compilers for `modern-compiler`", async () => {
402+
sassEmbeddedCompilerSpies.mockRestore();
403+
404+
let isInRace = false;
405+
406+
let firstDisposeSpy;
407+
let secondDisposeSpy;
408+
409+
const actualFn = sassEmbedded.initAsyncCompiler.bind(sassEmbedded);
410+
411+
const initSpy = jest
412+
.spyOn(sassEmbedded, "initAsyncCompiler")
413+
.mockImplementation(async () => {
414+
const compiler = await actualFn();
415+
416+
if (!isInRace) {
417+
firstDisposeSpy = jest.spyOn(compiler, "dispose");
418+
isInRace = true;
419+
420+
return new Promise((resolve) => {
421+
const interval = setInterval(() => {
422+
if (!isInRace) {
423+
clearInterval(interval);
424+
resolve(compiler);
425+
}
426+
});
427+
});
428+
}
429+
430+
isInRace = false;
431+
secondDisposeSpy = jest.spyOn(compiler, "dispose");
432+
433+
return compiler;
434+
});
435+
436+
const testId1 = getTestId("language", "scss");
437+
const testId2 = getTestId("language", "sass");
438+
const options = { api: "modern-compiler" };
439+
440+
// eslint-disable-next-line no-undefined
441+
const compiler = getCompiler(undefined, {
442+
entry: {
443+
one: `./${testId1}`,
444+
two: `./${testId2}`,
445+
},
446+
loader: { options },
447+
});
448+
const stats = await compile(compiler);
449+
450+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
451+
expect(getErrors(stats)).toMatchSnapshot("errors");
452+
expect(initSpy).toHaveBeenCalledTimes(2);
453+
454+
await close(compiler);
455+
456+
initSpy.mockRestore();
457+
458+
expect(firstDisposeSpy).toHaveBeenCalledTimes(1);
459+
firstDisposeSpy.mockRestore();
460+
461+
expect(secondDisposeSpy).toHaveBeenCalledTimes(1);
462+
secondDisposeSpy.mockRestore();
463+
});
464+
401465
it("should try to load using valid order", async () => {
402466
jest.doMock("sass", () => {
403467
const error = new Error("Some error sass");

0 commit comments

Comments
 (0)