Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added options.amd as a function #119

Merged
merged 1 commit into from Apr 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions Gruntfile.js
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
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
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
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
@@ -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
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