问题
Can someone give me some advice or links for discussion on whether I should bundle JS for backend?
I tried to Google with this title (and similar words) and I can't get any useful links.
Just want to know, say I am using latest Node.JS (es6-ready), should I bundle/compile the JS? If not, how am I suppose to use typescript/flow?
Thank you.
回答1:
I feel like you are asking two different questions. I'll try to answer both.
How can I just run TypeScript code?
This is the one your question's title seems to ask ("How to use typescript/flow in nodejs without compiling it"). For this, you can use the ts-node package on npm. But it's usually not a good idea to use ts-node over just compiling when running in production because it tends not to be as fast.
How should TypeScript code get distributed to be run?
Any TypeScript code will need to get compiled from .ts
files to .js
files to eventually be run. Basically something like the same thing applies to Flow code.
If you plan to distribute a package written in TypeScript, you should be publishing the .js
and .d.ts
files together. This is so that
- Your package consumers don't have to recompile your package. (they already get
.js
files. - Your non-TypeScript consumers don't need to install TypeScript to use your package. (they already have runnable
.js
files) - Your TypeScript consumers can get good type safety and completions. (they get your
.d.ts
files)
For more information, see the TypeScript documentation on Publishing Declaration Files.
来源:https://stackoverflow.com/questions/40752666/how-to-use-typescript-flow-in-nodejs-without-compiling-it