diff --git a/.changeset/quick-pumas-retire.md b/.changeset/quick-pumas-retire.md new file mode 100644 index 0000000..8bdb3d0 --- /dev/null +++ b/.changeset/quick-pumas-retire.md @@ -0,0 +1,5 @@ +--- +"@swc/cli": patch +--- + +fix(cli): Destroy the worker pool after the initial compilation diff --git a/packages/cli/src/swc/__tests__/dir.test.ts b/packages/cli/src/swc/__tests__/dir.test.ts new file mode 100644 index 0000000..4e2d326 --- /dev/null +++ b/packages/cli/src/swc/__tests__/dir.test.ts @@ -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); + }); +}); diff --git a/packages/cli/src/swc/dir.ts b/packages/cli/src/swc/dir.ts index 78733ca..a3fb040 100644 --- a/packages/cli/src/swc/dir.ts +++ b/packages/cli/src/swc/dir.ts @@ -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()); } const end = process.hrtime(start);