swc-node icon indicating copy to clipboard operation
swc-node copied to clipboard

Is there a way to use this tool like TSC?

Open kfirba opened this issue 4 years ago • 4 comments

Hey,

I'm trying to figure out how to use @swc-node/core, but not sure how I would use it. This tool provides 2 functions that receive an input string only.

Is there a way I can define an entry file, and from there, it will automatically transpile everything that this file needs (recursively)?

Maybe write some custom code for it?

Thanks

kfirba avatar Jul 19 '21 05:07 kfirba

It seems like @swc-node/core is just a configuration layer around @swc/core - I have been using @swc/cli to do what you are describing and have a small .swcrc file, and this has had good results.

https://swc.rs/docs/usage-swc-cli/

You can use the options function in @swc-node/core as inspiration for your .swcrc file:

https://github.com/Brooooooklyn/swc-node/blob/fbfe8d6cf4cd62528eea920a724ccba00b46ddec/packages/core/index.ts#L26

My .swcrc for compiling an Next.js + Express server written in TypeScript simply contains this:

{
  "jsc": {
    "parser": {
      "syntax": "typescript"
    },
  },
  "module": {
    "type": "commonjs"
  }
}

I then have the following build scripts:

    "build:server": "swc server --config-file server/.swcrc --out-dir dist/server --copy-files",
    "build:server:watch": "swc server --config-file server/.swcrc --out-dir dist/server --copy-files --watch",

stefee avatar Sep 12 '21 12:09 stefee

@stefee do you know how to use it like ts-node-dev, to run the TS code and restart on changes?

ftzi avatar Nov 20 '21 18:11 ftzi

After following this issue, ts-node itself has since added support for swc, so there's that.

seangenabe avatar Nov 20 '21 18:11 seangenabe

@SrBrahma I am using nodemon to watch the dist directory, and then npm-run-all (run-p) to run this in parallel with build:

    "dev": "run-p build:server:watch start:watch",
    "build:server:watch": "swc server --config-file server/.swcrc --out-dir dist/server --watch",
    "start:watch": "nodemon --watch dist/server dist/server/index.js"

Note: I am building a specific directory called server and my entrypoint is server/index.ts in case that's not clear. For example if you are building the src dir with src/exec.ts entrypoint directory and your .swcrc is at root, then you would do this instead:

    "dev": "run-p build:server:watch start:watch",
    "build:server:watch": "swc src --out-dir dist --watch",
    "start:watch": "nodemon --watch dist dist/exec.js"

stefee avatar Nov 20 '21 18:11 stefee