Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Configure path to underscore in AMD wrapper (option 1 of 2) #20

Closed
wants to merge 3 commits into from

4 participants

@colinhicks

Addresses #18. See discussion there for context.

Since compiled templates include a reference to _, that dependency needs to be explicitly defined when the AMD wrapper is used.

With this pull, given amdWrapper: true, the first line of jst output is:

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

The default path to the underscore module can be overriden in the Gruntfile (see option 2 for alternate syntax):

// Gruntfile.js
jst: {
    compile {
        options: {
          amdWrapper: true,
          amdPathToUnderscore: 'path/to/lodash'
        },
        files: {
          "build/templates.js": ["templates/**/*.html"]
        }
    }
}

// build/templates.js
define(['path/to/lodash'], function(_) {

Note: You may also choose to specify this path in your RequireJS config.

@tkellen
Owner

Hey Colin, thanks for taking the time to put this PR (and the other) together. I'd like to merge this, but I think having the dependency name be 'underscore' is sufficient, given that you can override this in your RequireJS config.

@tbranyen / @lazd would you agree?

If not, I'll merge with the option to name the underscore module, if so, I'll merge after that has been removed.

@tkellen
Owner
@vladikoff
Owner

hey @tkellen this is one of the old PRs in the gruntjs org, what should we do about it?

@tkellen
Owner

Defining underscore as a dependency on the AMD wrapper is fine. I don't think we need to support amdPathToUnderscore, though. You can configure the path to lodash/underscore in your RequireJS config.

@colinhicks would you be willing to update this PR?

@sindresorhus
Collaborator

ping, also need to fix merge conflict.

@vladikoff vladikoff closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
12 Gruntfile.js
@@ -74,6 +74,18 @@ module.exports = function(grunt) {
"tmp/amd_wrapper.js": ["test/fixtures/template.html"]
}
},
+ amd_custom_path: {
+ options: {
+ templateSettings: {
+ variable: 'obj'
+ },
+ amdWrapper:true,
+ amdPathToUnderscore: '../mock/path/to/lodash'
+ },
+ files: {
+ "tmp/amd_custom_path.js": ["test/fixtures/template.html"]
+ }
+ },
uglyfile: {
options: {
templateSettings: {
View
2  package.json
@@ -28,7 +28,7 @@
"test": "grunt test"
},
"dependencies": {
- "lodash": "~1.0.0",
+ "lodash": "~1.0.1",
"grunt-lib-contrib": "~0.5.1"
},
"devDependencies": {
View
5 tasks/jst.js
@@ -22,7 +22,8 @@ module.exports = function(grunt) {
namespace: 'JST',
templateSettings: {},
processContent: function (src) { return src; },
- separator: lf + lf
+ separator: lf + lf,
+ amdPathToUnderscore: 'underscore'
});
// assign filename transformation functions
@@ -71,7 +72,7 @@ module.exports = function(grunt) {
output[index] = " " + line;
});
}
- output.unshift("define(function(){");
+ output.unshift("define(['" + options.amdPathToUnderscore + "'], function(_) {");
output.push(" return " + nsInfo.namespace + ";" + lf + "});");
}
grunt.file.write(f.dest, output.join(grunt.util.normalizelf(options.separator)));
View
14 test/expected/amd_custom_path.js
@@ -0,0 +1,14 @@
+define(['../mock/path/to/lodash'], function(_) {
+
+this["JST"] = this["JST"] || {};
+
+this["JST"]["test/fixtures/template.html"] = function(obj) {
+var __t, __p = '', __e = _.escape;
+__p += '<head><title>' +
+((__t = ( obj.title )) == null ? '' : __t) +
+'</title></head>';
+return __p
+};
+
+ return this["JST"];
+});
View
2  test/expected/amd_wrapper.js
@@ -1,4 +1,4 @@
-define(function(){
+define(['underscore'], function(_) {
this["JST"] = this["JST"] || {};
View
2  test/expected/pretty_amd.js
@@ -1,4 +1,4 @@
-define(function(){
+define(['underscore'], function(_) {
this["JST"] = this["JST"] || {};
View
6 test/jst_test.js
@@ -6,7 +6,7 @@ exports['jst'] = {
var expect, result;
- test.expect(9);
+ test.expect(10);
expect = grunt.file.read("test/expected/jst.js");
result = grunt.file.read("tmp/jst.js");
@@ -35,6 +35,10 @@ exports['jst'] = {
expect = grunt.file.read("test/expected/pretty_amd.js");
result = grunt.file.read("tmp/pretty_amd.js");
test.equal(expect, result, "should make the AMD wrapper output pretty");
+
+ expect = grunt.file.read("test/expected/amd_custom_path.js");
+ result = grunt.file.read("tmp/amd_custom_path.js");
+ test.equal(expect, result, "should use custom path to underscore AMD dependency when defined");
expect = grunt.file.read("test/expected/process_content.js");
result = grunt.file.read("tmp/process_content.js");
Something went wrong with that request. Please try again.