Precompile Mithril views which use JSX into JavaScript by insin
This plugin requires Grunt.
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-msx --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-msx');
In your project's Gruntfile, add a section named msx
to the data object passed into grunt.initConfig()
.
grunt.initConfig({
msx: {
your_target: {
// Target-specific file lists and/or options go here.
},
},
})
Working on
In this example, the app
option are used to convert all files matched test/fixtures/*.jsx
to Mithril views.
grunt.initConfig({
msx: {
app: {
files: [{
expand: true,
cwd: 'test/fixtures',
src : '**/*.jsx',
dest : 'test/expected',
}]
}
},
})
browserify: {
app: {
files: { '<%= yeoman.app %>/scripts/main.js': ['<%= yeoman.app %>/jsx/main.jsx'] },
options: {
transform: [require('grunt-msx').browserify]
}
}
},
todo.view = function(ctrl) {
return <html>
<body>
<input onchange={m.withAttr("value", ctrl.description)} value={ctrl.description()}/>
<button onclick={ctrl.add.bind(ctrl, ctrl.description)}>Add</button>
<table>
{ctrl.list.map(function(task, index) {
return <tr>
<td>
<input
type="checkbox"
onclick={m.withAttr("checked", task.done)}
checked={task.done()}
/>
</td>
<td style={{textDecoration: task.done() ? "line-through" : "none"}}>
{task.description()}
</td>
</tr>
})}
</table>
</body>
</html>
};
To
todo.view = function(ctrl) {
return m("html", [
m("body", [
m("input", {onchange:m.withAttr("value", ctrl.description), value:ctrl.description()}),
m("button", {onclick:ctrl.add.bind(ctrl, ctrl.description)}, ["Add"]),
m("table", [
ctrl.list.map(function(task, index) {
return m("tr", [
m("td", [
m("input",
{type:"checkbox",
onclick:m.withAttr("checked", task.done),
checked:task.done()}
)
]),
m("td", {style:{textDecoration: task.done() ? "line-through" : "none"}}, [
task.description()
])
])
})
])
])
])
};
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
Copyright (c) 2014 Hung Quang Phan. Licensed under the MIT license.