Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(modern-compiler): dispose redundant compilers #1245

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of if/elses deeply nested, but we can't return early in this scope, so it's okay.

}
}

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