I've been trying to follow several guides all week on integrating Typescript into my AngularJS 1.6.2. application. Everytime I get confused half way through. Right now, I'm on the fourth one in this list:
- https://angular.io/guide/upgrade
- https://medium.com/@ylerjen/migrating-angularjs-application-to-typescript-5230eb8c46e1
- https://jenniferwadella.com/blog/using-typescript-angularjs
- https://tech.ovoenergy.com/angular-typescript/
Here is the file structure of my project:
ProjectRoot
--> app
----> js
------> controllers
------> directives
------> services
------> modules
------> app.js
------> main.js
------> routes.js
----> templates
----> index.html
--> gruntFile.js
--> package.json
The structure is a lot more complicated than this but these are the folders and files I deemed to be most relevant.
Following the fourth guide above, I added the following files to my project:
ProjectRoot/app.ts
import { BaselineService } from './app/js/services/baselineService'
import { BaselineController } from './app/js/controllers/baselineController'
import angular from 'angular';
angular
.module('app',[])
.controller('ctrl', BaselineController)
.service('service', BaselineService);
ProjectRoot/app/js/services/baselineService.ts
export class BaselineService {
static $inject = ['$http'];
constructor(private $http) {
}
get() {
return this.$http.get('url');
}
}
ProjectRoot/app/js/controllers/baselineController.ts
import angular from 'angular';
import { BaselineService } from '../services/baselineService';
export class BaselineController {
prop: string;
static $inject = ['service'];
constructor (baselineService: BaselineService) {
this.prop = baselineService.get();
}
}
angular.module('app').controller('BaselineController', BaselineController);
ProjectRoot/gruntFile.js
module.exports = function (grunt) {
...
grunt.initConfig({
...
ts: {
default: {
src: ['_all.ts'],
out: 'src/out.js',
options: {
sourceMap: true
}
}
}
});
}
ProjectRoot/package.json
{
...
"dependencies": {
...
"browserify": "^17.0.0",
...
}
...
}
There are several points of confusion that I'm stuck on. I'll mention two here:
- The guide mentions a file that it relies on:
/// <reference path='typings/Angularjs/angular.d.ts' />
/// <reference path="typings/Angularjs/angular-route.d.ts" />
/// <reference path='typings/moment/moment.d.ts' />
/// <reference path='app.ts' />
/// <reference path='controller.ts' />
/// <reference path='service' />
It says:
This file keeps track of all your files in your project and when compiling they are compiled into es5, concatenated into one file and contains source maps.
But it doesn't mention what to call this file or what the extension is or where it should reside.
- The guide also mentions that:
We need to use tsify and browserify in conjunction to first compile typescript and then crawl the dependency chain that we lay out with our imports. Then we can bundle up our app with a simple command:
browserify app.ts -p tsify --debug > bundle.js
It doesn't go into any detail on how to do this (as if it leaves it for another article) and when I execute the command in Powershell under ProjectRoot
it tells me The term 'browserify' is not recognized as the name of a cmdlet, function, script file, or operable program.
(even after I npm i browserify
).
So just to start, I'm asking (pretty please) for help with the above 2) hurdles. Does anyone know where to save the file mentioned in 1) and what to call it? And does anyone know how to install and run browserify in order to bundle my app?
Thank you very much.