Skip to content

Commit

Permalink
fix(modern-compiler): dispose redundant compilers (#1245)
Browse files Browse the repository at this point in the history
  • Loading branch information
hikiko4ern authored Nov 1, 2024
1 parent 262dc3d commit 004ed38
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,8 @@ function getCompileFn(loaderContext, implementation, apiType) {
webpackCompiler.hooks.shutdown.tap("sass-loader", () => {
compiler.dispose();
});
} else {
compiler.dispose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ exports[`implementation option not specify: errors 1`] = `[]`;

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

exports[`implementation option should dispose redundant compilers for \`modern-compiler\`: errors 1`] = `[]`;

exports[`implementation option should dispose redundant compilers for \`modern-compiler\`: warnings 1`] = `[]`;

exports[`implementation option should not swallow an error when trying to load a sass implementation: errors 1`] = `
[
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Expand Down
4 changes: 4 additions & 0 deletions test/__snapshots__/implementation-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ exports[`implementation option not specify: errors 1`] = `[]`;

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

exports[`implementation option should dispose redundant compilers for \`modern-compiler\`: errors 1`] = `[]`;

exports[`implementation option should dispose redundant compilers for \`modern-compiler\`: warnings 1`] = `[]`;

exports[`implementation option should not swallow an error when trying to load a sass implementation: errors 1`] = `
[
"ModuleBuildError: Module build failed (from ../src/cjs.js):
Expand Down
64 changes: 64 additions & 0 deletions test/implementation-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,70 @@ describe("implementation option", () => {
await close(compiler);
});

it("should dispose redundant compilers for `modern-compiler`", async () => {
sassEmbeddedCompilerSpies.mockRestore();

let isInRace = false;

let firstDisposeSpy;
let secondDisposeSpy;

const actualFn = sassEmbedded.initAsyncCompiler.bind(sassEmbedded);

const initSpy = jest
.spyOn(sassEmbedded, "initAsyncCompiler")
.mockImplementation(async () => {
const compiler = await actualFn();

if (!isInRace) {
firstDisposeSpy = jest.spyOn(compiler, "dispose");
isInRace = true;

return new Promise((resolve) => {
const interval = setInterval(() => {
if (!isInRace) {
clearInterval(interval);
resolve(compiler);
}
});
});
}

isInRace = false;
secondDisposeSpy = jest.spyOn(compiler, "dispose");

return compiler;
});

const testId1 = getTestId("language", "scss");
const testId2 = getTestId("language", "sass");
const options = { api: "modern-compiler" };

// eslint-disable-next-line no-undefined
const compiler = getCompiler(undefined, {
entry: {
one: `./${testId1}`,
two: `./${testId2}`,
},
loader: { options },
});
const stats = await compile(compiler);

expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
expect(initSpy).toHaveBeenCalledTimes(2);

await close(compiler);

initSpy.mockRestore();

expect(firstDisposeSpy).toHaveBeenCalledTimes(1);
firstDisposeSpy.mockRestore();

expect(secondDisposeSpy).toHaveBeenCalledTimes(1);
secondDisposeSpy.mockRestore();
});

it("should try to load using valid order", async () => {
jest.doMock("sass", () => {
const error = new Error("Some error sass");
Expand Down

0 comments on commit 004ed38

Please sign in to comment.