Skip to content

Commit

Permalink
Fix type declarations not being generated properly (#7)
Browse files Browse the repository at this point in the history
Fixes: #6
Fixes: #5
  • Loading branch information
hamlim authored May 21, 2024
1 parent 8b18340 commit 634e330
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 79 deletions.
6 changes: 6 additions & 0 deletions packages/hohoro/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
### Unreleased:

### [0.1.2] - May 21st, 2024

- Fix `.d.ts` files not being generated properly by `hohoro`
- Fix not cleaning up `temp.tsconfig.json` file if `swc` failed to compile
- Fix calling `build` programatically not working properly (although this is an unstable API)

### [0.1.1] - May 20th, 2024

- Fix file paths within `build-cache.json` to be relative to the project root
Expand Down
26 changes: 17 additions & 9 deletions packages/hohoro/__tests__/incremental-build.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ before(() => {
const templateDir = pathJoin(
__dirname,
"..",
"test-workspace-dir",
"sample-workspace-dir",
"template",
);
const srcDir = pathJoin(__dirname, "..", "test-workspace-dir", "src");
const srcDir = pathJoin(__dirname, "..", "sample-workspace-dir", "src");
const files = fg.sync(pathJoin(templateDir, "**/*.{ts,tsx,js,json}"));

for (const file of files) {
Expand All @@ -38,18 +38,22 @@ test("It correctly builds the library", async () => {
},
};
await runBuild({
rootDirectory: pathJoin(__dirname, "..", "test-workspace-dir"),
rootDirectory: pathJoin(__dirname, "..", "sample-workspace-dir"),
logger,
});

const distFiles = fg.sync(
pathJoin(__dirname, "..", "test-workspace-dir", "dist", "**/*"),
pathJoin(__dirname, "..", "sample-workspace-dir", "dist", "**/*"),
);

assert.ok(
distFiles.some((file) => file.includes("tsx-file.js")),
"Couldn't find tsx-file.js!",
);
assert.ok(
distFiles.some((file) => file.includes("tsx-file.d.ts")),
"Couldn't find tsx-file.d.ts!",
);
assert.ok(
distFiles.some((file) => file.includes("js-file.js")),
"Couldn't find js-file.js!",
Expand Down Expand Up @@ -81,14 +85,14 @@ test("It only builds changed files", async () => {
const tsxFile = pathJoin(
__dirname,
"..",
"test-workspace-dir",
"sample-workspace-dir",
"src",
"tsx-file.tsx",
);
writeFileSync(tsxFile, `export const foo = 'bar';`);

await runBuild({
rootDirectory: pathJoin(__dirname, "..", "test-workspace-dir"),
rootDirectory: pathJoin(__dirname, "..", "sample-workspace-dir"),
logger,
});

Expand All @@ -99,13 +103,17 @@ test("It only builds changed files", async () => {
);

const distFiles = fg.sync(
pathJoin(__dirname, "..", "test-workspace-dir", "dist", "**/*"),
pathJoin(__dirname, "..", "sample-workspace-dir", "dist", "**/*"),
);

assert.ok(
distFiles.some((file) => file.includes("tsx-file.js")),
"Couldn't find tsx-file.js!",
);
assert.ok(
distFiles.some((file) => file.includes("tsx-file.d.ts")),
"Couldn't find tsx-file.d.ts!",
);
assert.ok(
distFiles.some((file) => file.includes("js-file.js")),
"Couldn't find js-file.js!",
Expand All @@ -119,13 +127,13 @@ test("It only builds changed files", async () => {

// cleanup src and dist dirs after the tests run
after(() => {
const srcDir = pathJoin(__dirname, "..", "test-workspace-dir", "src");
const srcDir = pathJoin(__dirname, "..", "sample-workspace-dir", "src");

const files = fg.sync(pathJoin(srcDir, "**/*.{ts,tsx,js,json}"));
for (const file of files) {
rmSync(file, { recursive: true });
}
rmSync(pathJoin(__dirname, "..", "test-workspace-dir", "dist"), {
rmSync(pathJoin(__dirname, "..", "sample-workspace-dir", "dist"), {
recursive: true,
});
});
43 changes: 33 additions & 10 deletions packages/hohoro/incremental-build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fg from "fast-glob";
import { exec } from "node:child_process";
import { createHash } from "node:crypto";
import { createReadStream, readFileSync, rmSync, writeFileSync } from "node:fs";
import path, { join as pathJoin } from "node:path";
import path, { join as pathJoin, resolve } from "node:path";
import { promisify } from "node:util";

const execAsync = promisify(exec);
Expand All @@ -25,11 +25,13 @@ function compile({ rootDirectory, files, logger }) {
"--strip-leading-paths",
].join(" ");
debug(`[compile] ${command}`);
return execAsync(command).then(({ stdout, stderr }) => {
return execAsync(command, { cwd: rootDirectory }).then(({ stdout, stderr }) => {
if (stderr) {
logger.error(stderr);
}
logger.log(stdout);
if (stdout) {
logger.log(stdout);
}
});
}

Expand All @@ -41,22 +43,33 @@ function compile({ rootDirectory, files, logger }) {
// ignores the tsconfig file when passing file paths in!
async function compileDeclarations({ rootDirectory, files, logger }) {
const tempTSConfigPath = pathJoin(rootDirectory, "temp.tsconfig.json");
const tempTSconfig = {
extends: "./tsconfig.json",
include: files,
compilerOptions: {
noEmit: false,
declaration: true,
emitDeclarationOnly: true,
outDir: "dist",
},
exclude: ["**/__tests__/**"],
};
debug(`[compileDeclarations] Writing temp tsconfig file: `, JSON.stringify(tempTSconfig, null, 2));
writeFileSync(
tempTSConfigPath,
JSON.stringify({
extends: "./tsconfig.json",
includes: files,
}),
JSON.stringify(tempTSconfig, null, 2),
);
const command = `tsc --project temp.tsconfig.json`;
debug(`[compileDeclarations] ${command}`);
return execAsync(command).then(
return execAsync(command, { cwd: rootDirectory }).then(
// For both success and failures we want to cleanup the temp file!
({ stdout, stderr }) => {
if (stderr) {
logger.error(stderr);
}
logger.log(stdout);
if (stdout) {
logger.log(stdout);
}
rmSync(tempTSConfigPath);
},
() => {
Expand Down Expand Up @@ -142,10 +155,20 @@ export async function runBuild(

const absoluteChangedFiles = changedFiles.map(changedFile => rootDirectory + changedFile);

await Promise.all([
const [compileResult, declarationsResult] = await Promise.allSettled([
compile({ rootDirectory, files: absoluteChangedFiles, logger }),
compileDeclarations({ rootDirectory, files: absoluteChangedFiles, logger }),
]);
if (compileResult.status === "rejected" || declarationsResult.status === "rejected") {
if (compileResult.status === "rejected") {
logger.error(`Failed to compile: ${compileResult.reason}`);
}
if (declarationsResult.status === "rejected") {
logger.error(`Failed to compile declarations: ${declarationsResult.reason}`);
}
debug(`Ran in ${Date.now() - start}ms`);
process.exit(1);
}
try {
writeFileSync(cacheFilePath, JSON.stringify(filesHashed));
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion packages/hohoro/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hohoro",
"description": "An incremental build tool for JavaScript and TypeScript projects.",
"version": "0.1.1",
"version": "0.1.2",
"bin": {
"hohoro": "./bin/index.mjs"
},
Expand Down
17 changes: 17 additions & 0 deletions packages/hohoro/sample-workspace-dir/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "typescript",
"dynamicImport": false,
"decorators": false
},
"transform": null,
"target": "es5",
"loose": false,
"externalHelpers": false,
// Requires v1.2.50 or upper and requires target to be es2016 or upper.
"keepClassNames": false
},
"minify": false
}
2 changes: 2 additions & 0 deletions packages/hohoro/sample-workspace-dir/template/tsx-file.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// I'm a simple tsx file
export type Foo = string;
23 changes: 23 additions & 0 deletions packages/hohoro/sample-workspace-dir/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

/* Linting */
"skipLibCheck": true,
"strict": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true
}
}
20 changes: 0 additions & 20 deletions packages/hohoro/test-workspace-dir/.swcrc

This file was deleted.

1 change: 0 additions & 1 deletion packages/hohoro/test-workspace-dir/template/tsx-file.tsx

This file was deleted.

17 changes: 0 additions & 17 deletions packages/hohoro/test-workspace-dir/tsconfig.json

This file was deleted.

12 changes: 2 additions & 10 deletions packages/template-library/.swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,9 @@
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false,
"syntax": "typescript",
"dynamicImport": false,
"privateMethod": false,
"functionBind": false,
"exportDefaultFrom": false,
"exportNamespaceFrom": false,
"decorators": false,
"decoratorsBeforeExport": false,
"topLevelAwait": false,
"importMeta": false
"decorators": false
},
"transform": null,
"target": "es5",
Expand Down
11 changes: 0 additions & 11 deletions packages/template-library/build.tsconfig.json

This file was deleted.

6 changes: 6 additions & 0 deletions packages/template-library/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
console.log("Hello via Bun!");

export type Foo = string;

export function foo(arg: Foo) {
return "Hello!";
}

0 comments on commit 634e330

Please sign in to comment.