JoshuaKGoldberg / create-typescript-app

Quickstart-friendly TypeScript template with comprehensive, configurable, opinionated tooling. 💝
MIT License
918 stars 69 forks source link

🚀 Feature: ability to debug the running package code #1145

Closed johnnyreilly closed 8 months ago

johnnyreilly commented 9 months ago

Bug Report Checklist

Overview

I love being able to debug. Whenever I'm making a package I try to set up a way to debug the code in VS Code. Either using a combo of tsc / sourcemaps and Node.js or using ts-node.

I think it'd be awesome if CTA had this built in. I'd be potentially up for contributing this. My instinct would be to prefer using just tsc / sourcemaps and Node.js to avoid adding another dependency.

What do you think?

Additional Info

N/A

johnnyreilly commented 9 months ago

Got a rough version working locally in VS Code. Implementation looks like this:

launch.json file:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": ["<node_internals>/**"],
            "program": "debug/index.js",
            "preLaunchTask": "build:debug"
        }
    ]
}

task.json file:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "npm",
            "script": "debug",
            "problemMatcher": [],
            "label": "build:debug",
            "detail": "Build a debug version of the project"
        }
    ]
}

The scripts of package.json has a new entry:

        "debug": "rimraf debug && tsc --noEmit false --outDir debug",

And requires a dev dependency of rimraf to delete the debug folder prior to debug builds.

With the above in place, you can F5 in VS Code and debug, hitting your breakpoints. Is this an implementation that would work well generally?

JoshuaKGoldberg commented 9 months ago

Hmmm. Hmm... I' really prefer not to add to package.json or take on new directories for builds. Is there a way to reuse the existing tsup builder (pnpm build) instead?

johnnyreilly commented 9 months ago

Yeah I think so - given that the tsup config has 2 key settings set we're actually in a decent position:

import { defineConfig } from "tsup";

export default defineConfig({
    bundle: false,
    clean: true, // WILL CLEAN DIRECTORY PRIOR TO BUILD MEANING WE DON'T DEBUG STALE CODE
    dts: true,
    entry: ["src/**/*.ts"],
    format: "esm",
    outDir: "lib",
    sourcemap: true, // WILL ALLOW US TO DEBUG ORIGINAL SOURCE CODE
});

Given these are in place I think we might only need tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "npm",
            "script": "build",
            "problemMatcher": [],
            "label": "build",
            "detail": "Build the project"
        }
    ]
}

and launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Debug Program",
            "skipFiles": ["<node_internals>/**"],
            "program": "lib/index.js",
            "preLaunchTask": "build"
        }
    ]
}
JoshuaKGoldberg commented 9 months ago

Awesome. Let's do it!

johnnyreilly commented 9 months ago

I'll put a PR together! Out of curiosity, what is the motivation for using tsup? I've read the docs and this:

https://jakeginnivan.medium.com/options-for-publishing-typescript-libraries-9c37bec28fe

But I'm not super clear on the benefits of using it over tsc. Is it about speed of compilation?

JoshuaKGoldberg commented 9 months ago

It's a few things:

Being able to do all that in a single tool run by a single pnpm run build is nice and straightforward. I like that!

github-actions[bot] commented 8 months ago

:tada: This is included in version v1.54.0 :tada:

The release is available on:

Cheers! 📦🚀