Skip to content

Commit

Permalink
Added options.amd as a function
Browse files Browse the repository at this point in the history
  • Loading branch information
alexindigo committed Apr 21, 2015
1 parent 58a5b5e commit 06d17f3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ module.exports = function(grunt) {
'tmp/amd_namespace_as_function.js' : ['test/fixtures/modules/**/*.hbs']
}
},
amd_compile_function: {
options: {
amd: function() { return ['handlebars', 'custom dependency'].join("', '"); },
namespace: false
},
files: {
'tmp/amd_compile_function.js': ['test/fixtures/amd.html']
}
},
commonjs_compile: {
options: {
commonjs: true
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Enable the compiled file to be required on node.js by preppending and appending
For this option to work you need to define the `namespace` option.

#### amd
Type: `Boolean` or `String` or `Array`
Type: `Boolean` or `String` or `Array` or `Function`
Default: `false`

Wraps the output file with an AMD define function and returns the compiled template namespace unless namespace has been explicitly set to false in which case the template function will be returned directly.
Expand All @@ -93,6 +93,8 @@ If `String` then that string will be used in the module definition `define(['you

If `Array` then those strings will be used in the module definition. `'handlebars'` should always be the first item in the array, eg: `amd: ['handlebars', 'handlebars.helpers']`

If `Function` then it will be called per each module and returned string will be used in the module defintion `"define(['" + options.amd(filename, ast, compiled) + "']"`

```js
define(['handlebars'], function(Handlebars) {
//...//
Expand Down
4 changes: 3 additions & 1 deletion docs/handlebars-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Enable the compiled file to be required on node.js by preppending and appending
For this option to work you need to define the `namespace` option.

## amd
Type: `Boolean` or `String` or `Array`
Type: `Boolean` or `String` or `Array` or `Function`
Default: `false`

Wraps the output file with an AMD define function and returns the compiled template namespace unless namespace has been explicitly set to false in which case the template function will be returned directly.
Expand All @@ -64,6 +64,8 @@ If `String` then that string will be used in the module definition `define(['you

If `Array` then those strings will be used in the module definition. `'handlebars'` should always be the first item in the array, eg: `amd: ['handlebars', 'handlebars.helpers']`

If `Function` then it will be called per each module and returned string will be used in the module defintion `"define(['" + options.amd(filename, ast, compiled) + "']"`

```js
define(['handlebars'], function(Handlebars) {
//...//
Expand Down
5 changes: 4 additions & 1 deletion tasks/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ module.exports = function(grunt) {
var declarations = [];
var partials = [];
var templates = [];
// template identifying parts
var ast, compiled, filename;

// Namespace info for current template
var nsInfo;
Expand Down Expand Up @@ -121,7 +123,6 @@ module.exports = function(grunt) {
var src = processContent(grunt.file.read(filepath), filepath);

var Handlebars = require('handlebars');
var ast, compiled, filename;
try {
// parse the handlebars template into it's AST
ast = processAST(Handlebars.parse(src));
Expand Down Expand Up @@ -191,6 +192,8 @@ module.exports = function(grunt) {
output.unshift('define([\'handlebars\'], function(Handlebars) {');
} else if (typeof options.amd === 'string') {
output.unshift('define([\'' + options.amd + '\'], function(Handlebars) {');
} else if (typeof options.amd === 'function') {
output.unshift("define(['" + options.amd(filename, ast, compiled) + "'], function(Handlebars) {");
} else if (Array.isArray(options.amd)) {
// convert options.amd to a string of dependencies for require([...])
var amdString = '';
Expand Down
7 changes: 7 additions & 0 deletions test/expected/amd_compile_function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
define(['handlebars', 'custom dependency'], function(Handlebars) {

return Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
return "<section class=\"main-app\">\n <h1>Some title</h1>\n <p>I've been compiled with amd support</p>\n</section>";
},"useData":true})

});
8 changes: 8 additions & 0 deletions test/handlebars_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ exports.handlebars = {
test.done();
});
},
amd_compile_function: function(test) {
test.expect(1);

filesAreEqual('amd_compile_function.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap everything with an AMD define block and have a custom module name.');
test.done();
});
},
commonjs_compile: function(test) {
test.expect(1);

Expand Down

0 comments on commit 06d17f3

Please sign in to comment.