Nestjs incorrent dist folder structure with monorepo mode - nestjs

How come it generates dist/apps/twitter and puts everything into this folder must be put just into dist folder? What can lead to this?
Expected dist folder structure:
dist:
--apps:
----twitter/src
--libs

This isn't "incorrect" per se. When Nest is in monorepo mode, it by default will use webpack to bundle the application code together to a single file. This of course can be overridden and tsc can be used instead to output all of the compiled TS code if that is preferred. When tsc compiles code that contains sources outside of a single directory (e.g. apps/twitter/src is the base directory but libs/whatever is imported as well) then Typescript does its best to maintain the directory structure in the resulting dist so that import paths do not end up getting messed up.
The reason for having the apps/twitter twice is because Nest sets the output directory of the build to be dist/apps/<app-name>, similarly to how Nx does. This is done so that if you end up having multiple applications, say apps/google, you can havedist/apps/twitter and dist/apps/google and not have their dists interfere with each other

Related

__dirname in typescript for nodejs

This is the first time I'm using typescript in my node project.
my directory structure looks like this
root
src // typescript files
index.ts
build // compiled js files
src
index.js
storage // storage for media
I'm outputting my compiled javascript into ./build
now, In my index.ts file, I have
fs.existsSync(resolve(__dirname, '../storage'))
This works when i'm running nodemon with ts-node and of course fails when I run javascript output from build.
I believe there's a way to tell typescript to process these files according to tsconfig.compilerOptions.outDir
or compile as srcNew/index.js instead of build/src/index.js but I'm not able to find it.
I figured it out,
Turns out this is a behaviour of typescript compiler.
And you should only set tsconfig.compilerOptions.rootDir when you want your current directory structure to be a part of the tsconfig.compilerOptions.outDir directory. Otherwise it would copy your current directory struture into outDir as it is.
You can read more about it here.

Nestjs project fails to start because it reads files outside of it's module

My Nestjs Project fails to start because it indexes other node projects out of its own project folder.
Seems like your configuration and thus your entity file does not recognize src/ as part of your application.
Try replacing src/directors/director.entity inside your movies.entity.ts with relative path such as ../../directors/director.entity and see what happens then.

Webpack bundle.js content

I start learning webpack , node , and react and I am bit confused about some basic staff.
Does webpack bundle whole react.js + my javascript files into one output file, so when deploying to production I don't need to install node packages used in project (assuming that they are added to webpack.config.js file) ?
If above is right:
On my server I just need to place index.html + output from
webpack ( bundle.js) ? ( In simple scenario) ?
package.json will be used only on development side ?
You only need index.html and the bundle.js (or any name you gave the file) for the app to work, provided that you are not using any local assets. You don't need to include node modules. Package.json should tell you what to include in your project so that you don't have to include node modules whenever you want to upload your project along with few other decalarations.
The way Webpack works is that you specify one or more entry points and one or more output files. Webpack then reads the entry point and also traverses through the import / require statements recursively. It then generates final bundle file(s) which includes all the traversed files.
Yes, Webpack outputs everything in the the bundle.js file(s). You can configure multiple output bundles. So, you just need HTML and output bundle to deploy the app.
The package.json specifies the packages upon which the app depends, apart from several other things. While traversing through the entry points, webpack will also include the packages specified in import / require. Function of package.json is to tell npm to install those packages.

Difference between lib and dist folders when packaging library using webpack?

Ive just published my first package (a react component) to npm but im having some trouble understanding the difference between what the lib directory is compared to the dist.
Currently I generate both lib and dist however my package "main" points to the dist unminified js file which has been built using webpack and output as UMD. The lib folder is built using babel taking the src and outputting to lib.
The dist folder contains both [unminified/minified].js files as well as [unminified/minified].css files.
My main confusion is with the lib folder since imports from there currently wouldn't work seeing as I just transform src -> lib meaning the scss references are still there and the scss files aren't transformed either.
I use CSS Modules (css-loader, styles-loader, postcss-loader etc) to generate my CSS files and this is where the confusion is since, wouldn't I also need to use webpack to generate my lib folder seeing as the scss files/import references need to be transformed to css?
Are you meant to have both lib and dist or is the UMD build in dist fulling the same purpose as that of having a lib folder?
If you are supposed to have both how would I achieve this, since I couldnt find any info regarding generating the lib folder when using CSS modules within your js files and still maintaing the same folder structure of that of src (while still generating dist)?
Usually the dist folder is for shipping a UMD that a user can use if they aren't using package management. The lib folder is what package.json main points to, and users that install your package using npm will consume that directly. The only use of the lib as opposed to src is to transform your source using babel and webpack to be more generally compatible, since most build processes don't run babel transforms on packages in node_modules.
As far as handling the style imports, it's probably a good idea to not import scss or css files in your source js that you export. This is because node can't import styles like that by default. If you have an example that demos your component, it makes sense to import the styles there. The common pattern is to publish minified and unminified css in the dist folder, and in your documentation tell the consumer to explicitly import the css file using whatever technique they prefer. I took this approach with redux bug reporter if you need an example. Hope that helps!
In general lib refers to libraries that are included in a package, dist on the other hand are distribution files for your project. As an example you could write a bunch of javascript and include jquery (which is a lib) and then when they're all bundled up you have a single dist file.
Ok think I found out how to do this. There is a babel plugin that allows you to use webpack loaders when running babel (babel-plugin-webpack-loaders). Thus my CSS mapping is inlined within the js file and the mapping hashes used are also the same as that used when building dist. Yay!

Is there a variable that refers to the package base directory?

Is there a variable (or how to make such a variable) that refers to the package base directory (where package.json is)?
The use case:
I am using Babel to compile code from ES6 to ES5. ./src/ to ./dist/. Then I refer to the ./dist/ code in the main property of the package.json
The problem happens when ./src/ code uses files that are not JS, e.g. ./src/schemas/*.json. These files do not exist in ./dist/ folder. Therefore, when referring to non-JavaScript files from ./src/ I need to use a path that keeps a reference to ./src/ file.
I can already do this using ./../src/schemas/foo.json when requesting a file. Though, thats a fragile approach.
I know I can simply copy all the non-JavaScript files to ./dist/, but
duplication of content does not seem like an appropriate solution.
This is the appropriate solution.
Why ?
Your src folder should hold the source code of your project (not including dependencies & build routines)
Your dist folder is supposed to hold a standalone version of your app or website that you'll be able to distribute "as is" (you should be able to upload the content of dist via ssh/ftp or whatever to your remote server and it should be working)
Note: This thread is more about code organization than code itself (so anybody can have his opinion), but this is the kind of workflow yeoman and lots of build systems use.

Resources