Skip to content

Commit

Permalink
fix(cli): Fix output file for declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
escaton committed Jun 28, 2024
1 parent c224d5a commit c1bd889
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 29 deletions.
29 changes: 25 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,21 @@ describe("dirWorker", () => {
`${filename}.${DEFAULT_OUT_FILE_EXTENSION}`
)
);

expect(compileModule.outputResult).toHaveBeenCalledWith({
output: {
code: "code",
map: "map",
},
sourceFile: `${filename}.ts`,
destFile: `outDir/${filename}.${DEFAULT_OUT_FILE_EXTENSION}`,
destDtsFile: `outDir/${filename}.d.ts`,
destSourcemapFile: `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

0 comments on commit c1bd889

Please sign in to comment.