Permalink
Browse files

Grunt task, command line, performance improvements

  • Loading branch information...
daffl committed Nov 30, 2012
1 parent ab30f99 commit bcebb9d2e03f74f383e62d6a5ffad84be39abd5d
View
@@ -12,4 +12,5 @@ logs
results
node_modules
-npm-debug.log
+npm-debug.log
+resources/js/resources.js
View
@@ -11,6 +11,10 @@ program.version(pkg.version)
.parse(process.argv);
var files = [];
+if(!program.args || program.args.length === 0) {
+ program.args = ['**/*.ejs', '**/*.mustache'];
+}
+
program.args.forEach(function(pattern) {
files = files.concat(globsync.glob(pattern));
});
View
@@ -1,11 +0,0 @@
-<!DOCTYPE html>
-<html>
-<head>
- <title>can.compile loader file</title>
-</head>
-<body>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.js"></script>
- <script type="text/javascript" src="http://canjs.us/release/latest/can.jquery.js"></script>
- <script type="text/javascript" src="http://canjs.us/release/latest/can.view.mustache.js"></script>
-</body>
-</html>

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,37 @@
+/*global module:false*/
+module.exports = function (grunt) {
+
+ // Project configuration.
+ grunt.initConfig({
+ pkg : '<json:package.json>',
+ meta : {
+ resources : 'resources/js'
+ },
+ concat : {
+ resources : {
+ src : [ '<%= meta.resources %>/jquery.1.8.3.js', '<%= meta.resources %>/can.jquery.1.1.2.js',
+ '<%= meta.resources %>/can.view.mustache.1.1.2.js' ],
+ dest : '<%= meta.resources %>/resources.js'
+ }
+ },
+ min : {
+ resources : {
+ src : '<%= meta.resources %>/resources.js',
+ dest : '<%= meta.resources %>/resources.min.js'
+ }
+ },
+ cancompile : {
+ all : {
+ src : ['**/*.ejs', '**/*.mustache'],
+ // Change into the example folder
+ out : 'example/views.production.js'
+ }
+ }
+ });
+
+ grunt.loadTasks("./tasks");
+ // Default task.
+ grunt.registerTask('resources', 'concat:resources min:resources');
+ grunt.registerTask('default', 'resources');
+
+};
View
@@ -1,26 +1,34 @@
var Zombie = require("zombie");
var fs = require('fs');
-module.exports = function (files, callback) {
- Zombie.visit("file://" + __dirname + "/../empty.html", {
+module.exports = function (files, normalize, callback) {
+ files = Array.isArray(files) ? files : [files];
+ var normalizer = function(filename) {
+ return filename;
+ }
+ if(!callback) {
+ callback = normalize;
+ } else {
+ normalizer = normalize;
+ }
+
+ Zombie.visit("file://" + __dirname + "/../resources/loader.html", {
runScripts : true
}, function (err, browser, status) {
if (err) return callback(err);
var window = browser.document.window;
var can = window.can;
- var getScript = function (id, type, text) {
- if (!text && !type) {
- type = id.substring(id.lastIndexOf('.') + 1, id.length);
- text = fs.readFileSync(id).toString();
- }
- id = can.view.toId(id);
+ var getScript = function (filename) {
+ var type = filename.substring(filename.lastIndexOf('.') + 1, filename.length);
+ var text = fs.readFileSync(filename).toString();
+ // Create an id from the normalized filename
+ var id = can.view.toId(normalizer(filename));
+ // TODO throw error if type is not registered
var script = can.view.types["." + type].script(id, text);
return "can.view.preload('" + id + "'," + script + ");";
}
- var result = files.map(function(filename) {
- return getScript(filename);
- });
+ var result = files.map(getScript);
callback(null, result);
});
View
@@ -1,12 +1,23 @@
var compiler = require('./compile');
var fs = require('fs');
+var path = require('path');
module.exports = function(files, configuration, callback) {
- compiler(files, function(err, compiled) {
+ // Normalize ids to filenames relative to the output file
+ var normalizer = function(filename) {
+ return path.relative(path.dirname(configuration.out), filename);
+ }
+
+ compiler(files, normalizer, function(err, compiled) {
+ if(err) {
+ return callback(err);
+ }
+
if(!callback) {
callback = configuration;
configuration = {};
}
+
var outfile = configuration.out;
var output = '(function(window, undefined) {\nvar can = window.can;\n' +
compiled.join('\n') +
View
@@ -26,4 +26,51 @@ Compile all EJS and Mustache files in the current directory and all subdirectori
can-compile also comes with a [Grunt](http://gruntjs.com) task so you can easily make it part of your production build.
-## Programmatically
+module.exports = function (grunt) {
+
+```javascript
+// Project configuration.
+grunt.initConfig({
+ pkg : '<json:package.json>',
+ meta : {
+ resources : 'resources/js'
+ },
+ concat : {
+ resources : {
+ src : [ '<%= meta.resources %>/jquery.1.8.3.js', '<%= meta.resources %>/can.jquery.1.1.2.js',
+ '<%= meta.resources %>/can.view.mustache.1.1.2.js' ],
+ dest : '<%= meta.resources %>/resources.js'
+ }
+ },
+ min : {
+ resources : {
+ src : '<%= meta.resources %>/resources.js',
+ dest : '<%= meta.resources %>/resources.min.js'
+ }
+ },
+ cancompile : {
+ all : {
+ src : ['**/*.ejs', '**/*.mustache'],
+ // Change into the example folder
+ out : 'views.production.js'
+ }
+ }
+ });
+
+ grunt.loadTasks("./tasks");
+ // Default task.
+ grunt.registerTask('resources', 'concat:resources min:resources');
+ grunt.registerTask('default', 'resources');
+
+};
+```
+
+## Programmatically
+
+```javascript
+var compiler = require('can-compile');
+
+compiler.compile('file.ejs', function(error, output) {
+ output // -> compiled `file.ejs`
+});
+```
Oops, something went wrong.

0 comments on commit bcebb9d

Please sign in to comment.