can-compile
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -27,3 +27,4 @@ 'use strict'; | ||
} | ||
} | ||
}, | ||
release: {} | ||
}); | ||
@@ -35,2 +36,3 @@ | ||
grunt.loadNpmTasks('grunt-contrib-jshint'); | ||
grunt.loadNpmTasks('grunt-release'); | ||
}; |
@@ -5,9 +5,10 @@ 'use strict'; | ||
var jsdom = require("jsdom"); | ||
var window = null; | ||
var visit = function (callback) { | ||
if(window === null) { | ||
var window = {}; | ||
var visit = function (version, callback) { | ||
if(!window[version]) { | ||
// TODO jQuery version mapping | ||
jsdom.env('<h1>can-compile</h1>', | ||
[ __dirname + '/../resources/jquery.js', | ||
__dirname + '/../resources/can.jquery.js', | ||
__dirname + '/../resources/can.view.mustache.js' ], | ||
[ 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', | ||
'http://canjs.com/release/' + version + '/can.jquery.js', | ||
'http://canjs.com/release/' + version + '/can.view.mustache.js' ], | ||
function (error, win) { | ||
@@ -18,24 +19,20 @@ if(error) { | ||
window = win; | ||
callback(null, window); | ||
window[version] = win; | ||
callback(null, window[version]); | ||
} | ||
); | ||
} else { | ||
callback(null, window); | ||
callback(null, window[version]); | ||
} | ||
}; | ||
module.exports = function (filename, normalize, callback, log) { | ||
var normalizer = function (filename) { | ||
module.exports = function (options, callback) { | ||
var filename = typeof options === 'string' ? options : options.filename; | ||
var normalizer = options.normalizer || function (filename) { | ||
return filename; | ||
}; | ||
if (!callback) { | ||
callback = normalize; | ||
} else { | ||
normalizer = normalize; | ||
} | ||
visit(function (error, win) { | ||
if (log) { | ||
log('Compiling ' + filename); | ||
visit(options.version || 'latest', function (error, win) { | ||
if (typeof options.log === 'function') { | ||
options.log('Compiling ' + filename); | ||
} | ||
@@ -42,0 +39,0 @@ |
@@ -16,3 +16,8 @@ 'use strict'; | ||
return function (callback) { | ||
compiler(file, normalizer, function (error, compiled, id) { | ||
compiler({ | ||
filename: file, | ||
normalizer: normalizer, | ||
log: log, | ||
version: configuration.version | ||
}, function (error, compiled, id) { | ||
if (error) { | ||
@@ -22,3 +27,3 @@ return callback(error); | ||
callback(null, compiled, id); | ||
}, log); | ||
}); | ||
}; | ||
@@ -46,2 +51,2 @@ }); | ||
module.exports.compile = compiler; | ||
module.exports.compile = compiler; |
{ | ||
"name": "can-compile", | ||
"description": "Compile CanJS Mustache and EJS views for lightning fast production apps", | ||
"version": "0.2.1", | ||
"homepage": "http://daffl.github.com/can-compile", | ||
"author": { | ||
"name": "David Luecke", | ||
"email": "daff@neyeon.com", | ||
"web": "http://daffl.github.com" | ||
}, | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "mocha test" | ||
}, | ||
"engines": { | ||
"node": "~0.10" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:daffl/can-compile.git" | ||
}, | ||
"bin": { | ||
"can-compile": "./bin/can-compile" | ||
}, | ||
"keywords": [ | ||
"gruntplugin", | ||
"canjs" | ||
], | ||
"devDependencies": { | ||
"expect.js": ">= 0.1.2", | ||
"mocha": "1.8.1", | ||
"grunt": "~ 0.4.0", | ||
"grunt-contrib-jshint": ">= 0.1.1" | ||
}, | ||
"dependencies": { | ||
"glob-whatev": "0.1.8", | ||
"commander": "1.1.1", | ||
"async": "0.1.22", | ||
"jsdom": "~0.5.6" | ||
}, | ||
"peerDependencies": { | ||
"grunt": "~0.4.0" | ||
}, | ||
"scripts": { | ||
"test": "mocha test" | ||
} | ||
"name": "can-compile", | ||
"description": "Compile CanJS Mustache and EJS views for lightning fast production apps", | ||
"version": "0.3.0", | ||
"homepage": "http://daffl.github.com/can-compile", | ||
"author": { | ||
"name": "David Luecke", | ||
"email": "daff@neyeon.com", | ||
"web": "http://daffl.github.com" | ||
}, | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "mocha test" | ||
}, | ||
"engines": { | ||
"node": "~0.10" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:daffl/can-compile.git" | ||
}, | ||
"bin": { | ||
"can-compile": "./bin/can-compile" | ||
}, | ||
"keywords": [ | ||
"gruntplugin", | ||
"canjs" | ||
], | ||
"devDependencies": { | ||
"expect.js": ">= 0.1.2", | ||
"mocha": "1.8.1", | ||
"grunt": "~ 0.4.0", | ||
"grunt-contrib-jshint": ">= 0.1.1", | ||
"grunt-release": "~0.5.1" | ||
}, | ||
"dependencies": { | ||
"glob-whatev": "0.1.8", | ||
"commander": "1.1.1", | ||
"async": "0.1.22", | ||
"jsdom": "~0.5.6" | ||
}, | ||
"peerDependencies": { | ||
"grunt": "~0.4.0" | ||
} | ||
} |
@@ -22,5 +22,5 @@ # can-compile | ||
Compile `todo.ejs`, write it to `views.production.js`: | ||
Compile `todo.ejs` using CanJS version 1.1.2, write it to `views.production.js`: | ||
> can-compile todo.ejs | ||
> can-compile todo.ejs --can 1.1.2 | ||
@@ -42,36 +42,43 @@ Compile all EJS files in the current directory and all subdirectories and `mustache/test.mustache`. | ||
// Project configuration. | ||
grunt.initConfig({ | ||
cancompile: { | ||
dist: { | ||
src: ['**/*.ejs', '**/*.mustache'], | ||
out: 'production/views.production.js' | ||
} | ||
}, | ||
concat: { | ||
dist: { | ||
src: [ | ||
'../resources/js/can.jquery.js', | ||
'../resources/js/can.view.mustache.js', | ||
'js/app.js', // You app | ||
'<%= cancompile.dist.out %>' // The compiled views | ||
], | ||
dest: 'production/production.js' | ||
} | ||
}, | ||
uglify: { | ||
dist: { | ||
files: { | ||
'production/production.min.js': ['<%= concat.dist.dest %>'] | ||
} | ||
} | ||
} | ||
}); | ||
// Project configuration. | ||
grunt.initConfig({ | ||
cancompile: { | ||
dist: { | ||
src: ['**/*.ejs', '**/*.mustache'], | ||
out: 'production/views.production.js' | ||
}, | ||
legacy: { | ||
src: ['**/*.ejs', '**/*.mustache'], | ||
out: 'production/views.production.js', | ||
options: { | ||
version: '1.1.2' | ||
} | ||
} | ||
}, | ||
concat: { | ||
dist: { | ||
src: [ | ||
'../resources/js/can.jquery.js', | ||
'../resources/js/can.view.mustache.js', | ||
'js/app.js', // You app | ||
'<%= cancompile.dist.out %>' // The compiled views | ||
], | ||
dest: 'production/production.js' | ||
} | ||
}, | ||
uglify: { | ||
dist: { | ||
files: { | ||
'production/production.min.js': ['<%= concat.dist.dest %>'] | ||
} | ||
} | ||
} | ||
}); | ||
// Default task. | ||
grunt.registerTask('default', ['cancompile', 'concat', 'uglify']); | ||
// Default task. | ||
grunt.registerTask('default', ['cancompile', 'concat', 'uglify']); | ||
grunt.loadTasks('../tasks'); | ||
grunt.loadNpmTasks('grunt-contrib-uglify'); | ||
grunt.loadNpmTasks('grunt-contrib-concat'); | ||
grunt.loadNpmTasks('can-compile'); | ||
grunt.loadNpmTasks('grunt-contrib-uglify'); | ||
grunt.loadNpmTasks('grunt-contrib-concat'); | ||
}; | ||
@@ -92,2 +99,22 @@ ``` | ||
Passing an object as the first parameter allows you the following configuration options: | ||
- `filename` {String}: The name of the file to be compiled | ||
- `version` {String} (default: `latest`): The CanJS version to be used | ||
- `log` {Function}: A logger function (e..g `console.log.bind(console)`) | ||
- `normalizer` {Function}: A Function that returns the normalized path name | ||
```javascript | ||
compiler.compile({ | ||
filename: 'file.ejs', | ||
log: console.log.bind(console), | ||
normalizer: function(filename) { | ||
return path.relative(__dirname, filename); | ||
}, | ||
version: '1.1.6' | ||
}, function(error, output) { | ||
output // -> compiled `file.ejs` | ||
}); | ||
``` | ||
## Note | ||
@@ -103,2 +130,6 @@ | ||
__0.3.0:__ | ||
- Allows compilation for different CanJS versions | ||
__0.2.1:__ | ||
@@ -105,0 +136,0 @@ |
@@ -15,3 +15,3 @@ 'use strict'; | ||
if(err) { | ||
return grunt.fail.fatal(err); | ||
return done(err); | ||
} | ||
@@ -23,2 +23,2 @@ | ||
}); | ||
}; | ||
}; |
109
test/test.js
/*global describe, it */ | ||
/*jshint -W083 */ | ||
'use strict'; | ||
@@ -7,45 +8,79 @@ | ||
var path = require('path'); | ||
var expected = { | ||
'1.1.5': { | ||
ejs: "can.EJS(function(_CONTEXT,_VIEW) { " + | ||
"with(_VIEW) { with (_CONTEXT) {var ___v1ew = [];___v1ew.push(\"<h2>\");" + | ||
"___v1ew.push(can.view.txt(1,'h2',0,this,function(){ " + | ||
"return message }));___v1ew.push(\"</h2>\");; return ___v1ew.join('')}} })", | ||
mustache: 'can.Mustache(function(_CONTEXT,_VIEW) { with(_VIEW) { with (_CONTEXT) {var ___v1ew = [];var ___c0nt3xt = this && this.___st4ck3d ? this : [];___c0nt3xt.___st4ck3d = true;var ___st4ck = function(context, self) {var s;if (arguments.length == 1 && context) {s = !context.___st4ck3d ? [context] : context;} else if (!context.___st4ck3d) {s = [self, context];} else if (context && context === self && context.___st4ck3d) {s = context.slice(0);} else {s = context && context.___st4ck3d ? context.concat([self]) : ___st4ck(context).concat([self]);}return (s.___st4ck3d = true) && s;};___v1ew.push("<h2>");___v1ew.push(can.view.txt(1,\'h2\',0,this,function(){ return can.Mustache.txt({context:___st4ck(___c0nt3xt,this),options:options},null,can.Mustache.get("message",{context:___st4ck(___c0nt3xt,this),options:options},false,false));}));___v1ew.push("</h2>");; return ___v1ew.join(\'\')}} })' | ||
}, | ||
describe('CanJS view compiler tests', function() { | ||
var expectedEJS = "can.EJS(function(_CONTEXT,_VIEW) { " + | ||
"with(_VIEW) { with (_CONTEXT) {var ___v1ew = [];___v1ew.push(\"<h2>\");" + | ||
"___v1ew.push(can.view.txt(1,'h2',0,this,function(){ " + | ||
"return message }));___v1ew.push(\"</h2>\");; return ___v1ew.join('')}} })"; | ||
'1.1.2': { | ||
ejs: "can.EJS(function(_CONTEXT,_VIEW) { " + | ||
"with(_VIEW) { with (_CONTEXT) {var ___v1ew = [];___v1ew.push(\"<h2>\");" + | ||
"___v1ew.push(can.view.txt(1,'h2',0,this,function(){ " + | ||
"return message }));___v1ew.push(\"</h2>\");; return ___v1ew.join('')}} })", | ||
var expectedMustache = 'can.Mustache(function(_CONTEXT,_VIEW) { with(_VIEW) { with (_CONTEXT) {var ___v1ew = [];var ___c0nt3xt = this && this.___st4ck3d ? this : [];___c0nt3xt.___st4ck3d = true;var ___st4ck = function(context, self) {var s;if (arguments.length == 1 && context) {s = !context.___st4ck3d ? [context] : context;} else if (!context.___st4ck3d) {s = [self, context];} else if (context && context === self && context.___st4ck3d) {s = context.slice(0);} else {s = context && context.___st4ck3d ? context.concat([self]) : ___st4ck(context).concat([self]);}return (s.___st4ck3d = true) && s;};___v1ew.push("<h2>");___v1ew.push(can.view.txt(1,\'h2\',0,this,function(){ return can.Mustache.txt({context:___st4ck(___c0nt3xt,this),options:options},null,can.Mustache.get("message",{context:___st4ck(___c0nt3xt,this),options:options},false,false));}));___v1ew.push("</h2>");; return ___v1ew.join(\'\')}} })'; | ||
mustache: "can.Mustache(function(_CONTEXT,_VIEW) { " + | ||
"with(_VIEW) { with (_CONTEXT) {var ___v1ew = [];var ___c0nt3xt = []; ___c0nt3xt.___st4ck = true;" + | ||
"var ___st4ck = function(context, self) {var s;if (arguments.length == 1 && context) {" + | ||
"s = !context.___st4ck ? [context] : context;} else {" + | ||
"s = context && context.___st4ck ? context.concat([self]) : ___st4ck(context).concat([self]);}" + | ||
"return (s.___st4ck = true) && s;};___v1ew.push(\"<h2>\");" + | ||
"___v1ew.push(can.view.txt(1,'h2',0,this,function(){ " + | ||
"return can.Mustache.txt(___st4ck(___c0nt3xt,this),null," + | ||
"can.Mustache.get(\"message\",___st4ck(___c0nt3xt,this)));}));" + | ||
"___v1ew.push(\"</h2>\");; return ___v1ew.join('')}} })" | ||
} | ||
}; | ||
var normalizer = function(filename) { | ||
return path.relative(__dirname, filename); | ||
}; | ||
expected['latest'] = expected['1.1.5']; | ||
it('compiles EJS, normalizes view ids', function(done) { | ||
compiler.compile(__dirname + '/fixtures/view.ejs', normalizer, | ||
function(error, output, id) { | ||
expect(output).to.be(expectedEJS); | ||
expect(id).to.be('fixtures_view_ejs'); | ||
done(); | ||
}); | ||
}); | ||
var normalizer = function (filename) { | ||
return path.relative(__dirname, filename); | ||
}; | ||
it('compiles Mustache, normalizes view ids', function(done) { | ||
compiler.compile(__dirname + '/fixtures/view.mustache', normalizer, | ||
function(error, output, id) { | ||
expect(output).to.be(expectedMustache); | ||
expect(id).to.be('fixtures_view_mustache'); | ||
done(); | ||
}); | ||
}); | ||
for(var version in expected) { | ||
(function(version, expectedEJS, expectedMustache) { | ||
describe('CanJS view compiler tests, version ' + version, function () { | ||
it('compiles EJS, normalizes view ids', function (done) { | ||
compiler.compile({ | ||
filename: __dirname + '/fixtures/view.ejs', | ||
normalizer: normalizer, | ||
version: version | ||
}, function (error, output, id) { | ||
expect(output).to.be(expectedEJS); | ||
expect(id).to.be('fixtures_view_ejs'); | ||
done(); | ||
}); | ||
}); | ||
it('generates output text', function(done) { | ||
compiler([__dirname + '/fixtures/view.ejs', __dirname + '/fixtures/view.mustache'], {}, | ||
function(err, result) { | ||
var expected = '(function(window) {\n' + | ||
"can.view.preload('test_fixtures_view_ejs'," + expectedEJS + ");\n" + | ||
"can.view.preload('test_fixtures_view_mustache'," + expectedMustache + ");\n" + | ||
'})(this);'; | ||
it('compiles Mustache, normalizes view ids', function (done) { | ||
compiler.compile({ | ||
filename: __dirname + '/fixtures/view.mustache', | ||
normalizer: normalizer, | ||
version: version | ||
}, function (error, output, id) { | ||
expect(output).to.be(expectedMustache); | ||
expect(id).to.be('fixtures_view_mustache'); | ||
done(); | ||
}); | ||
}); | ||
expect(result).to.be(expected); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('generates output text', function (done) { | ||
compiler([__dirname + '/fixtures/view.ejs', __dirname + '/fixtures/view.mustache'], { | ||
version: version | ||
}, function (err, result) { | ||
var expected = '(function(window) {\n' + | ||
"can.view.preload('test_fixtures_view_ejs'," + expectedEJS + ");\n" + | ||
"can.view.preload('test_fixtures_view_mustache'," + expectedMustache + ");\n" + | ||
'})(this);'; | ||
expect(result).to.be(expected); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
})(version, expected[version].ejs, expected[version].mustache); | ||
} | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
147
1074048
5
245
20329