From 06d17f3255e51bce2d61dfd2c73ea7fa6de0df93 Mon Sep 17 00:00:00 2001 From: Alex Indigo Date: Tue, 21 Apr 2015 04:04:39 -0700 Subject: [PATCH] Added options.amd as a function --- Gruntfile.js | 9 +++++++++ README.md | 4 +++- docs/handlebars-options.md | 4 +++- tasks/handlebars.js | 5 ++++- test/expected/amd_compile_function.js | 7 +++++++ test/handlebars_test.js | 8 ++++++++ 6 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 test/expected/amd_compile_function.js diff --git a/Gruntfile.js b/Gruntfile.js index 083c74c..a3375ef 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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 diff --git a/README.md b/README.md index 066b197..5ab4ad4 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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) { //...// diff --git a/docs/handlebars-options.md b/docs/handlebars-options.md index 16f1196..b592289 100644 --- a/docs/handlebars-options.md +++ b/docs/handlebars-options.md @@ -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. @@ -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) { //...// diff --git a/tasks/handlebars.js b/tasks/handlebars.js index 3bece58..8bccf6c 100644 --- a/tasks/handlebars.js +++ b/tasks/handlebars.js @@ -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; @@ -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)); @@ -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 = ''; diff --git a/test/expected/amd_compile_function.js b/test/expected/amd_compile_function.js new file mode 100644 index 0000000..fba7a70 --- /dev/null +++ b/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 "
\n

Some title

\n

I've been compiled with amd support

\n
"; +},"useData":true}) + +}); diff --git a/test/handlebars_test.js b/test/handlebars_test.js index 93ce1eb..d4e6051 100644 --- a/test/handlebars_test.js +++ b/test/handlebars_test.js @@ -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);