diff --git a/packages/cli/src/swc/__tests__/dirWorker.test.ts b/packages/cli/src/swc/__tests__/dirWorker.test.ts index 0cc4793..829c422 100644 --- a/packages/cli/src/swc/__tests__/dirWorker.test.ts +++ b/packages/cli/src/swc/__tests__/dirWorker.test.ts @@ -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 = { @@ -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"; @@ -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`, diff --git a/packages/cli/src/swc/compile.ts b/packages/cli/src/swc/compile.ts index 7b162c8..7441e14 100644 --- a/packages/cli/src/swc/compile.ts +++ b/packages/cli/src/swc/compile.ts @@ -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"; @@ -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; @@ -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 @@ -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([ diff --git a/packages/cli/src/swc/dirWorker.ts b/packages/cli/src/swc/dirWorker.ts index 4b5f8c4..33929f3 100644 --- a/packages/cli/src/swc/dirWorker.ts +++ b/packages/cli/src/swc/dirWorker.ts @@ -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;