Using webpack's loaders without bundling - node.js

I need to compile .ts files in node.js environment but without bundling. Is it possible to do that, just compile without resolving and so on?

Related

Compile a typescript node js app including dependencies

I have created a Node.js app using typescript and trying to compile it. Currently, the build only contains the source files in the src folder. But I also want to include the dependencies like express, body-parser, etc from node_modules in the final build. This is similar to how webpack compiles all the files together creating bundles.
How can I do this using the typescript compiler only? Or it can be done using webpack only?
Please help.
Typescript compiler only compiles typescript to javascript. You have to manage yourself the build as you want.
I would use a post script to organize your program. Here is a example of pre and post script using npm run build Pre and post script gist

TypeScript build into single file with all external dependencies (node_modules) within it

Is it possible to build a TS source into a single file, which would also contain node_modules imported packages' source?
That would be very helpful in a serverless project. I have done this before on a non-TS project but was using webpack (for another reason).
It seems this was briefly possible before but was due a bug https://github.com/Microsoft/TypeScript/issues/13414 ?
You will need to use a bundler such as webpack to bundle your compiled code and all your node_modules dependencies. The TypeScript compiler (tsc) just transforms TypeScript code into JavaScript, and won't deal with bundling.

Why to use webpack-node-externals in node?

I'm using webpack to bundle my typescript nodejs code.
I use webpack-node-externals to avoid errors in node_modules during the compile time.
webpack-node-externals says that, allows you to define externals - modules that should not be bundled.
But why? Webpack should bundle everything that I need to start my bundle right? It can extract and remove module that I don't use. (tree-shake for example).
If I use webpack-node-externals, then I'll have to do npm i in my prod folder to get all the dependencies.
I think this is miss the point of webpack can do. right?
I think you are correct that in your case, bundling into a single file would make more sense. webpack-node-external appears to be designed for use of NodeJS libraries, not standalone apps. From their doc:
When writing a node library, for instance, you may want to split your code to several files, and use Webpack to bundle them. However - you wouldn't want to bundle your code with its entire node_modules dependencies, for two reasons:
It will bloat your library on npm.
It goes against the entire npm dependencies management. If you're using Lodash, and the consumer of your library also has the same Lodash dependency, npm makes sure that it will be added only once. But bundling Lodash in your library will actually make it included twice, since npm is no longer managing this dependency.
As a consumer of a library, I want the library code to include only its logic, and just state its dependencies so they could me merged/resolved with the rest of the dependencies in my project. Bundling your code with your dependencies makes it virtually impossible.
I disagree with the comments that suggest Webpack was not designed to bundle Node scripts, considering that Webpack has a specific setting for just that (target). Unfortunately, there are too many third-party libraries that do not play nice with Webpack (as I just discovered today), so pragmatically speaking you're better off installing modules in the distribution folder anyway.
This is because of the binary dependency in node_modules/ as explained in:
https://archive.jlongster.com/Backend-Apps-with-Webpack--Part-I
Webpack will load modules from the node_modules folder and bundle them
in. This is fine for frontend code, but backend modules typically
aren't prepared for this (i.e. using require in weird ways) or even
worse are binary dependencies.
I went through this explanation, you can see my studies here:
https://github.com/ApolloTang/wf-backend-with-webpack-explained/tree/main/steps

How do I whitelist subdependencies on webpack?

I'm bundling code using Webpack with target:'node'.
I don't need to include all the dependencies and I am using webpack-node-externals to exclude node modules from the bundle. There are some modules I need to keep and I use node-external's whitelist option to do this.
However, my code is failing at runtime because the subdependencies of these whitelisted modules don't get bundled!
Does anyone know a way that I can bundle subdependencies too?
Thanks!

Electron putting node_modules in asar fails module dependencies

I've got an Electron project in node.js and am currently attempting to make distribution easier by collapsing the massive node_modules hierarchy into an asar. This way I can have app.asar and modules.asar, as suggested by various places such as grunt-asar.
The problem is that this needs me to change all require() statements to have the asar in the path, like: require('modules.asar/package'). That's fine for modules I'm including from my code but when a module itself uses require('dependency-package') that then fails to resolve. As I understand it, node.js will resolve in the "node_modules" folder for these but there is no "node_modules" directory anymore due to packaging modules into an asar.
Is there anything I can do to help this?

Resources