Remix.run Logo
ivanjermakov 7 hours ago

Can it be used for Node builds or browser-only same as Vite?

tvbusy 7 hours ago | parent | next [-]

It uses Vite so the same limitations as Vite. However, I have been using Vite for my NestJS servers without any problem with `vite-plugin-node`. See example at https://github.com/leosuncin/nest-vite-example/blob/master/v...

6 hours ago | parent [-]
[deleted]
UnfitFootprint 5 hours ago | parent | prev | next [-]

Here is my particular incantation for targeting node that is working very well: https://pastebin.com/ynz4B5X0

Essentially you pretend to be a library

TheAlexLichter 6 hours ago | parent | prev | next [-]

I am using Vite+ for CLIs as well, yes. You don't use Vite as dev server then but lint, format, task running and caching is still there!

silverwind 4 hours ago | parent | next [-]

I think tsdown is much better suited for CLI and library builds. Vite has many web-isms that don't matter for these.

bdxn 6 hours ago | parent | prev [-]

I'd be interested in seeing this implementation if it's publicly available. Do you have a GitHub link? Thanks!

curtisblaine 7 hours ago | parent | prev [-]

I'm always curious of the use case when someone proposes Node code bundling. What's the advantage? Obfuscation in SEA?

afavour 6 hours ago | parent | next [-]

In my experience the bundling isn’t really the important aspect (though it also doesn’t harm anything), it’s more just having an ecosystem of plugins for code transpiling, static asset inclusion (e.g. text files) etc and a configuration format folks are already used to.

inbx0 5 hours ago | parent | prev | next [-]

For me, the main benefit is deployment bundle/artifact size reduction. Mostly from dropping unneeded files from node_modules. Many packages include both esm and cjs builds, sources, docs, TS types, etc. stuff that you don’t need in prod. This matters for lambdas, for example, because deployed code size has limits there.

ivanjermakov 7 hours ago | parent | prev | next [-]

Running typescript without compilation is still tricky with plain Node. `vite dev` has amazing DX not available for Node programs. I'm wondering if Vite+ tackles this problem.

curtisblaine 7 hours ago | parent [-]

Don't we have `tsx` and `nodemon` (or the native Node reloader) for that? What are the DX gaps you see on the server side out of on-the-fly transpilation and reload on watch?

ivanjermakov 6 hours ago | parent | next [-]

Yes, I use tsx for Node programs. It's not great when sharing the same codebase for both client and server code, they have completely different dev workflows.

afavour 6 hours ago | parent | prev | next [-]

One advantage of precompilation is risk reduction. Say tsx gets hacked somehow (hardly unprecedented with Node modules!) you’ve got it running on your production server exposed to the internet. Precompilation on a CI pipeline is still a risk but a significantly lower one.

pjmlp 6 hours ago | parent [-]

If only the whole JavaScript wasn't as dependency hell of single function packages....

Cthulhu_ 6 hours ago | parent | prev | next [-]

In theory, typescript doesn't need to be transpiled, you can run ts files using `node --experimental-strip-types file.ts` as long as you don't use any code that needs transpilation (like typescript enums).

Still need tsx to do type checking

ivanjermakov 6 hours ago | parent [-]

No, because of ESM import resolution rules. Typescript suggests extensionless imports, making it incompartible with ESM and therefore Node. Luckly, `node --import=tsx file.ts` handles imports well.

This is especially hairy when making a typescript library that is distributed non-compiled (without dist/) and is supposed to run in both browser and Node.

https://github.com/nodejs/node/issues/46006

https://github.com/microsoft/TypeScript/issues/16577

MrJohz 5 hours ago | parent | next [-]

You can use one of the following:

`allowImportingTsExtensions: true` (https://www.typescriptlang.org/tsconfig/#allowImportingTsExt..., useful if you're running `tsc` in noEmit mode as a linter)

`rewriteRelativeImportExtensions: true` (https://www.typescriptlang.org/tsconfig/#rewriteRelativeImpo..., useful if you're using `tsc` to compile TS files to JS.

This allows you to use fully-specified imports in TypeScript files, which works basically everywhere — NodeJS, TypeScript, bundlers, etc.

The exceptions are browsers (obviously, only normal JS syntax there), and packages inside `node_modules`, which NodeJS will not do any type stripping for. So if you're writing a library, you'll probably still need to distribute the compiled sources, rather than distributing the raw TypeScript files alone. Or you use the JSDoc syntax for TypeScript, which can do everything that .ts files can do, but is more verbose and idiosyncratic.

moogly 3 hours ago | parent | prev [-]

I transpile for prod, but use --strip-types when running in dev, and all I had to do was to make a 10-line ESM register hook that rewrites .js to .ts if the .js import fails, and then a one-liner import register trampoline script. Not sure I'd do that in prod, but works fine in dev at least.

This way I could just use node --watch instead of tsx or nodemon.

ivanjermakov 2 hours ago | parent [-]

Mind sharing the implementation? I think it's basically what tsx is doing when used in `node --import tsx`.

curtisblaine 6 hours ago | parent | prev [-]

@afavour if you need precompilation in CI can't you simply use... tsc?

mnutt 5 hours ago | parent | prev | next [-]

In cases where startup time matters and you have a slow disk, bundling can drastically reduce the number of filesystem calls you have to make for large dependency graphs.

UnfitFootprint 5 hours ago | parent | prev [-]

I’ve found if you want interop ts esm and js cjs you need to compile your code - and then `tsc` doesn’t bundle your dependencies for you and outputs incomplete code.