Here is the design that I work with. I found it useful on two larger projects I built and haven't hit any road blocks so far.
Required Libraries for this Setup
- Angular
- Angular UI Router
Folder Structure
your-project/
apps/
global.html
app1/
index.html
app1.module.js
app1.js
parts/
foo.js
foo.html
...
app2/
libs
lib1/
lib1.module.js
parts/
directive1.js
directive1.html
lib2/
third-party/
- Configure your server web framework to find
apps/app1/index.htmlwhen a request for/app1comes in. Use friendly URLs (e.g.the-first-application/instead ofapp1/and map them using your server technology if required. - Your server technology must include
global.htmlinindex.htmlbecause it contains the vendor includes (see below). - Include the assets required by the respective app in
index.html(see below). - Put the
ng-appand the root<div ui-view></div>in theindex.html. - Every app and lib is a separate angular module.
- Every app gets a
<app-name>.module.jsfile that contains the angular module definition and dependency list. - Every app gets a
<app-name>.jsfile that contains the modules' config and run blocks, and the routing config as part of it. - Every app gets a
partsfolder that contains the applications' controllers, views, services and directives in a structure that makes sense for the specific app. I don't consider sub-folders likecontrollers/,views/etc helpful, because they don't scale, but YMMV. - Libs follow the same structure as apps, but leave things out that they don't need (obviously).
Start out with services and directives within the app where they are used. As soon as you need something in another app as well, refactor to a library. Try to create functionally consistent libraries, not just all-directive or all-services libraries.
Assets
I minify both JS and CSS files for release builds but work with unminified files during development. Here's a setup that support this:
- Manage vendor includes globally in
global.html. This way, the heavy stuff can be loaded from cache when navigating between SPAs. Make sure caching works appropriately. - Per-SPA assets are defined in
index.html. This should only include the app-specific files as well as the used libraries.
The folder structure above makes it easy to find the right files for the minification build steps.