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(cli): Fix output file for declarations #42

Merged
merged 3 commits into from
Jun 29, 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
5 changes: 5 additions & 0 deletions .changeset/neat-kiwis-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@swc/cli": minor
---

Fix destination filename for .d.ts
35 changes: 31 additions & 4 deletions packages/cli/src/swc/__tests__/dirWorker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Options } from "@swc/core";
import handleCompile from "../dirWorker";
import { CliOptions, DEFAULT_OUT_FILE_EXTENSION } from "../options";
import * as utilModule from "../util";
import * as compileModule from "../compile";
import path from "path";

type HandleCompileOptions = {
Expand Down Expand Up @@ -44,9 +45,19 @@ const createHandleCompileOptions = (

jest.mock("../util", () => ({
...jest.requireActual("../util"),
compile: jest.fn(),
compile: jest
.fn()
.mockReturnValue(Promise.resolve({ code: "code", map: "map" })),
}));

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

beforeEach(() => {
jest.clearAllMocks();
});

describe("dirWorker", () => {
it('should call "compile" with the "DEFAULT_OUT_FILE_EXTENSION" when "outFileExtension" is undefined', async () => {
const filename = "test";
Expand All @@ -70,11 +81,27 @@ describe("dirWorker", () => {
`${filename}.${DEFAULT_OUT_FILE_EXTENSION}`
)
);

expect(compileModule.outputResult).toHaveBeenCalledWith({
output: {
code: "code",
map: "map",
},
sourceFile: `${filename}.ts`,
destFile: path.join(
options.outDir,
`${filename}.${DEFAULT_OUT_FILE_EXTENSION}`
),
destDtsFile: path.join(options.outDir, `${filename}.d.ts`),
destSourcemapFile: path.join(
options.outDir,
`${filename}.${DEFAULT_OUT_FILE_EXTENSION}.map`
),
options: { sourceFileName: `../${options.filename}` },
});
});
});

describe("dirWorker", () => {
it('should call "compile" with "outFileExtension" when undefined', async () => {
it('should call "compile" with "outFileExtension" when it is set in options', async () => {
const filename = "test";
const options = createHandleCompileOptions({
filename: `${filename}.ts`,
Expand Down
51 changes: 27 additions & 24 deletions packages/cli/src/swc/compile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import slash from "slash";
import { promises } from "fs";
import { dirname, relative, basename, join } from "path";
import { dirname, relative } from "path";
import { transformFile, transformFileSync } from "@swc/core";
import type { Options, Output } from "@swc/core";

Expand All @@ -9,7 +9,7 @@ const { mkdir, stat, writeFile } = promises;
function withSourceMap(
output: Output,
options: Options,
destFile: string,
sourceMapFile: string,
destDir: string
) {
let dts: string | undefined;
Expand All @@ -23,17 +23,10 @@ function withSourceMap(
}
}

let dtsPath: string | undefined;

if (dts) {
dtsPath = join(destDir, basename(destFile) + ".d.ts");
}

if (!output.map || options.sourceMaps === "inline") {
return {
sourceCode: output.code,
dts,
dtsPath,
};
}
// TODO: remove once fixed in core https://github.com/swc-project/swc/issues/1388
Expand All @@ -46,39 +39,49 @@ function withSourceMap(
}
output.map = JSON.stringify(sourceMap);

const sourceMapPath = destFile + ".map";
output.code += `\n//# sourceMappingURL=${slash(
relative(destDir, sourceMapPath)
relative(destDir, sourceMapFile)
)}`;

return {
sourceMap: output.map,
sourceMapPath,
sourceCode: output.code,
dts,
dtsPath,
};
}

export async function outputResult(
output: Output,
sourceFile: string,
destFile: string,
options: Options
) {
export async function outputResult({
output,
sourceFile,
destFile,
destDtsFile,
destSourcemapFile,
options,
}: {
output: Output;
sourceFile: string;
destFile: string;
destDtsFile: string;
destSourcemapFile: string;
options: Options;
}) {
const destDir = dirname(destFile);

const { sourceMap, sourceMapPath, sourceCode, dts, dtsPath } =
withSourceMap(output, options, destFile, destDir);
const { sourceMap, sourceCode, dts } = withSourceMap(
output,
options,
destSourcemapFile,
destDir
);

await mkdir(destDir, { recursive: true });
const { mode } = await stat(sourceFile);

const dtsPromise = dts
? writeFile(dtsPath!, dts, { mode })
? writeFile(destDtsFile, dts, { mode })
: Promise.resolve();
const sourceMapPromise = sourceMapPath
? writeFile(sourceMapPath, sourceMap!, { mode })
const sourceMapPromise = sourceMap
? writeFile(destSourcemapFile, sourceMap, { mode })
: Promise.resolve();

await Promise.all([
Expand Down
16 changes: 15 additions & 1 deletion packages/cli/src/swc/dirWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,21 @@ export default async function handleCompile(opts: {
const result = await compile(opts.filename, options, opts.sync, dest);

if (result) {
await outputResult(result, opts.filename, dest, options);
const destDts = getDest(
opts.filename,
opts.outDir,
opts.cliOptions.stripLeadingPaths,
`.d.ts`
);
const destSourcemap = dest + ".map";
await outputResult({
output: result,
sourceFile: opts.filename,
destFile: dest,
destDtsFile: destDts,
destSourcemapFile: destSourcemap,
options,
});
return CompileStatus.Compiled;
} else {
return CompileStatus.Omitted;
Expand Down
Loading