https://github.com/chadhietala/ember-cli-lazy-code
Turns your JS into JSON and leverages Ember's IoC system to JIT modules
https://github.com/chadhietala/ember-cli-lazy-code
Last synced: 8 months ago
JSON representation
Turns your JS into JSON and leverages Ember's IoC system to JIT modules
- Host: GitHub
- URL: https://github.com/chadhietala/ember-cli-lazy-code
- Owner: chadhietala
- License: mit
- Created: 2016-04-09T18:52:59.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-10-19T07:37:28.000Z (about 4 years ago)
- Last Synced: 2025-03-30T00:51:14.057Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 47.9 KB
- Stars: 5
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Ember-cli-lazy-code
This addon is an inspiration from Google's startup latency [blog](http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html).
This addon converts the app file's AMD modules into a string equivalent format. It primarily is used to
lazily evaluate AMD modules by converting into an Object literal format that can be JSON stringified.
This is very useful on low powered devices where we want to lazily evaluate javascript and only evaluate Javascript that is needed in the initial load.
Consider the following AMD modules:
```javascript
define('a', ['exports', 'b', 'c'], function(_exports, _a, _b) {
_exports.add = function add(n1, n2) {
return _a.default(n1) + _b.default(n2);
}
});
define('b', ['exports', 'c'], function(_exports, _c) {
_exports.default = function addOne(n) {
return _c.default(n) + 1;
}
});
define('c', ['exports'], function(_exports) {
_exports.default = function id(n) {
return n;
}
});
```
With an AMD module there 3 parts of the signature:
1. The module id `string`
2. The modules dependencies ids `Array`
3. The anonymous function that executes the modules code. Called with the reified dependecies.
With this addon, we write an AST transform that captures the call of the `define` and re-writes it into an Object literal structure that can be `JSON.stringify`ed.
After the build, all app's AMD modules will be in this format by default:
```javascript
{
"a": {
"imports": ["exports", "b", "c"],
"arguments": ["_exports", "_b", "_c"],
"body": "_exports.add = function add(n1, n2) { return _a.default(n1) + _b.default(n2); }"
},
"b": {
"imports": ["exports", "c"],
"arguments": ["_exports", "_c"],
"body": "_exports.default = function addOne(n) { return _c.default(n) + 1; }"
},
"c": {
"imports": ["exports"],
"arguments": ["_exports"],
"body": "_exports.default = function id(n) { return n;}"
}
}
```
This addon only supports the `strings` mode currently. In future, we may add variations to this.
Since the format of the modules has been changed, we need to teach the [loader.js](https://github.com/kratiahuja/loader.js/blob/master/lib/loader/loader.js#L284) to recognize this new format.
## Note
When using this addon, please use [ember-cli-string-module-loader](https://github.com/kratiahuja/ember-cli-string-module-loader) addon instead of [loader.js](https://github.com/ember-cli/loader.js) addon.
## Installation
* `git clone` this repository
* `npm install`
* `bower install`
## Running
* `ember server`
* Visit your app at http://localhost:4200.
## Running Tests
* `npm test` (Runs `ember try:testall` to test your addon against multiple Ember versions)
* `ember test`
* `ember test --server`
## Building
* `ember build`
For more information on using ember-cli, visit [http://www.ember-cli.com/](http://www.ember-cli.com/).