Skip to content

Commit d9e2aa9

Browse files
committed
Add the possibility to pass a name to the module when AMD option is true
1 parent 8dc25fa commit d9e2aa9

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ define(['handlebars'], function(Handlebars) {
102102
});
103103
```
104104

105+
#### amdDefinitionName
106+
Type: `String`
107+
Default: `false`
108+
109+
This option gives a name to the AMD module. It is necessary to have amd true.
110+
105111
#### commonjs
106112
Type: `Boolean`
107113
Default: `false`

tasks/handlebars.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module.exports = function(grunt) {
6060
separator: grunt.util.linefeed + grunt.util.linefeed,
6161
wrapped: true,
6262
amd: false,
63+
amdDefinitionName: false,
6364
commonjs: false,
6465
knownHelpers: [],
6566
knownHelpersOnly: false
@@ -185,14 +186,25 @@ module.exports = function(grunt) {
185186

186187
}
187188

188-
if (options.amd) {
189+
// Start to wrap file with AMD
190+
if (options.amd) {
191+
// addd AMD name string from options.amdDefinitionName
192+
var amdNameString = "";
193+
194+
// Verify if it was set a name to define method and its a string
195+
if(options.amdDefinitionName && typeof options.amdDefinitionName === 'string'){
196+
amdNameString = '\''+options.amdDefinitionName+'\',';
197+
}else{
198+
amdNameString = '';
199+
}
200+
189201
// Wrap the file in an AMD define fn.
190202
if (typeof options.amd === 'boolean') {
191-
output.unshift('define([\'handlebars\'], function(Handlebars) {');
203+
output.unshift('define('+amdNameString+'[\'handlebars\'], function(Handlebars) {');
192204
} else if (typeof options.amd === 'string') {
193-
output.unshift('define([\'' + options.amd + '\'], function(Handlebars) {');
205+
output.unshift('define('+amdNameString+'[\'' + options.amd + '\'], function(Handlebars) {');
194206
} else if (typeof options.amd === 'function') {
195-
output.unshift('define([\'' + options.amd(filename, ast, compiled) + '\'], function(Handlebars) {');
207+
output.unshift('define('+amdNameString+'[\'' + options.amd(filename, ast, compiled) + '\'], function(Handlebars) {');
196208
} else if (Array.isArray(options.amd)) {
197209
// convert options.amd to a string of dependencies for require([...])
198210
var amdString = '';
@@ -205,7 +217,7 @@ module.exports = function(grunt) {
205217
}
206218

207219
// Wrap the file in an AMD define fn.
208-
output.unshift('define([' + amdString + '], function(Handlebars) {');
220+
output.unshift('define('+amdNameString+'[' + amdString + '], function(Handlebars) {');
209221
}
210222

211223
if (useNamespace) {
@@ -215,6 +227,7 @@ module.exports = function(grunt) {
215227
}
216228
output.push('});');
217229
}
230+
// End of AMD
218231

219232
if (options.commonjs) {
220233
if (useNamespace) {

0 commit comments

Comments
 (0)