Skip to content

Commit 2d6d34d

Browse files
authored
feat: Map typescript input extensions (#85)
will closes swc-project/swc#9800 It seems that swc doesn't rewrite relative import extensions: swc-project/swc#9611, so I think this is a safe change. If I'm missing something please tell me and I will do corresponding changes.
1 parent 259271f commit 2d6d34d

File tree

6 files changed

+45
-30
lines changed

6 files changed

+45
-30
lines changed

packages/cli/src/swc/__tests__/dirWorker.test.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Options } from "@swc/core";
22
import handleCompile from "../dirWorker";
3-
import { CliOptions, DEFAULT_OUT_FILE_EXTENSION } from "../options";
3+
import { CliOptions } from "../options";
44
import * as utilModule from "../util";
55
import * as compileModule from "../compile";
66
import path from "path";
@@ -59,7 +59,7 @@ beforeEach(() => {
5959
});
6060

6161
describe("dirWorker", () => {
62-
it('should call "compile" with the "DEFAULT_OUT_FILE_EXTENSION" when "outFileExtension" is undefined', async () => {
62+
it('should call "compile" with the corresponding extension when "outFileExtension" is undefined', async () => {
6363
const filename = "test";
6464
const options = createHandleCompileOptions({
6565
filename: `${filename}.ts`,
@@ -78,7 +78,7 @@ describe("dirWorker", () => {
7878
options.sync,
7979
path.join(
8080
options.outDir,
81-
`${filename}.${DEFAULT_OUT_FILE_EXTENSION}`
81+
`${filename}.${utilModule.mapTsExt(options.filename)}`
8282
)
8383
);
8484

@@ -90,12 +90,15 @@ describe("dirWorker", () => {
9090
sourceFile: `${filename}.ts`,
9191
destFile: path.join(
9292
options.outDir,
93-
`${filename}.${DEFAULT_OUT_FILE_EXTENSION}`
93+
`${filename}.${utilModule.mapTsExt(filename)}`
94+
),
95+
destDtsFile: path.join(
96+
options.outDir,
97+
`${filename}.${utilModule.mapDtsExt(filename)}`
9498
),
95-
destDtsFile: path.join(options.outDir, `${filename}.d.ts`),
9699
destSourcemapFile: path.join(
97100
options.outDir,
98-
`${filename}.${DEFAULT_OUT_FILE_EXTENSION}.map`
101+
`${filename}.${utilModule.mapTsExt(filename)}.map`
99102
),
100103
options: { sourceFileName: `../${options.filename}` },
101104
});

packages/cli/src/swc/__tests__/options.test.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const createDefaultResult = (): ParserArgsReturn => ({
3737
outDir: undefined,
3838
// @ts-expect-error
3939
outFile: undefined,
40-
outFileExtension: "js",
40+
outFileExtension: undefined,
4141
quiet: false,
4242
sourceMapTarget: undefined,
4343
stripLeadingPaths: false,
@@ -88,16 +88,6 @@ describe("parserArgs", () => {
8888
});
8989
expect(result).toEqual(expectedOptions);
9090
});
91-
92-
it("provides a sensible default", () => {
93-
const args = [
94-
"node",
95-
"/path/to/node_modules/swc-cli/bin/swc.js",
96-
"src",
97-
];
98-
const result = parserArgs(args);
99-
expect(result!.cliOptions.outFileExtension).toEqual("js");
100-
});
10191
});
10292

10393
describe("errors", () => {

packages/cli/src/swc/dir.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { stderr } from "process";
55
import { format } from "util";
66
import { CompileStatus } from "./constants";
77
import { Callbacks, CliOptions } from "./options";
8-
import { exists, getDest } from "./util";
8+
import { exists, getDest, mapTsExt } from "./util";
99
import handleCompile from "./dirWorker";
1010
import {
1111
globSources,
@@ -270,13 +270,18 @@ async function watchCompilation(
270270
try {
271271
if (isCompilableExtension(filename, extensions)) {
272272
await unlink(
273-
getDest(filename, outDir, stripLeadingPaths, ".js")
273+
getDest(
274+
filename,
275+
outDir,
276+
stripLeadingPaths,
277+
`.${mapTsExt(filename)}`
278+
)
274279
);
275280
const sourcemapPath = getDest(
276281
filename,
277282
outDir,
278283
stripLeadingPaths,
279-
".js.map"
284+
`.${mapTsExt(filename)}.map`
280285
);
281286
const sourcemapExists = await exists(sourcemapPath);
282287
if (sourcemapExists) {

packages/cli/src/swc/dirWorker.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import slash from "slash";
22
import { dirname, relative } from "path";
33
import { CompileStatus } from "./constants";
4-
import { compile, getDest } from "./util";
4+
import { compile, getDest, mapDtsExt, mapTsExt } from "./util";
55
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";
109

1110
export default async function handleCompile(opts: {
1211
filename: string;
@@ -20,7 +19,7 @@ export default async function handleCompile(opts: {
2019
opts.filename,
2120
opts.outDir,
2221
opts.cliOptions.stripLeadingPaths,
23-
`.${opts.outFileExtension ?? DEFAULT_OUT_FILE_EXTENSION}`
22+
`.${opts.outFileExtension ?? mapTsExt(opts.filename)}`
2423
);
2524
const sourceFileName = slash(relative(dirname(dest), opts.filename));
2625

@@ -33,7 +32,7 @@ export default async function handleCompile(opts: {
3332
opts.filename,
3433
opts.outDir,
3534
opts.cliOptions.stripLeadingPaths,
36-
`.d.ts`
35+
`.${mapDtsExt(opts.filename)}`
3736
);
3837
const destSourcemap = dest + ".map";
3938
await outputResult({

packages/cli/src/swc/options.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const DEFAULT_EXTENSIONS = [
1919
const pkg = require("../../package.json");
2020

2121
let program: Command;
22-
export const DEFAULT_OUT_FILE_EXTENSION = "js";
2322

2423
export const initProgram = () => {
2524
program = new commander.Command();
@@ -104,8 +103,7 @@ export const initProgram = () => {
104103

105104
program.option(
106105
"--out-file-extension [string]",
107-
"Use a specific extension for the output files [default: js]",
108-
DEFAULT_OUT_FILE_EXTENSION
106+
"Use a specific extension for the output files"
109107
);
110108

111109
program.option(
@@ -267,7 +265,7 @@ export interface CliOptions {
267265
readonly extensions: string[];
268266
readonly watch: boolean;
269267
readonly copyFiles: boolean;
270-
readonly outFileExtension: string;
268+
readonly outFileExtension?: string;
271269
readonly includeDotfiles: boolean;
272270
readonly deleteDirOnStart: boolean;
273271
readonly quiet: boolean;
@@ -407,7 +405,7 @@ export default function parserArgs(args: string[]) {
407405
extensions: opts.extensions || DEFAULT_EXTENSIONS,
408406
watch: !!opts.watch,
409407
copyFiles: !!opts.copyFiles,
410-
outFileExtension: opts.outFileExtension || DEFAULT_OUT_FILE_EXTENSION,
408+
outFileExtension: opts.outFileExtension,
411409
includeDotfiles: !!opts.includeDotfiles,
412410
deleteDirOnStart: Boolean(opts.deleteDirOnStart),
413411
quiet: !!opts.quiet,

packages/cli/src/swc/util.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as swc from "@swc/core";
22
import slash from "slash";
33
import { mkdirSync, writeFileSync, promises } from "fs";
4-
import { dirname, join, relative } from "path";
4+
import { dirname, extname, join, relative } from "path";
55
import { stderr } from "process";
66

77
export async function exists(path: string): Promise<boolean> {
@@ -158,3 +158,23 @@ export function getDest(
158158
}
159159
return join(outDir, base);
160160
}
161+
162+
export function mapTsExt(filename: string) {
163+
return (
164+
{
165+
".ts": "js",
166+
".mts": "mjs",
167+
".cts": "cjs",
168+
}[extname(filename)] ?? "js"
169+
);
170+
}
171+
172+
export function mapDtsExt(filename: string) {
173+
return (
174+
{
175+
".ts": "d.ts",
176+
".mts": "d.mts",
177+
".cts": "d.cts",
178+
}[extname(filename)] ?? "d.ts"
179+
);
180+
}

0 commit comments

Comments
 (0)