You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this short tutorial we'll create a simple monorepo workspace using Nx, with two (2) apps, the first in angular an the other in react, also, there'll be two (2) APIs, NestJS and Django. The code of this would be later added here, in a github repo.
I strongly recommend you to use VSCode and install the NX Console Extention
Its just my opinion, any other editor or extention could be used 👍
without furthermore words, lets start.
Creating the workspace
First we have to run the follow command, and create a simple workspace (not angular, react o frameworklike, just a empty workspace). For official doc, you can check this out.
npx create-nx-workspace@latest
then, its not mandatory but dont hurt at all so, we add NX globally
yarn global add nx
# or
npm install -g nx
With this, we have the base.
Creating frontend Apps
Now, lets add the firsts apps, for this example I would add 2 apps, an Angular App, and a React App,
and a ts library with a simple , we need to install some dependencies. Lets do it:
For Angular App
npm install --save-dev @nrwl/angular
For React App
npm install --save-dev @nrwl/react
With this, now is time to run some NX commands. The best way is use the Nx CLI to create the apps, because its wrapped on top of angular cli, extending its functionality and providing extra features. Lets start with Angular, there is two ways:
Using Nx console in VSCode
Use the console, is very intuitive. (TODO: Extend with images or video)
Using the terminal
The most probably scenario if you pick this way, will be expend some time reading the official documentation for the Nx CLI. In any case, I'll let you here the analogous command for the Nx console way
nx generate @nrwl/angular:application --name=wallet-playground --style=scss --inlineTemplate --skipTests --no-interactive
# or if you dont have Nx CLI instaled globally
npx nx generate @nrwl/angular:application --name=wallet-playground --style=scss --inlineTemplate --skipTests --no-interactive
For the React app, the process is pretty much the same, but the Nx command I used is:
That's it, we have a simple monorepo with two frontend apps ready to use. Now, is time to add a TS library both apps can use
Creating the shared library
The goal is create a simple TS framework agnostic library. As you'd imagine, creating this TS library is very similar to the last process, we could use Nx Console or Nx CLI, you'll obtain the same result. Thats why i only going to let here ther direct Nx CLI command:
Now its up to you, added some logic and connect the apps, get fun while do it. The last section will be a FAQ of all the bugs I have encounter until now, hoping it help some of you.
FAQ and Bugs
Sometimes you'd see a webpack error like this
To solve it, you've to create a webpack.config.js file, and add every module for error present in the app. For the above case will be
Now, we have to change the webpack compiler used by angular, normally its in the angular.json file, but in Nx monorepo, we'll find it in the workspace.json, inside the app, in the build and serve options. See the images below for more details
you have to install "@angular-builders/custom-webpack": "^12.1.2" in your package.json devDependencies, and then, add in
your workspace.json the correct executor: For build is -> @angular-builders/custom-webpack:browser and for serve is -> @angular-builders/custom-webpack:dev-server
In the same file, after add the custom webpack, dont forget to reference the new webpack.config.js file we just created.
If at the time you read this, the website metion above is down, you can use the code and replace the colors, also im gonna let the following image for futher reference
after having thease, we gonna refact the default angular style folder. As you may know, when a new angular project is created, we only have one .scss file (called style.scss). I always change the struct a little, having something like this
now, its important to know one particular behavior of material. in order to override the foreground and background element, you need to replace the sass funcions and pass your recent created variables. This would do it:
Now, lets add some functionality to be able of change between light mode and dark mode
Theme Service
At this time we have set up a custom theme light theme and dark theme, but it lack in the ability to change between the two. To solve that, we gonna create a custom angular service to change the theme base in the user selection.
Lets create an angular lib using NX console or the following command
you can use another implementation or class name, Im only showing a way to do it. Next we nee a service, to handle the logic of when add or remove the dark mode class
I always love when a website remember my ui selections, its feel like a im dealing with a not so stupid app. Thats why we gonna saved the last user selection to display when the user come back.