Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/quick-pumas-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@swc/cli": patch
---

fix(cli): Destroy the worker pool after the initial compilation
76 changes: 76 additions & 0 deletions packages/cli/src/swc/__tests__/dir.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { swcDir } from "../index";

jest.mock("../compile", () => ({
outputResult: jest.fn(),
}));

let mockRun: any;
const mockDestroy = jest.fn();

jest.mock("piscina", () => ({
__esModule: true,
default: class {
run(...args: any[]) {
return mockRun(...args);
}
destroy() {
return mockDestroy();
}
},
}));

const cliOptions: any = {
outDir: "./.temp/",
watch: false,
filenames: ["./src/swcx/"],
extensions: [".ts"],
stripLeadingPaths: true,
sync: false,
};
const swcOptions: any = {
jsc: {
target: "esnext",
externalHelpers: false,
},
module: {
type: "commonjs",
},
};

describe("dir worker pool", () => {
beforeEach(() => {
mockDestroy.mockClear();
});

it("should destroy the pool after a successful compilation", async () => {
mockRun = () => Promise.resolve(1); // mock compile success

const onSuccess = jest.fn();
const onFail = jest.fn();

await swcDir({
cliOptions,
swcOptions,
callbacks: { onSuccess, onFail },
});

expect(onSuccess.mock.calls).toHaveLength(1);
expect(mockDestroy.mock.calls).toHaveLength(1);
});

it("should destroy the pool when a file fails to compile", async () => {
mockRun = () => Promise.reject(new Error("fail")); // mock compile fail

const onSuccess = jest.fn();
const onFail = jest.fn();

await swcDir({
cliOptions,
swcOptions,
callbacks: { onSuccess, onFail },
});

expect(onFail.mock.calls).toHaveLength(1);
expect(mockDestroy.mock.calls).toHaveLength(1);
});
});
40 changes: 21 additions & 19 deletions packages/cli/src/swc/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,26 +156,28 @@ async function initialCompilation(
handleCopy(file, outDir, stripLeadingPaths)
)
),
]).then(([compiled, copied]) => {
compiled.forEach((result, index) => {
const filename = compilable[index];
if (result.status === "fulfilled") {
results.set(filename, result.value);
} else {
results.set(filename, CompileStatus.Failed);
reasons.set(filename, result.reason.message);
}
});
])
.then(([compiled, copied]) => {
compiled.forEach((result, index) => {
const filename = compilable[index];
if (result.status === "fulfilled") {
results.set(filename, result.value);
} else {
results.set(filename, CompileStatus.Failed);
reasons.set(filename, result.reason.message);
}
});

copied.forEach((result, index) => {
const filename = copyable[index];
if (result.status === "fulfilled") {
results.set(filename, result.value);
} else {
results.set(filename, CompileStatus.Failed);
}
});
});
copied.forEach((result, index) => {
const filename = copyable[index];
if (result.status === "fulfilled") {
results.set(filename, result.value);
} else {
results.set(filename, CompileStatus.Failed);
}
});
})
.finally(() => workers.destroy());

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Add Promise.finally to the package lib

This package's build script runs tsc, but packages/cli/tsconfig.json only includes ES2019.Array, ES2020.Promise, and ES2015 in lib; that combination declares Promise.allSettled but not Promise.prototype.finally. With this new call the package no longer type-checks (Property 'finally' does not exist on type 'Promise<...>'), so the CLI build will fail until the lib is updated or the cleanup is expressed without finally.

Useful? React with 👍 / 👎.

}
const end = process.hrtime(start);

Expand Down
Loading