Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: .undefined file extension when interacting with dir directly #288

Merged
merged 3 commits into from
Jan 28, 2024
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
82 changes: 82 additions & 0 deletions src/swc/__tests__/dirWorker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Options } from "@swc/core";
import handleCompile from "../dirWorker";
import { CliOptions, DEFAULT_OUT_FILE_EXTENSION } from "../options";
import * as utilModule from '../util';

type HandleCompileOptions = {
cliOptions: CliOptions;
swcOptions: Options;
sync: false,
outDir: "outDir",
filename: string,
outFileExtension?: string;
}

const createHandleCompileOptions = (options?: Partial<HandleCompileOptions>): HandleCompileOptions => ({
cliOptions: {
outDir: "",
outFile: "",
filename: "",
stripLeadingPaths: false,
filenames: [],
sync: false,
workers: undefined,
sourceMapTarget: undefined,
extensions: [],
watch: false,
copyFiles: false,
outFileExtension: "",
includeDotfiles: false,
deleteDirOnStart: false,
quiet: true,
only: [],
ignore: [],
},
swcOptions: {},
sync: false,
outDir: "outDir",
filename: "",
...options,
});

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

describe("dirWorker", () => {
it('should call "compile" with the "DEFAULT_OUT_FILE_EXTENSION" when "outFileExtension" is undefined', async () => {
const filename = 'test';
const options = createHandleCompileOptions({
filename: `${filename}.ts`
});

try {
await handleCompile(options);
} catch (err) {
// We don't care about the error in this test, we want to make sure that "compile" was called
}

// Assert that subFunction was called with the correct parameter
expect(utilModule.compile).toHaveBeenCalledWith(options.filename, { sourceFileName: `../${options.filename}`}, options.sync, `${options.outDir}/${filename}.${DEFAULT_OUT_FILE_EXTENSION}`);
});
});

describe("dirWorker", () => {
it('should call "compile" with "outFileExtension" when undefined', async () => {
const filename = 'test';
const options = createHandleCompileOptions({
filename: `${filename}.ts`,
outFileExtension: 'cjs'
});

try {
await handleCompile(options);
} catch (err) {
// We don't care about the error in this test, we want to make sure that "compile" was called
}

// Assert that subFunction was called with the correct parameter
expect(utilModule.compile).toHaveBeenCalledWith(options.filename, { sourceFileName: `../${options.filename}`}, options.sync, `${options.outDir}/${filename}.${options.outFileExtension}`);
});
});
2 changes: 1 addition & 1 deletion src/swc/__tests__/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe("parserArgs", () => {
expect(result).toEqual(expectedOptions);
});

it("provides the a sensible default", () => {
it("provides a sensible default", () => {
const args = ["node", "/path/to/node_modules/swc-cli/bin/swc.js", "src"];
const result = parserArgs(args);
expect(result.cliOptions.outFileExtension).toEqual("js");
Expand Down
5 changes: 3 additions & 2 deletions src/swc/dirWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ import { outputResult } from "./compile";

import type { Options } from "@swc/core";
import type { CliOptions } from "./options";
import { DEFAULT_OUT_FILE_EXTENSION } from "./options";

export default async function handleCompile(opts: {
filename: string;
outDir: string;
sync: boolean;
cliOptions: CliOptions;
swcOptions: Options;
outFileExtension: string;
outFileExtension?: string;
}) {
const dest = getDest(
opts.filename,
opts.outDir,
opts.cliOptions.stripLeadingPaths,
`.${opts.outFileExtension}`
`.${opts.outFileExtension ?? DEFAULT_OUT_FILE_EXTENSION}`
);
const sourceFileName = slash(relative(dirname(dest), opts.filename));

Expand Down
5 changes: 3 additions & 2 deletions src/swc/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Options } from "@swc/core";
const pkg = require("../../package.json");

let program: commander.Command;
export const DEFAULT_OUT_FILE_EXTENSION = "js";

export const initProgram = () => {
program = new commander.Command();
Expand Down Expand Up @@ -84,7 +85,7 @@ export const initProgram = () => {
program.option(
"--out-file-extension [string]",
"Use a specific extension for the output files [default: js]",
"js"
DEFAULT_OUT_FILE_EXTENSION,
);

program.option(
Expand Down Expand Up @@ -302,7 +303,7 @@ export default function parserArgs(args: string[]) {
extensions: opts.extensions || DEFAULT_EXTENSIONS,
watch: !!opts.watch,
copyFiles: !!opts.copyFiles,
outFileExtension: opts.outFileExtension || "js",
outFileExtension: opts.outFileExtension || DEFAULT_OUT_FILE_EXTENSION,
includeDotfiles: !!opts.includeDotfiles,
deleteDirOnStart: Boolean(opts.deleteDirOnStart),
quiet: !!opts.quiet,
Expand Down
Loading