Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit bcba7fc

Browse files
authored
fix: .undefined file extension when interacting with dir directly (#288)
1 parent 8766bcb commit bcba7fc

File tree

4 files changed

+89
-5
lines changed

4 files changed

+89
-5
lines changed

src/swc/__tests__/dirWorker.test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { Options } from "@swc/core";
2+
import handleCompile from "../dirWorker";
3+
import { CliOptions, DEFAULT_OUT_FILE_EXTENSION } from "../options";
4+
import * as utilModule from '../util';
5+
6+
type HandleCompileOptions = {
7+
cliOptions: CliOptions;
8+
swcOptions: Options;
9+
sync: false,
10+
outDir: "outDir",
11+
filename: string,
12+
outFileExtension?: string;
13+
}
14+
15+
const createHandleCompileOptions = (options?: Partial<HandleCompileOptions>): HandleCompileOptions => ({
16+
cliOptions: {
17+
outDir: "",
18+
outFile: "",
19+
filename: "",
20+
stripLeadingPaths: false,
21+
filenames: [],
22+
sync: false,
23+
workers: undefined,
24+
sourceMapTarget: undefined,
25+
extensions: [],
26+
watch: false,
27+
copyFiles: false,
28+
outFileExtension: "",
29+
includeDotfiles: false,
30+
deleteDirOnStart: false,
31+
quiet: true,
32+
only: [],
33+
ignore: [],
34+
},
35+
swcOptions: {},
36+
sync: false,
37+
outDir: "outDir",
38+
filename: "",
39+
...options,
40+
});
41+
42+
jest.mock('../util', () => ({
43+
...jest.requireActual("../util"),
44+
compile: jest.fn(),
45+
}));
46+
47+
describe("dirWorker", () => {
48+
it('should call "compile" with the "DEFAULT_OUT_FILE_EXTENSION" when "outFileExtension" is undefined', async () => {
49+
const filename = 'test';
50+
const options = createHandleCompileOptions({
51+
filename: `${filename}.ts`
52+
});
53+
54+
try {
55+
await handleCompile(options);
56+
} catch (err) {
57+
// We don't care about the error in this test, we want to make sure that "compile" was called
58+
}
59+
60+
// Assert that subFunction was called with the correct parameter
61+
expect(utilModule.compile).toHaveBeenCalledWith(options.filename, { sourceFileName: `../${options.filename}`}, options.sync, `${options.outDir}/${filename}.${DEFAULT_OUT_FILE_EXTENSION}`);
62+
});
63+
});
64+
65+
describe("dirWorker", () => {
66+
it('should call "compile" with "outFileExtension" when undefined', async () => {
67+
const filename = 'test';
68+
const options = createHandleCompileOptions({
69+
filename: `${filename}.ts`,
70+
outFileExtension: 'cjs'
71+
});
72+
73+
try {
74+
await handleCompile(options);
75+
} catch (err) {
76+
// We don't care about the error in this test, we want to make sure that "compile" was called
77+
}
78+
79+
// Assert that subFunction was called with the correct parameter
80+
expect(utilModule.compile).toHaveBeenCalledWith(options.filename, { sourceFileName: `../${options.filename}`}, options.sync, `${options.outDir}/${filename}.${options.outFileExtension}`);
81+
});
82+
});

src/swc/__tests__/options.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe("parserArgs", () => {
8080
expect(result).toEqual(expectedOptions);
8181
});
8282

83-
it("provides the a sensible default", () => {
83+
it("provides a sensible default", () => {
8484
const args = ["node", "/path/to/node_modules/swc-cli/bin/swc.js", "src"];
8585
const result = parserArgs(args);
8686
expect(result.cliOptions.outFileExtension).toEqual("js");

src/swc/dirWorker.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@ import { outputResult } from "./compile";
66

77
import type { Options } from "@swc/core";
88
import type { CliOptions } from "./options";
9+
import { DEFAULT_OUT_FILE_EXTENSION } from "./options";
910

1011
export default async function handleCompile(opts: {
1112
filename: string;
1213
outDir: string;
1314
sync: boolean;
1415
cliOptions: CliOptions;
1516
swcOptions: Options;
16-
outFileExtension: string;
17+
outFileExtension?: string;
1718
}) {
1819
const dest = getDest(
1920
opts.filename,
2021
opts.outDir,
2122
opts.cliOptions.stripLeadingPaths,
22-
`.${opts.outFileExtension}`
23+
`.${opts.outFileExtension ?? DEFAULT_OUT_FILE_EXTENSION}`
2324
);
2425
const sourceFileName = slash(relative(dirname(dest), opts.filename));
2526

src/swc/options.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Options } from "@swc/core";
55
const pkg = require("../../package.json");
66

77
let program: commander.Command;
8+
export const DEFAULT_OUT_FILE_EXTENSION = "js";
89

910
export const initProgram = () => {
1011
program = new commander.Command();
@@ -84,7 +85,7 @@ export const initProgram = () => {
8485
program.option(
8586
"--out-file-extension [string]",
8687
"Use a specific extension for the output files [default: js]",
87-
"js"
88+
DEFAULT_OUT_FILE_EXTENSION,
8889
);
8990

9091
program.option(
@@ -302,7 +303,7 @@ export default function parserArgs(args: string[]) {
302303
extensions: opts.extensions || DEFAULT_EXTENSIONS,
303304
watch: !!opts.watch,
304305
copyFiles: !!opts.copyFiles,
305-
outFileExtension: opts.outFileExtension || "js",
306+
outFileExtension: opts.outFileExtension || DEFAULT_OUT_FILE_EXTENSION,
306307
includeDotfiles: !!opts.includeDotfiles,
307308
deleteDirOnStart: Boolean(opts.deleteDirOnStart),
308309
quiet: !!opts.quiet,

0 commit comments

Comments
 (0)