Skip to content

Commit

Permalink
feat(cli): add --out-file-extension (with tests) (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsvgit authored Jan 28, 2024
1 parent 85546a5 commit 0e0d745
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
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}`
);
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

0 comments on commit 0e0d745

Please sign in to comment.