Boilerplate for Single Page Applications built on top of AngularJS and browserify. Covers building application in separate environments, dependency management, directory structure and coding conventions.
In-browser dependencies are managed by bower. By default boilerplate uses:
- AngularJS 1.3
- jQuery 2.1 (can be removed if not needed)
angular-ui-routerbower package
To install additional libraries, use bower with --save flag and edit concat:vendor task in Gruntfile.js. After adding any new library, you have to run grunt vendor again.
$ bower install --save moment.jsCode is organized in CommonJS modules. These are bundled altogether by browserify. Build tasks are defined in Gruntfile.js. Default grunt task runs server on localhost:8000 and automatically rebuilds application on any source change. Rebuilded application is pushed into browser right away (livereload).
Significant tasks:
grunt build-dev,grunt build-prod- build application indev/prodmodegrunt lint- lint project using.jshintrcfile (default included in project)grunt vendor- reinstall and rebundle JavaScript vendorsgrunt publish- synchronizepublic/with S3 bucketgrunt- serve application with livereload
When extending Gruntfile.js with other npm packages, use --save-dev flag:
$ npm install grunt-contrib-sass --save-devCSS bundle is created by concatenation of all CSS files in lib/ dictionary.
config/- configuration filesfronts/- raw versions of*.htmlfileslib/{module-name}/- module files (*.js,*.css,*.html)lib/{module-name}/tpl- templates (*.html) for module, these can be required as string (require('./tpl/list.html)lib/{module-name}/controllers,lib/{module-name}/directives- AngularJS specific codepublic/- bundled application files, can be used as webroot or synchronized with some cloud storage (eg. S3 bucket)
You only have to have node.js installed. bower and browserify are installed locally.
$ git clone git@github.com:jelz/spa-boilerplate.git
$ cd spa-boilerplate
$ cp config/config.json.dist config/config.json
$ npm install
$ grunt- create
lib/{module-name}/directory withindex.jsfile in there - create AngularJS module in this file, export it from the module (
module.exports = ...;) - update
lib/app/modules.js, add dependency to main AngularJS module inlib/app/index.js
Use grunt publish to push code into S3 bucket. Provide AWS credentials and configuration in config/aws.json.
$ cp config/aws.json.dist config/aws.json
$ vi config/aws.json
$ grunt publishOnly changed files will be pushed. Bucket has to exist. No policy or website configuration will be added.
UserState Angular service provides basic token-based authentication flow. Calling UserState.login() redirects user to login service page (loginUrl in config.json) with query string parameter ?redirect_uri=.... This service should authenticate user, issue a token and call SPA back with it:
- if
redirect_uriis a domain name without trialing slash, it should be appended with/#___{token-value} - if
redirect_uriends with slash or file name, it should be appended with#___{token-value} - if
redirect_uricontains hash (#), then it should be appended with___{token-value}
Any further $http requests will have X-Access-Token (can be changed in config.json - tokenHeaderName property) header set. Calling UserState.logout() clears all user-related data.
public/login/mock/login.js implements mocked login flow.
MIT