Skip to content

Commit

Permalink
feat(cli): Support jsc.experimental.emitIsolatedDts (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Jun 22, 2024
1 parent f035da2 commit bf04342
Show file tree
Hide file tree
Showing 6 changed files with 403 additions and 93 deletions.
6 changes: 6 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @swc/cli

## 0.3.13

### Patch Changes

- 9eb0a67: Support emitting '.d.ts' files

## 0.3.10

### Patch Changes
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swc/cli",
"version": "0.3.12",
"version": "0.3.13",
"description": "CLI for the swc project",
"main": "lib/swc/index.js",
"scripts": {
Expand Down Expand Up @@ -56,9 +56,10 @@
"source-map": "^0.7.3"
},
"devDependencies": {
"@swc/cli": "=0.3.6",
"@swc/core": "^1.3.107",
"@swc/cli": "=0.3.12",
"@swc/core": "^1.6.4",
"@swc/jest": "workspace:^",
"@swc/types": "^0.1.9",
"@types/jest": "^29.5.0",
"@types/node": "^20.11.5",
"@types/semver": "^7.3.13",
Expand Down
49 changes: 34 additions & 15 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 } from "path";
import { dirname, relative, basename, join } from "path";
import { transformFile, transformFileSync } from "@swc/core";
import type { Options, Output } from "@swc/core";

Expand Down Expand Up @@ -32,10 +32,29 @@ function withSourceMap(
relative(destDir, sourceMapPath)
)}`;

let dts: string | undefined;

// TODO: Remove once fixed in core
if ((output as any).output) {
const json = JSON.parse((output as any).output);

if (json.__swc_isolated_declarations__) {
dts = json.__swc_isolated_declarations__;
}
}

let dtsPath: string | undefined;

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

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

Expand All @@ -47,24 +66,24 @@ export async function outputResult(
) {
const destDir = dirname(destFile);

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

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

if (!sourceMapPath) {
await writeFile(destFile, sourceCode, { mode });
} else {
await Promise.all([
writeFile(destFile, sourceCode, { mode }),
writeFile(sourceMapPath, sourceMap!, { mode }),
]);
}
const dtsPromise = dts
? writeFile(dtsPath!, dts, { mode })
: Promise.resolve();
const sourceMapPromise = sourceMapPath
? writeFile(sourceMapPath, sourceMap!, { mode })
: Promise.resolve();

await Promise.all([
writeFile(destFile, sourceCode, { mode }),
dtsPromise,
sourceMapPromise,
]);
}

export async function compile(
Expand Down
2 changes: 1 addition & 1 deletion packages/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"devDependencies": {
"@jest/transform": "^27.5.1",
"@swc/core": "^1.3.107",
"@swc/core": "^1.6.4",
"@types/node": "^16.11.12",
"@typescript-eslint/eslint-plugin": "^5.6.0",
"@typescript-eslint/parser": "^5.6.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/swc-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"license": "MIT",
"private": false,
"devDependencies": {
"@swc/core": "^1.3.107",
"@swc/core": "^1.6.4",
"webpack": "^5.69.1"
},
"peerDependencies": {
Expand Down
Loading

0 comments on commit bf04342

Please sign in to comment.