Giter VIP home page Giter VIP logo

angular-require-lazy's Introduction

angular-require-lazy

An example-experimental application for mixing AngularJS, RequireJS and require-lazy.

Changes

  • (2017/01/31) A long time has passed... And this project is no longer relevant: Angular has moved to 2 (or 4 or whatever), I have moved away of Angular, AMD is now superseeded by the excellent ES6 modules and Webpack does what I ever wanted this project to do; I will be answering questions and issues as and if they appear, but I doubt there is much point in continuing this project; a big thank you to everyone!
  • (2014/10/26) Added support for test coverage and SonarQube integration; documentation is on the way, do check the wiki
  • (2014/10/20) Support for true on-demand Angular module loading (i.e. triggered by an action other than route navigation); see scripts/app/modules/expenses/expensesController and the ngLazy AMD module
  • (2014/10/12) MAJOR changes for simplification; RequireJS is used strictly for script loading, the $injector AMD module is gone
  • (2014/10/03) Added support for testing with Karma.
  • (2014/03/08)
    1. Angular's $injector is now exposed as AMD module; it is no longer a promise.
    2. Enabled module.config() for lazy Angular modules. THIS IS UNTESTED AND EXPERIMENTAL FOR THE TIME BEING.
  • (2013/10/24) Changed the framework to be more Angular-like.
  • (2013/09/15) Added a lazy directive example, see scripts/app/modules/categories/category-directive.js.

Preface

This is an example application for keeping track of personal/family expenses. This is still work in progress and open for discussion. Its main purpose is to demonstrate the mixing of Angular and require-lazy, not to be functionally complete, at least at this stage.

Requirements

  1. Node.js to run the mock server and the build scripts. The node executable must be in PATH for the build scripts to work.
  2. A browser with decent debugging and network traffic analysis capabilities (e.g. latest Firefox+Firebug).

Run

  1. Clone the GIT repository
  2. npm instal
  3. bower install
  4. node app.js to run the server
  5. Hit http://localhost:8110/app.html (this is the unbuilt-development version of the application)

Build, test and run

(Steps 1, 2 & 3 above are prerequisites)

(NOTE: There used to be a standalone script based build system; this was deprecated and removed in favor of Grunt.)

Using Grunt

  1. Make sure grunt-cli is installed and the grunt command is in PATH.
  2. grunt to compile everything
  3. (make sure server is running → node app.js)
  4. Hit http://localhost:8110/app-built.html (this is the built version of the application)

Testing (with Karma/Jasmine)

Run karma start --single-run.

What happens?

This application is set up to load only the scripts it needs for the current view. When the view changes, only the scripts necessary for the new view are loaded.

To observe this behaviour open Firebug to the network tab, then load the unbuilt-development version of the application. Observe the scripts being loaded: ng-grid-XXX.js is not needed and not loaded. Navigate to the "Expenses" view (top menu). ng-grid-XXX.js and the application scripts required for this view are loaded just in time.

The scripts needed for each view are packed together into "bundle" files, when the application is built. So for the expenses view app/modules/expenses/main.js, app/modules/expenses/expenses-view.js, app/modules/expenses/expenses-template.html, and ng-grid-2.0.7-debug.js are bundled into one script file (i.e. expenses-built.js) and the file is loaded with a cache-breaking hash (something like a ?v=e1974633ea3017db85324f449bc6479f request parameter). This process is using r.js and require-lazy.

The noteworthy points are:

  • AngularJS modules can be lazy loaded. Even pure Angular modules, like the demonstrated case with ngGrid (see the "Expenses" view).
  • Directives can be lazy loaded too, using the currentModule AMD module (see scripts/app/modules/categories/categoryDirective.js).
  • There is a "module" discovery mechanism: any directory under app/modules/ that contains a main.js script and a main.metadata.json can automatically appear in the menu (see build-scripts/discoverModules.js). This is used both by the build process and by the server. The discovery mechanism can be implemented by any server-side technology: you need to auto-generate the lazy-registry AMD module.
  • The application is split into bundles automatically using r.js standard configuration and require-lazy; no further configuration is needed.

How?

  • The providers (e.g. $controllerProvider, $compileProvider) are captured from a config function called from bootstrap.js. This function is defined in scripts/lib/angular-require-lazy/lazyAngularUtils.js (cacheInternals()).
  • After bootstrapping, we replace Angular's module() method with a proxy that can handle lazy loaded modules (see makeLazyAngular() and makeLazyModule() in scripts/lib/angular-require-lazy/lazyAngularUtils.js).
  • Templates can be loaded as text through a RequireJS plugin (as "templateCache!path/to/my.html") and registered with Angular's $templateCache with the correct name (here "path/to/my.html").
  • The developer uses the lazy loading mechanism by the special AMD module currentModule. This is a proxy to the currently loading Angular module, providing the familiar API (some methods are still under development).
  • See any view module under WebContent/scripts/app/modules/ to see the implementation of a view; this is the actual code a developer would write, i.e. application code, not framework code.

Testing - Coverage - Sonar

Run grunt test to run the test suite.

Run grunt coverage to make the test coverage report. The output will be in build-coverage/report/output/lcov.info and the HTML version under build-coverage/report/output/lcov-report/index.html.

To have Sonar analyze the project, execute the coverage report first and then, run sonar-runner (make sure it is in the PATH).

Why?

I believe both RequireJS and AngularJS are very useful libraries/frameworks. I am surprized that Angular does not cooperate smoothly with RequireJS out of the box, so I have been experimenting with this implementation.

I believe this can be further improved and I hope it will contribute to a solution bridging the worlds of AMD and Angular modules.

The providers capturing technique (implemented in scripts/lib/angular-require-lazy/lazyAngularUtils.js) is based heavily on angularjs-requirejs-lazy-controllers.

angular-require-lazy's People

Contributors

nikospara avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

angular-require-lazy's Issues

404 Not Found transition.js

I have followed the 5-step-instruction described in the "Run" section and I can reach the page, but it does not show anything. The console log below shows among other things 404 Not Found for transition.js

less: XHR: Getting 'http://localhost:8110/css/bootstrap/bootstrap.less'  less-1.6.2.js:7012:9
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user’s experience. For more help http://xhr.spec.whatwg.org/  less-1.6.2.js:7377:4
less: XHR: Getting 'http://localhost:8110/css/bootstrap/variables.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/mixins.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/reset.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/scaffolding.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/grid.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/layouts.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/type.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/forms.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/component-animations.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/buttons.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/navs.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/navbar.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/modals.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/utilities.less'  less-1.6.2.js:7012:9
"less: parsed http://localhost:8110/css/bootstrap/bootstrap.less successfully."  less-1.6.2.js:7012:9
"less: css for http://localhost:8110/css/bootstrap/bootstrap.less generated in 688ms"  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/custom/style.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/variables.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/custom/variables.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/mixins.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/custom/sprites.less'  less-1.6.2.js:7012:9
"less: parsed http://localhost:8110/css/custom/style.less successfully."  less-1.6.2.js:7012:9
"less: css for http://localhost:8110/css/custom/style.less generated in 428ms"  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/responsive.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/variables.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/mixins.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/responsive-utilities.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/responsive-1200px-min.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/responsive-768px-979px.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/responsive-767px-max.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/responsive-navbar.less'  less-1.6.2.js:7012:9
"less: parsed http://localhost:8110/css/bootstrap/responsive.less successfully."  less-1.6.2.js:7012:9
"less: css for http://localhost:8110/css/bootstrap/responsive.less generated in 265ms"  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/custom/overrides.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/variables.less'  less-1.6.2.js:7012:9
less: XHR: Getting 'http://localhost:8110/css/bootstrap/mixins.less'  less-1.6.2.js:7012:9
"less: parsed http://localhost:8110/css/custom/overrides.less successfully."  less-1.6.2.js:7012:9
"less: css for http://localhost:8110/css/custom/overrides.less generated in 193ms"  less-1.6.2.js:7012:9
less: css generated in 1577ms  less-1.6.2.js:7012:9
GET 
http://localhost:8110/scripts/lib/angular-ui-bootstrap/src/transition/transition.js [HTTP/1.1 404 Not Found 15ms]
Error: Script error for "lib/angular-ui-bootstrap/src/transition/transition"
http://requirejs.org/docs/errors.html#scripterror  require.js:168:17
Error: scripts/lib/angular-ui-bootstrap/template/modal/backdrop.html HTTP status: 404  text.js:311:31
Error: Load timeout for modules: lib/angular-ui-bootstrap/src/modal/modal,lib/angular-ui-bootstrap/src/collapse/collapse
http://requirejs.org/docs/errors.html#timeout  require.js:168:17

Lazy directive

It should work so

template:

<chat ng:lazy-directive="js/chatLazyDirective.js" />

After first load it should be cached.

npm start : return error

after npm install - npm start will return following error log

0 info it worked if it ends with ok
1 verbose cli [ 'c:\Program Files (x86)\nodejs\node.exe',
1 verbose cli 'c:\Program Files (x86)\nodejs\node_modules\npm\bin\npm-cli.js',
1 verbose cli 'start' ]
2 info using [email protected]
3 info using [email protected]
4 verbose stack Error: missing script: start
4 verbose stack at run (c:\Program Files (x86)\nodejs\node_modules\npm\lib\run-script.js:165:19)
4 verbose stack at c:\Program Files (x86)\nodejs\node_modules\npm\lib\run-script.js:81:5
4 verbose stack at c:\Program Files (x86)\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:52:40
4 verbose stack at c:\Program Files (x86)\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:376:33
4 verbose stack at checkBinReferences_ (c:\Program Files (x86)\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:341:59)
4 verbose stack at final (c:\Program Files (x86)\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:373:17)
4 verbose stack at then (c:\Program Files (x86)\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:127:33)
4 verbose stack at c:\Program Files (x86)\nodejs\node_modules\npm\node_modules\read-package-json\read-json.js:267:40
4 verbose stack at evalmachine.:334:14
4 verbose stack at c:\Program Files (x86)\nodejs\node_modules\npm\node_modules\graceful-fs\graceful-fs.js:102:5
5 verbose cwd c:\users\emoimas\Downloads\angular-require-lazy-master (1)\angular-require-lazy-master
6 error Windows_NT 6.1.7601
7 error argv "c:\Program Files (x86)\nodejs\node.exe" "c:\Program Files (x86)\nodejs\node_modules\npm\bin\npm-cli.js" "start"
8 error node v0.12.2
9 error npm v2.7.4
10 error missing script: start
11 error If you need help, you may report this error at:
11 error https://github.com/npm/npm/issues
12 verbose exit [ 1, true ]

"Unbootstrapping" angular app

Hi Nikos!

I know the project is not maintained anymore, but I was wondering if you have ever found a way to "unbootstrap" an angular app? I'm bootstrapping the application manually, and would like to be able to remove any event listeners, dom nodes etc. on demand. Do you think that's possible at all?

Config Dependencies

Hi
Did you find a way to make Dependencies for the module.config working?
Cheers
Bernd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.