can-compile
NodeJS module that compiles CanJS EJS and Mustache views into a single JavaScript file for lightning fast production apps.
With NodeJS installed, just run NPM:
npm install can-compile -g
Command line
The can-compile
command line tool takes a list of files (by default all *.ejs
and *.mustache
files in the current folder)
or a list of filename patterns and writes the compiled views into an out
file
(default: views.production.js
).
Examples:
Compile all EJS and Mustache files in the current folder and write them to views.combined.js
using version 2.1.0:
can-compile --out views.combined.js --can 2.1.0
Compile todo.ejs
using CanJS version 1.1.2, write it to views.production.js
:
can-compile todo.ejs --can 1.1.2
Compile all EJS files in the current directory and all subdirectories and mustache/test.mustache
.
Write the result to views.combined.js
:
can-compile **/*.ejs mustache/test.mustache --out views.combined.js --can 2.0.0
Grunt task
can-compile also comes with a Grunt task so you can easily make it part of your production build.
Just npm install can-compile
in you project folder (or add it as a development dependency).
The following example shows a Gruntfile that compiles all Mustache views and then builds a concatenated and minified production.js
of a CanJS application:
module { // Project configuration. grunt; // Default task. grunt; grunt; grunt; grunt;};
Gulp task
It is also quite easy to get this up and running with your production build using Gulp. By placing the following example in your gulpfile.js, all mustache templates in the client/app directory will be compiled into a file at public/assets/views.production.js
.
var gulp = ;var compilerGulp = ; var options = version: '2.2.7'; gulp;
Or you can use available helpers to create your task and watch your files:
var gulp = ;var compilerGulp = ; var options = version: '2.2.7' src: 'client/app/**/*.mustache' out: 'public/assets/views.production.js' tags: 'editor' 'my-component'; // Creates a task called 'app-views'.// You must pass in the same gulp instance.// You may also pass optional task dependencies (ex. 'clean')compilerGulp; // Creates a task called 'app-views-watch'. Optional, but convenient.compilerGulp; // The default task (called when you run `gulp` from cli)gulp;
You'll need gulp installed globally and locally:
sudo npm install gulp -g
npm install gulp
And a local copy of can-compile
npm install can-compile
Run gulp
in the command line to build.
Programmatically
You can compile individual files directly like this:
var compiler = ;var options = filename: 'file.ejs' version: '2.0.1'; compiler;
The options object allows the following configuration options:
filename
{String}: The name of the file to be compiledversion
{String}: The CanJS version to be usedlog
{Function}: A logger function (e..gconsole.log.bind(console)
)normalizer
{Function}: A Function that returns the normalized path nametags
{Array}: A list of all your can.Component tags. They need to be registered in order to pre-compile views properly.extensions
{Object}: An object to map custom file extensions to the standard extension (e.g.{ 'mst' : 'mustache' }
)viewAttributes
{Array}: A list of attribute names (RegExp or String), used for additional behavior for an attribute in a view (can.view.attr)paths
an object withejs
,mustache
orstache
and ajquery
property pointing to files of existing versions or CanJS and jQuery instead of the CDN links.
compiler;
Loading with RequireJS
To use your pre-compile views with RequireJS just add a custom wrapper
in the options
that uses the AMD definition to load can/view/mustache
and/or can/view/ejs
(depending on what you are using).
In a Grunt task:
Mustache:
module { // Project configuration. grunt;}
Stache:
module { // Project configuration. grunt;}
To load the generated files only when running the RequireJS optimizer r.js define an empty module in development like:
;
And require('views');
in your main application file.
When running the optimizer map this module to the production build file:
paths: views: 'views.production'
Note
Always make sure that the output file is in the same folder as the root level for the views that are being loaded.
So if your CanJS applications HTML file is in the app
folder within the current directory use a filename within
that folder as the output file:
can-compile --out app/views.production.js --can 2.0.0
Changelog
0.10.0:
0.9.0:
0.8.0:
- Fixed path normalize on Windows, Alternative file extension, can.view.attr (#32)
0.7.1:
0.7.0:
- Added up to date TodoMVC based example
- Made
version
flag mandatory (caused unexpected behaviour after CanJS updates) - Added CanJS 2.1 compatibility (#20, #21, #22)
- Fixed Grunt options (#8)
0.6.0:
0.5.0:
- Merged #11: Implement can.Component/tag-support
0.4.1:
- Merged #10: Allow for setting a custom normalizer
0.4.0:
- Verify CanJS 2.0.0 compatbility, load can.EJS which isn't in the core anymore
0.3.2:
- Custom
wrapper
option uses Handlebars because Underscore templates are useless in Grunt files
0.3.1:
- Adds a custom
wrapper
option (uses _.template).
0.3.0:
- Allows compilation for different CanJS versions
0.2.1:
- Switched to plain JSDom
- Update to CanJS 1.1.5
- Verified Node 0.10 compatibility
0.2.0:
- Grunt 0.4.0 compatibility
- Added Travis CI
0.1.0:
- Initial release
License
Copyright (C) 2014 David Luecke daff@neyeon.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.