Skip to content

Commit

Permalink
Merge pull request #12 from XaveScor/11-direct-typescript-use
Browse files Browse the repository at this point in the history
11: direct typescript calls instead of rollup/typescript-plugin
  • Loading branch information
XaveScor authored Aug 21, 2024
2 parents 66c80e4 + 1fb65ea commit 0175676
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 236 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
"vitest-directory-snapshot": "^0.2.2"
},
"dependencies": {
"@rollup/plugin-typescript": "^11.1.6",
"tslib": "^2.6.3",
"vite": "^5.3.4",
"yargs": "^17.7.2",
"zod": "^3.23.8"
},
"optionalDependencies": {
"typescript": "^5.0.0"
},
"exports": "./src/run.ts"
}
113 changes: 6 additions & 107 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
export {};
50 changes: 50 additions & 0 deletions src/buildTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as path from "node:path";
import * as process from "node:process";
import * as fs from "node:fs";

type BuildTypesOptions = {
sourceDir: string;
files: string[];
outDir: string;
};

export async function buildTypes({
sourceDir,
files,
outDir,
}: BuildTypesOptions) {
const ts = await import("typescript");
const configPath = path.join(sourceDir, "tsconfig.json");
const configFile = ts.readConfigFile(configPath, ts.sys.readFile);

const parsedCommandLine = ts.parseJsonConfigFileContent(
configFile.config,
ts.sys,
sourceDir,
{
declaration: true,
emitDeclarationOnly: true,
strict: false,
strictNullChecks: false,
strictFunctionTypes: false,
strictPropertyInitialization: false,
skipLibCheck: true,
skipDefaultLibCheck: true,
},
configPath,
);

const host = ts.createCompilerHost(parsedCommandLine.options);

const sourceToDtsMap = new Map<string, string>();
const program = ts.createProgram(files, parsedCommandLine.options, host);
program.emit(undefined, (fileName, data) => {
const relativePath = path.relative(sourceDir, fileName);
const finalPath = path.join(outDir, relativePath);
const sourceFileName = fileName.replace(/\.d\.ts$/, ".ts"); // Assuming source files have .ts extension
sourceToDtsMap.set(sourceFileName, finalPath);
fs.writeFileSync(finalPath, data);
});

return sourceToDtsMap;
}
Loading

0 comments on commit 0175676

Please sign in to comment.