Skip to content

Commit 7ad6cbd

Browse files
authored
Revert "fix(cli): Fix configuration reuse issue in swcDir() by deep clone" (#104)
- Reverts #95 - Closes #103
1 parent 8846450 commit 7ad6cbd

File tree

4 files changed

+25
-74
lines changed

4 files changed

+25
-74
lines changed

.changeset/big-pillows-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@swc/cli": patch
3+
---
4+
5+
Revert "fix(cli): Fix configuration reuse issue in swcDir() by deep clone"

packages/cli/src/swc/dir.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { stderr } from "process";
55
import { format } from "util";
66
import { CompileStatus } from "./constants";
77
import { Callbacks, CliOptions } from "./options";
8-
import { exists, getDest, mapTsExt, deepClone } from "./util";
8+
import { exists, getDest, mapTsExt } from "./util";
99
import handleCompile from "./dirWorker";
1010
import {
1111
globSources,
@@ -401,16 +401,12 @@ export default async function dir({
401401
swcOptions: Options;
402402
callbacks?: Callbacks;
403403
}) {
404-
// Deep clone the options to ensure full isolation between multiple calls
405-
const clonedCliOptions = deepClone(cliOptions);
406-
const clonedSwcOptions = deepClone(swcOptions);
404+
const { watch } = cliOptions;
407405

408-
const { watch } = clonedCliOptions;
409-
410-
await beforeStartCompilation(clonedCliOptions);
411-
await initialCompilation(clonedCliOptions, clonedSwcOptions, callbacks);
406+
await beforeStartCompilation(cliOptions);
407+
await initialCompilation(cliOptions, swcOptions, callbacks);
412408

413409
if (watch) {
414-
await watchCompilation(clonedCliOptions, clonedSwcOptions, callbacks);
410+
await watchCompilation(cliOptions, swcOptions, callbacks);
415411
}
416412
}

packages/cli/src/swc/dirWorker.ts

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import slash from "slash";
22
import { dirname, relative } from "path";
33
import { CompileStatus } from "./constants";
4-
import { compile, getDest, mapDtsExt, mapTsExt, deepClone } from "./util";
4+
import { compile, getDest, mapDtsExt, mapTsExt } from "./util";
55
import { outputResult } from "./compile";
66

77
import type { Options } from "@swc/core";
@@ -15,45 +15,29 @@ export default async function handleCompile(opts: {
1515
swcOptions: Options;
1616
outFileExtension?: string;
1717
}) {
18-
// Create a deep clone of the options to prevent shared references
19-
const clonedOpts = deepClone(opts);
20-
2118
const dest = getDest(
22-
clonedOpts.filename,
23-
clonedOpts.outDir,
24-
clonedOpts.cliOptions.stripLeadingPaths,
25-
`.${clonedOpts.outFileExtension ?? mapTsExt(clonedOpts.filename)}`
19+
opts.filename,
20+
opts.outDir,
21+
opts.cliOptions.stripLeadingPaths,
22+
`.${opts.outFileExtension ?? mapTsExt(opts.filename)}`
2623
);
27-
const sourceFileName = slash(relative(dirname(dest), clonedOpts.filename));
28-
29-
// Create a fresh copy of the swcOptions
30-
const options = deepClone(clonedOpts.swcOptions);
24+
const sourceFileName = slash(relative(dirname(dest), opts.filename));
3125

32-
// Set sourceFileName in the options
33-
options.sourceFileName = sourceFileName;
26+
const options = { ...opts.swcOptions, sourceFileName };
3427

35-
// Ensure we have the right extension for output files
36-
// Instead of directly setting on module.outFileExtension (which might not exist in the type),
37-
// we'll pass it separately to the compile function
38-
39-
const result = await compile(
40-
clonedOpts.filename,
41-
options,
42-
clonedOpts.sync,
43-
dest
44-
);
28+
const result = await compile(opts.filename, options, opts.sync, dest);
4529

4630
if (result) {
4731
const destDts = getDest(
48-
clonedOpts.filename,
49-
clonedOpts.outDir,
50-
clonedOpts.cliOptions.stripLeadingPaths,
51-
`.${mapDtsExt(clonedOpts.filename)}`
32+
opts.filename,
33+
opts.outDir,
34+
opts.cliOptions.stripLeadingPaths,
35+
`.${mapDtsExt(opts.filename)}`
5236
);
5337
const destSourcemap = dest + ".map";
5438
await outputResult({
5539
output: result,
56-
sourceFile: clonedOpts.filename,
40+
sourceFile: opts.filename,
5741
destFile: dest,
5842
destDtsFile: destDts,
5943
destSourcemapFile: destSourcemap,

packages/cli/src/swc/util.ts

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,6 @@ import { mkdirSync, writeFileSync, promises } from "fs";
44
import { dirname, extname, join, relative } from "path";
55
import { stderr } from "process";
66

7-
/**
8-
* Deep clone an object to ensure no shared references
9-
* @param obj The object to clone
10-
* @returns A new deep-cloned object
11-
*/
12-
export function deepClone<T>(obj: T): T {
13-
if (obj === null || typeof obj !== "object") {
14-
return obj;
15-
}
16-
17-
if (Array.isArray(obj)) {
18-
return (obj.map(item => deepClone(item)) as unknown) as T;
19-
}
20-
21-
const result = {} as T;
22-
for (const key in obj) {
23-
if (Object.prototype.hasOwnProperty.call(obj, key)) {
24-
result[key] = deepClone(obj[key]);
25-
}
26-
}
27-
return result;
28-
}
29-
307
export async function exists(path: string): Promise<boolean> {
318
let pathExists = true;
329
try {
@@ -66,22 +43,11 @@ export async function compile(
6643
sync: boolean,
6744
outputPath: string | undefined
6845
): Promise<swc.Output | void> {
69-
// Deep clone the options to ensure we don't have any shared references
70-
opts = deepClone({
46+
opts = {
7147
...opts,
72-
});
73-
48+
};
7449
if (outputPath) {
7550
opts.outputPath = outputPath;
76-
77-
// Extract the extension from the output path to ensure module resolution uses it
78-
const ext = extname(outputPath);
79-
if (ext && opts.module && typeof opts.module === "object") {
80-
// Force the module to use the correct extension for import path resolution
81-
// This explicit setting helps ensure we don't reuse cached module config
82-
// @ts-ignore: Adding a custom property that might not be in the type definition
83-
opts.module.forcedOutputFileExtension = ext.slice(1); // Remove the leading dot
84-
}
8551
}
8652

8753
try {

0 commit comments

Comments
 (0)