Skip to content

feat: Add TypeScript native option#1344

Draft
dethell wants to merge 2 commits intomainfrom
transpile-to-module-test
Draft

feat: Add TypeScript native option#1344
dethell wants to merge 2 commits intomainfrom
transpile-to-module-test

Conversation

@dethell
Copy link
Collaborator

@dethell dethell commented Jul 3, 2025

The goal is to allow running Counterfact with no transpilation at all. In my tests so far you can run this with TSX directly against the routes folder, no need for .cache.

@changeset-bot
Copy link

changeset-bot bot commented Jul 3, 2025

⚠️ No Changeset found

Latest commit: fa7d50f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 16060387221

Details

  • 2 of 10 (20.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 84.365%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/app.ts 0 8 0.0%
Totals Coverage Status
Change from base Build 15974819176: 0.02%
Covered Lines: 3511
Relevant Lines: 4249

💛 - Coveralls

@pmcelhaney pmcelhaney requested a review from Copilot August 7, 2025 01:03
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds TypeScript native support by introducing a new useTsx configuration option that allows the system to run TypeScript files directly without transpilation using the tsx package.

  • Adds useTsx boolean configuration option to enable native TypeScript execution
  • Conditionally skips transpilation and cache management when useTsx is enabled
  • Adds tsx package dependency for native TypeScript execution

Reviewed Changes

Copilot reviewed 4 out of 7 changed files in this pull request and generated 2 comments.

File Description
src/server/config.ts Adds useTsx boolean field to the Config interface
src/app.ts Modifies compilation path logic and conditionally disables transpiler when useTsx is enabled
src/server/determine-module-kind.ts Treats .ts files as ES modules when determining module kind
package.json Adds tsx dependency for native TypeScript execution

Comment on lines 122 to 124
const compiledPathsDirectory = nodePath
.join(modulesPath, ".cache")
.join(modulesPath, config.useTsx ? "routes" : ".cache")
.replaceAll("\\", "/");
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a hardcoded "routes" path when useTsx is true creates an inconsistency. This should use a more descriptive path or variable to clarify that this is the source directory rather than a compiled output directory.

Copilot uses AI. Check for mistakes.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, that's what we want. If we're using tsx we want to read the source code form the routes directory. Otherwise read the compiled code from the .cache directory.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had some updates to this PR pending but never pushed them up and the laptop went bust. So I need to get it cleaned up again. The updates were making it more generic instead of specifically referring to tsx. I think my approach should also work with the new node option for --experimental-strip-types. Also, it might work with ts-node. So I'm making it useNativeTypescript or something like that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we detect if either the runtime is tsnode or it's node with the type stripping flag enabled?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's compelling. I'll try that out.

nodePath.join(modulesPath, "routes").replaceAll("\\", "/"),
compiledPathsDirectory,
"commonjs",
"module",
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The module type is hardcoded to "module" but should be conditional based on config.useTsx. When useTsx is false, this should remain "commonjs" to maintain backward compatibility with the transpiled code.

Suggested change
"module",
config.useTsx ? "module" : "commonjs",

Copilot uses AI. Check for mistakes.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, Copilot. You're probably right.

@pmcelhaney
Copy link
Owner

I would love to finish this. Rather than a config option, we can test whether type stripping is supported at runtime.

import { mkdtempSync, writeFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { pathToFileURL } from "node:url";

export async function runtimeCanExecuteErasableTs() {
  const dir = mkdtempSync(join(tmpdir(), "ts-probe-"));
  const file = join(dir, "probe.ts");

  // “erasable” TS: type annotation only.
  writeFileSync(
    file,
    'const x: string = "ok"; export default x;\n',
    "utf8"
  );

  try {
    const mod = await import(pathToFileURL(file).href);
    return mod?.default === "ok";
  } catch {
    return false;
  } finally {
    rmSync(dir, { recursive: true, force: true });
  }
}

@dethell
Copy link
Collaborator Author

dethell commented Jan 15, 2026

I would love to finish this. Rather than a config option, we can test whether type stripping is supported at runtime.

import { mkdtempSync, writeFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { pathToFileURL } from "node:url";

export async function runtimeCanExecuteErasableTs() {
  const dir = mkdtempSync(join(tmpdir(), "ts-probe-"));
  const file = join(dir, "probe.ts");

  // “erasable” TS: type annotation only.
  writeFileSync(
    file,
    'const x: string = "ok"; export default x;\n',
    "utf8"
  );

  try {
    const mod = await import(pathToFileURL(file).href);
    return mod?.default === "ok";
  } catch {
    return false;
  } finally {
    rmSync(dir, { recursive: true, force: true });
  }
}

That's a great idea. This is still on my agenda. It's starting to rise back up on my list. hopefully 1st quarter here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants