Skip to content

Add the possibility to pass a name to the module when AMD option is true #160

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ options: {
var names = filename.replace(/modules\/(.*)(\/\w+\.hbs)/, '$1');
return names.split('/').join('.');
},
},
files: {
'ns_nested_tmpls.js' : [ 'modules/**/*.hbs']
files: {
'ns_nested_tmpls.js' : [ 'modules/**/*.hbs']
}
}
```

Expand Down Expand Up @@ -100,6 +100,11 @@ define(['handlebars'], function(Handlebars) {
return this['[template namespace]'];
});
```
#### amdDefinitionName
Type: `String`
Default: `false`

This option gives a name to the AMD module. It is necessary to have amd true.

#### commonjs
Type: `Boolean`
Expand Down
5 changes: 5 additions & 0 deletions docs/handlebars-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ define(['handlebars'], function(Handlebars) {
return this['[template namespace]'];
});
```
## amdDefinitionName
Type: `String`
Default: `false`

This option gives a name to the AMD module. It is necessary to have amd true.

## commonjs
Type: `Boolean`
Expand Down
23 changes: 19 additions & 4 deletions tasks/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ module.exports = function(grunt) {
separator: grunt.util.linefeed + grunt.util.linefeed,
wrapped: true,
amd: false,
amdDefinitionName: false,
commonjs: false,
knownHelpers: [],
knownHelpersOnly: false
Expand Down Expand Up @@ -185,14 +186,28 @@ module.exports = function(grunt) {

}

// Start to wrap file with AMD
if (options.amd) {

// add AMD name string from options.amdDefinitionName
var amdNameString = '';

// Verify if it was set a name to define method and its a string
if (options.amdDefinitionName && typeof options.amdDefinitionName === 'string') {
amdNameString = '\'' + options.amdDefinitionName + '\',';
} else {
amdNameString = '';
}

// Wrap the file in an AMD define fn.
if (typeof options.amd === 'boolean') {
output.unshift('define([\'handlebars\'], function(Handlebars) {');
output.unshift('define(' + amdNameString + '[\'handlebars\'], function(Handlebars) {');
} else if (typeof options.amd === 'string') {
output.unshift('define([\'' + options.amd + '\'], function(Handlebars) {');
output.unshift('define(' + amdNameString + '[\'' + options.amd + '\'], function(Handlebars) {');
} else if (typeof options.amd === 'function') {
output.unshift('define([\'' + options.amd(filename, ast, compiled) + '\'], function(Handlebars) {');
//jscs:disable maximumLineLength
output.unshift('define(' + amdNameString + '[\'' + options.amd(filename, ast, compiled) + '\'], function(Handlebars) {');
//jscs:enable maximumLineLength
} else if (Array.isArray(options.amd)) {
// convert options.amd to a string of dependencies for require([...])
var amdString = '';
Expand All @@ -205,7 +220,7 @@ module.exports = function(grunt) {
}

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

if (useNamespace) {
Expand Down