Skip to content

Commit

Permalink
Map input extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
CPunisher committed Feb 23, 2025
1 parent 259271f commit ef98308
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 30 deletions.
15 changes: 9 additions & 6 deletions packages/cli/src/swc/__tests__/dirWorker.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Options } from "@swc/core";
import handleCompile from "../dirWorker";
import { CliOptions, DEFAULT_OUT_FILE_EXTENSION } from "../options";
import { CliOptions } from "../options";
import * as utilModule from "../util";
import * as compileModule from "../compile";
import path from "path";
Expand Down Expand Up @@ -59,7 +59,7 @@ beforeEach(() => {
});

describe("dirWorker", () => {
it('should call "compile" with the "DEFAULT_OUT_FILE_EXTENSION" when "outFileExtension" is undefined', async () => {
it('should call "compile" with the corresponding extension when "outFileExtension" is undefined', async () => {
const filename = "test";
const options = createHandleCompileOptions({
filename: `${filename}.ts`,
Expand All @@ -78,7 +78,7 @@ describe("dirWorker", () => {
options.sync,
path.join(
options.outDir,
`${filename}.${DEFAULT_OUT_FILE_EXTENSION}`
`${filename}.${utilModule.mapTsExt(options.filename)}`
)
);

Expand All @@ -90,12 +90,15 @@ describe("dirWorker", () => {
sourceFile: `${filename}.ts`,
destFile: path.join(
options.outDir,
`${filename}.${DEFAULT_OUT_FILE_EXTENSION}`
`${filename}.${utilModule.mapTsExt(filename)}`
),
destDtsFile: path.join(
options.outDir,
`${filename}.${utilModule.mapDtsExt(filename)}`
),
destDtsFile: path.join(options.outDir, `${filename}.d.ts`),
destSourcemapFile: path.join(
options.outDir,
`${filename}.${DEFAULT_OUT_FILE_EXTENSION}.map`
`${filename}.${utilModule.mapTsExt(filename)}.map`
),
options: { sourceFileName: `../${options.filename}` },
});
Expand Down
12 changes: 1 addition & 11 deletions packages/cli/src/swc/__tests__/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const createDefaultResult = (): ParserArgsReturn => ({
outDir: undefined,
// @ts-expect-error
outFile: undefined,
outFileExtension: "js",
outFileExtension: undefined,
quiet: false,
sourceMapTarget: undefined,
stripLeadingPaths: false,
Expand Down Expand Up @@ -88,16 +88,6 @@ describe("parserArgs", () => {
});
expect(result).toEqual(expectedOptions);
});

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");
});
});

describe("errors", () => {
Expand Down
11 changes: 8 additions & 3 deletions packages/cli/src/swc/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { stderr } from "process";
import { format } from "util";
import { CompileStatus } from "./constants";
import { Callbacks, CliOptions } from "./options";
import { exists, getDest } from "./util";
import { exists, getDest, mapTsExt } from "./util";
import handleCompile from "./dirWorker";
import {
globSources,
Expand Down Expand Up @@ -270,13 +270,18 @@ async function watchCompilation(
try {
if (isCompilableExtension(filename, extensions)) {
await unlink(
getDest(filename, outDir, stripLeadingPaths, ".js")
getDest(
filename,
outDir,
stripLeadingPaths,
`.${mapTsExt(filename)}`
)
);
const sourcemapPath = getDest(
filename,
outDir,
stripLeadingPaths,
".js.map"
`.${mapTsExt(filename)}.map`
);
const sourcemapExists = await exists(sourcemapPath);
if (sourcemapExists) {
Expand Down
7 changes: 3 additions & 4 deletions packages/cli/src/swc/dirWorker.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import slash from "slash";
import { dirname, relative } from "path";
import { CompileStatus } from "./constants";
import { compile, getDest } from "./util";
import { compile, getDest, mapDtsExt, mapTsExt } from "./util";
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;
Expand All @@ -20,7 +19,7 @@ export default async function handleCompile(opts: {
opts.filename,
opts.outDir,
opts.cliOptions.stripLeadingPaths,
`.${opts.outFileExtension ?? DEFAULT_OUT_FILE_EXTENSION}`
`.${opts.outFileExtension ?? mapTsExt(opts.filename)}`
);
const sourceFileName = slash(relative(dirname(dest), opts.filename));

Expand All @@ -33,7 +32,7 @@ export default async function handleCompile(opts: {
opts.filename,
opts.outDir,
opts.cliOptions.stripLeadingPaths,
`.d.ts`
`.${mapDtsExt(opts.filename)}`
);
const destSourcemap = dest + ".map";
await outputResult({
Expand Down
8 changes: 3 additions & 5 deletions packages/cli/src/swc/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const DEFAULT_EXTENSIONS = [
const pkg = require("../../package.json");

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

export const initProgram = () => {
program = new commander.Command();
Expand Down Expand Up @@ -104,8 +103,7 @@ export const initProgram = () => {

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

program.option(
Expand Down Expand Up @@ -267,7 +265,7 @@ export interface CliOptions {
readonly extensions: string[];
readonly watch: boolean;
readonly copyFiles: boolean;
readonly outFileExtension: string;
readonly outFileExtension?: string;
readonly includeDotfiles: boolean;
readonly deleteDirOnStart: boolean;
readonly quiet: boolean;
Expand Down Expand Up @@ -407,7 +405,7 @@ export default function parserArgs(args: string[]) {
extensions: opts.extensions || DEFAULT_EXTENSIONS,
watch: !!opts.watch,
copyFiles: !!opts.copyFiles,
outFileExtension: opts.outFileExtension || DEFAULT_OUT_FILE_EXTENSION,
outFileExtension: opts.outFileExtension,
includeDotfiles: !!opts.includeDotfiles,
deleteDirOnStart: Boolean(opts.deleteDirOnStart),
quiet: !!opts.quiet,
Expand Down
22 changes: 21 additions & 1 deletion packages/cli/src/swc/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as swc from "@swc/core";
import slash from "slash";
import { mkdirSync, writeFileSync, promises } from "fs";
import { dirname, join, relative } from "path";
import { dirname, extname, join, relative } from "path";
import { stderr } from "process";

export async function exists(path: string): Promise<boolean> {
Expand Down Expand Up @@ -158,3 +158,23 @@ export function getDest(
}
return join(outDir, base);
}

export function mapTsExt(filename: string) {
return (
{
".ts": "js",
".mts": "mjs",
".cts": "cjs",
}[extname(filename)] ?? "js"
);
}

export function mapDtsExt(filename: string) {
return (
{
".ts": "d.ts",
".mts": "d.mts",
".cts": "d.cts",
}[extname(filename)] ?? "d.ts"
);
}

0 comments on commit ef98308

Please sign in to comment.