Category: Development
Angular 8 + Ionic 4 Monorepo Part 1: The Setup
If you’re like me you created this super cool angular web application https://kiwibudget.com/ (shameless promotion) and you users keep asking for a mobile app!
Since your (super cool) frontend is in angular the logical solution is to use Ionic to build the app since you got all this pre-written ng code that you wanna use.
I wont go too deep into the details but after trying to set things up with vanilla Angular workspaces (and failing spectacularly), then trying Angular + nrwl/nx (works but so much hackery to get ionic to work, maintenance would raise technical debt to infinity), I found the gold that is xplat.
This very quickly written post is to document my setup with future blogs as app development progresses.
Disclaimer. This post mostly serves as a note to me of how to set things up, so it’s pretty rough.
Lines that begin with ‘?’ should not be typed and are cli options.
Lets setup our nx workspace
npm init nx-workspace myworkspace
? What to create in the new workspace empty
? CLI to power the Nx workspace Angular CLI
npm install -g @nrwl/cli
npm install --save-dev @nstudio/xplat
ng add @nstudio/xplat
Lets make the web app (we call it ‘app’, xplat will prepend ‘web’, change this is you have other requirements)
(You can also configure the prefix but you can look into that yourself)
ng g app
? What name would you like for this app? app
? What type of app would like to create? web
? Which frontend framework should it use? angular
? Use xplat supporting architecture? Yes
? In which directory should the app be generated?
? Which stylesheet format would you like to use? SASS(.scss) [ http://sass-lang.com ]
Lets make the mobile app (we call it ‘app’, xplat will prepend ‘ionic’, change this is you have other requirements)
ng g app
? What name would you like for this app? app
? What type of app would like to create? ionic
? Which frontend framework should it use? angular
? Use xplat supporting architecture? Yes
We want to use the ionic command to manage our app so lets hook that up.
Create a file ionic.config.json
with the following
{
"name": "ionic-app",
"integrations": {},
"type": "angular",
"root": "apps/ionic-app"
}
We can now run our ionic app with ionic serve --project ionic-app
But wait it still fails…
For some reason capacitor is not installed automatically so since we have setup the ‘ionic’ command we can fix that with
ionic integrations enable capacitor --project ionic-app
Right now you can start apps with
ng serve web-app
ionic serve --project ionic-app
Done!
PS. If you are a lazy dev like me and want to use the Ionic devapp you will also need to install the cordova integration.
It’s not technically a smart idea but… thug life
ionic integrations enable cordova --project ionic-app
How to deploy an Angular 6 project to Heroku
As I had difficulty finding a reliable source online, I decided to create my own.
Here is how you deploy an Angular 6 app to Heroku
Step 1
You are going to need something to serve your files. Let’s go with express. We will also need ‘path’ to setup our server (unless you wanna hardcode those in yourself)
npm install --save express path
Step 2.
Now if we want Heroku to build our project on their servers we need to tell them two things.
1. How to build our project and
2. What versions of node/npm our code works with
You can do this by putting the following in package.json
"scripts": {
...
"postinstall": "ng build --prod"
},
"engines": {
"node": "8.11.3",
"npm": "6.1.0"
},
Remember to replace the versions of node and npm to the ones you have.
You can find out with
node --version
npm --version
Step 3
By default angular separates away from deployments what it thinks are ‘development’ only additions. However, since Heroku is building our code, we need to give it the ability to run those modules.
To do this you can either move @angular/cli
, @angular/compiler-cli
, typescript
and "@angular-devkit/build-angular": "~0.6.8"
__*__ from our devDependencies over to dependencies. Or we can have Heroku install those modules on their own.
I personally prefer the first as it allows you to specify versions, however if you wish to do the latter, you would place the following right under postinstall
"preinstall": "npm install -g @angular/cli @angular/compiler-cli typescript",
Step 4
Create our server file. In your main application directory (the one with package.json) create a file called server.js
. Add the following
const path = require('path');
const express = require('express');
const app = express();
// Serve static files
app.use(express.static(__dirname + '/dist/MY_APP_NAME'));
// Send all requests to index.html
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname + '/dist/MY_APP_NAME/index.html'));
});
// default Heroku port
app.listen(process.env.PORT || 5000);
Remember to replace MY_APP_NAME (both of them) to the name of your app.
Step 5
Now to create a Procfile to tell Heroku “how” we wish our app to be run. In your project directory (same one with package.json) create a file called Procfile
and place the following
web: node server.js
Step 6. Final Step
We can now build our app with npm install
and run it with ‘node server.js’.
If everything works, we should now see a working site on http://localhost:5000
If you have any issues feel free to leave a message in the comments.
To push to heroku, assuming you have the cli installed.
If not (https://devcenter.heroku.com/articles/heroku-cli#download-and-install)
heroku create
git add .
git commit -m "initial heroku deploy'
git push heroku master
Done. You should now see a deploy link. Open it up and you should see your site.
Hope that helped, if you have any issues try typing heroku local
on your machine for some more insight.
..
..
..
Still here?
Fine fine.. you’ve convinced me.. here’s a bonus.
Seeing as Heroku employs magical unicorns and it https all the things. You can add this to your server.js to perform a protocol redirect.
// Heroku automagically gives us SSL
// Lets write some middleware to redirect us
let env = process.env.NODE_ENV || 'development';
let forceSSL = (req, res, next) => {
if (req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect(['https://', req.get('Host'), req.url].join(''));
}
return next();
};
if (env === 'production') {
app.use(forceSSL);
}
Be sure to place it directly under const app = express()
otherwise urls like index.html will not redirect.
How to create an observable in Ionic v2
Observable.create(observer => { this.http.get(url, {headers}).subscribe(res => { this.response = res; observer.next(this.response); observer.complete(); }, err => { console.log(err); observer.error(err); observer.complete(); }); });
Recent Comments