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

extend the amd option #59

Open
alexus85 opened this issue Jan 26, 2015 · 1 comment
Open

extend the amd option #59

alexus85 opened this issue Jan 26, 2015 · 1 comment

Comments

@alexus85
Copy link

It would be great if the option.amd was working similarly as in the grunt-contrib-handlebars (since it's very similar) This would enable users to define the library name right in the template init function..

gruntfile.json

amd:  ['my_underscore'],

jst.js

           if (typeof options.amd === 'boolean') {
                output.unshift("define(function(){");
           } else if (typeof options.amd === 'string') {
                output.unshift('define([\'' + options.amd + '\'], function(_) {');
           } else if (Array.isArray(options.amd)) {
            // convert options.amd to a string of dependencies for require([...])
            var amdString = '';
            for (var i = 0; i < options.amd.length; i++) {
                if (i !== 0) {
                    amdString += ', ';
                }

                amdString += '\'' + options.amd[i] + '\'';
            }
            output.unshift('define([' + amdString + '], function(_) {');
           }

result:

define(['my_underscore'], function(_) {

this["test"] = this["test"] || {};

this["test"]["test1"] = function(obj) {
var __t, __p = '', __e = _.escape;
__p += '<head><title>' +
((__t = ( obj.title )) == null ? '' : __t) +
'</title></head>';
return __p
};

  return this["test"];

});
@jorrit
Copy link

jorrit commented Jul 6, 2015

I think the _ part should be configurable too.

I propose a solution like this:

config

amd: {
  'underscore': '_'
}

code (untested)

var amdRequirements = [];
var amdImports = [];
_.each(options.amd, function(v, k) {
  amdRequirements.push(k);
  amdImports.push(v);
});

output.unshift('define(' + JSON.stringify(amdRequirements) + ', function(' + amdImports.join() + ') {')

result

define(['underscore'], function(_) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants