point to mithril-objectify

This commit is contained in:
Leo Horie 2015-12-02 22:06:05 -05:00
parent b12453b156
commit b121ba2824

View file

@ -10,13 +10,13 @@ For example, if you are building a table with thousands of rows and finding that
## Compiling templates
You can optionally pre-compile templates that use `m()` by running the [`template-compiler.sjs`](tools/template-compiler.sjs) macro with [Sweet.js](https://github.com/mozilla/sweet.js). This step isn't required in order to use Mithril, but it's an easy way to squeeze a little bit more performance out of an application, without the need for code changes.
You can optionally pre-compile templates that use `m()` by using [mithril-objectify](https://github.com/tivac/mithril-objectify/). This step isn't required in order to use Mithril, but it's an easy way to squeeze a little bit more performance out of an application, without the need for code changes.
Compiling a template transforms the nested function calls of a template into a raw virtual DOM tree (which is merely a collection of native Javascript objects that is ready to be rendered via [`m.render`](mithril.render.md)). This means that compiled templates don't need to parse the string in `m("div#foo")` and they don't incur the cost of the function call.
It's worth mentioning that Mithril has built-in mechanisms elsewhere that take care of real bottlenecks like browser repaint management and DOM updating. This optional compilation tool is merely "icing on the cake" that speeds up the Javascript run-time of templates (which is already fast, even without compilation - see the [performance section on the homepage](http://lhorie.github.io/mithril/index.html#performance)).
The macro takes regular Mithril templates like the one below:
The tool takes regular Mithril templates like the one below:
```javascript
var view = function() {
@ -36,71 +36,18 @@ Note that compiled templates are meant to be generated by an automated build pro
---
### Installing NodeJS and SweetJS for one-off compilations
### Installing
SweetJS requires a [NodeJS](http://nodejs.org) environment. To install it, go to its website and use the installer provided.
Mithril-objectify requires a [NodeJS](http://nodejs.org) environment. To install it, go to its website and use the installer provided.
To install SweetJS, NodeJS provides a command-line package manager tool. In a command line, type:
To install mithril-objectify, NodeJS provides a command-line package manager tool. In a command line, type:
```
npm install -g sweet.js
npm install -g mithril-objectify
```
To compile a file, type:
Then, to compile a file, type:
```
sjs -r -m /template-compiler.sjs -o <output-filename>.js <input-filename>.js
mithril-objectify ./input-filename.js ./output-filename.js
```
---
### Automating Compilation
If you want to automate compilation, you can use [GruntJS](http://gruntjs.com), a task automation tool. If you're not familiar with GruntJS, you can find a tutorial on their website.
Assuming NodeJS is already installed, run the following command to install GruntJS:
```
npm install -g grunt-cli
```
Once installed, create two files in the root of your project, `package.json` and `Gruntfile.js`
`package.json`
```javascript
{
"name": "project-name-goes-here",
"version": "0.0.0", //must follow this format
"devDependencies": {
"grunt-sweet.js": "*"
}
}
```
`Gruntfile.js`
```javascript
module.exports = function(grunt) {
grunt.initConfig({
sweetjs: {
modules: ["template-compiler.sjs"],
compile: {expand: true, cwd: ".", src: "**/*.js", dest: "destination-folder-goes-here/"}
}
});
grunt.loadNpmTasks('grunt-sweet.js');
grunt.registerTask('default', ['sweetjs']);
}
```
Make sure to replace the `project-name-goes-here` and `destination-folder-goes-here` placeholders with appropriate values.
To run the automation task, run the following command from the root folder of your project:
```
grunt
```
More documentation on the grunt-sweet.js task and its options [can be found here](https://github.com/natefaubion/grunt-sweet.js)