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

feat(cli): add --out-file-extension (with tests) #286

Merged
merged 1 commit 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
24 changes: 24 additions & 0 deletions src/swc/__tests__/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const createDefaultResult = (): ParserArgsReturn => ({
outDir: undefined,
// @ts-expect-error
outFile: undefined,
outFileExtension: "js",
quiet: false,
sourceMapTarget: undefined,
stripLeadingPaths: false,
Expand Down Expand Up @@ -63,6 +64,29 @@ describe("parserArgs", () => {
expect(result).toEqual(defaultResult);
});

describe("--out-file-extension", () => {
it("provides the extension in the parsed args", () => {
const args = [
"node",
"/path/to/node_modules/swc-cli/bin/swc.js",
"src",
"--out-file-extension",
"magic_custom_extension",
];
const result = parserArgs(args);
const expectedOptions = deepmerge(defaultResult, {
cliOptions: { outFileExtension: "magic_custom_extension" },
});
expect(result).toEqual(expectedOptions);
});

it("provides the 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");
});
});

describe("errors", () => {
let mockExit: jest.SpyInstance;
let mockConsoleError: jest.SpyInstance;
Expand Down
13 changes: 12 additions & 1 deletion src/swc/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ async function initialCompilation(cliOptions: CliOptions, swcOptions: Options) {
copyFiles,
extensions,
outDir,
outFileExtension,
stripLeadingPaths,
sync,
quiet,
Expand Down Expand Up @@ -92,6 +93,7 @@ async function initialCompilation(cliOptions: CliOptions, swcOptions: Options) {
sync,
cliOptions,
swcOptions,
outFileExtension,
});
results.set(filename, result);
} catch (err: any) {
Expand Down Expand Up @@ -119,7 +121,14 @@ async function initialCompilation(cliOptions: CliOptions, swcOptions: Options) {
Promise.allSettled(
compilable.map(filename =>
workers
.run({ filename, outDir, sync, cliOptions, swcOptions })
.run({
filename,
outDir,
sync,
cliOptions,
swcOptions,
outFileExtension,
})
.catch(err => {
console.error(err.message);
throw err;
Expand Down Expand Up @@ -208,6 +217,7 @@ async function watchCompilation(cliOptions: CliOptions, swcOptions: Options) {
extensions,
outDir,
stripLeadingPaths,
outFileExtension,
quiet,
sync,
} = cliOptions;
Expand Down Expand Up @@ -252,6 +262,7 @@ async function watchCompilation(cliOptions: CliOptions, swcOptions: Options) {
sync,
cliOptions,
swcOptions,
outFileExtension,
});
if (!quiet && result === CompileStatus.Compiled) {
const end = process.hrtime(start);
Expand Down
3 changes: 2 additions & 1 deletion src/swc/dirWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ export default async function handleCompile(opts: {
sync: boolean;
cliOptions: CliOptions;
swcOptions: Options;
outFileExtension: string;
}) {
const dest = getDest(
opts.filename,
opts.outDir,
opts.cliOptions.stripLeadingPaths,
".js"
`.${opts.outFileExtension}`
Copy link
Contributor Author

@dsvgit dsvgit Jan 28, 2024

Choose a reason for hiding this comment

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

I checked how babel cli works. It takes extension already starts with a dot. But according to this PR swc-project/swc#4343 native swc cli takes it without a dot.
So only when passing it to getDest we add dot: .${opts.outFileExtension}

);
const sourceFileName = slash(relative(dirname(dest), opts.filename));

Expand Down
7 changes: 7 additions & 0 deletions src/swc/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export const initProgram = () => {
"Compile an input directory of modules into an output directory"
);

program.option(
"--out-file-extension [string]",
"Use a specific extension for the output files [default: js]"
);

program.option(
"-D, --copy-files",
"When compiling a directory copy over non-compilable files"
Expand Down Expand Up @@ -176,6 +181,7 @@ export interface CliOptions {
readonly extensions: string[];
readonly watch: boolean;
readonly copyFiles: boolean;
readonly outFileExtension: string;
readonly includeDotfiles: boolean;
readonly deleteDirOnStart: boolean;
readonly quiet: boolean;
Expand Down Expand Up @@ -294,6 +300,7 @@ export default function parserArgs(args: string[]) {
extensions: opts.extensions || DEFAULT_EXTENSIONS,
watch: !!opts.watch,
copyFiles: !!opts.copyFiles,
outFileExtension: opts.outFileExtension || "js",
includeDotfiles: !!opts.includeDotfiles,
deleteDirOnStart: Boolean(opts.deleteDirOnStart),
quiet: !!opts.quiet,
Expand Down
Loading