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

Add commonJS support #43

Open
wants to merge 6 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
35 changes: 35 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ module.exports = function(grunt) {
"tmp/pretty_amd.js": ["test/fixtures/template.html"]
}
},
pretty_commonjs: {
options: {
templateSettings: {
variable: 'obj'
},
prettify: true,
commonjs: true
},
files: {
"tmp/pretty_commonjs.js": ["test/fixtures/template.html"]
}
},
prettify: {
options: {
templateSettings: {
Expand Down Expand Up @@ -86,6 +98,29 @@ module.exports = function(grunt) {
"tmp/amd_wrapper_no_ns.js": ["test/fixtures/template.html"]
}
},
commonjs_wrapper: {
options: {
templateSettings: {
variable: 'obj'
},
commonjs:true
},
files: {
"tmp/commonjs_wrapper.js": ["test/fixtures/template.html"]
}
},
commonjs_wrapper_no_ns: {
options: {
templateSettings: {
variable: 'obj'
},
commonjs:true,
namespace:false
},
files: {
"tmp/commonjs_wrapper_no_ns.js": ["test/fixtures/template.html"]
}
},
uglyfile: {
options: {
templateSettings: {
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ options: {
}
```

#### commonjs
Type: `boolean`
Default: false

Exports the compiled templates using the CommonJS pattern for Node/Browserify and returns the compiled template namespace unless namespace has been explicitly set to false in which case the template function will be returned directly.

```js
//...//
module.exports = this['[template namespace]'];
```

Example:
```js
options: {
commonjs: true
}
```

#### processContent
Type: `function`

Expand Down
18 changes: 18 additions & 0 deletions docs/jst-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ options: {
}
```

## commonjs
Type: `boolean`
Default: false

Exports the template using CommonJS pattern, for Node and Browserify use

```js
//...//
module.exports = this['[template namespace]'];
```

Example:
```js
options: {
commonjs: true
}
```

## processContent
Type: `function`

Expand Down
15 changes: 15 additions & 0 deletions tasks/jst.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ module.exports = function(grunt) {
if (options.amd && options.namespace === false) {
return 'return ' + compiled;
}
else if (options.commonjs && options.namespace === false){
return 'module.exports = ' + compiled;
}
return nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+';';
});

Expand All @@ -84,6 +87,18 @@ module.exports = function(grunt) {
}
output.push("});");
}
else if (options.commonjs) {
if (options.prettify) {
output.forEach(function(line, index) {
output[index] = line;
});
}
if (options.namespace !== false) {
// Namespace has not been explicitly set to false; the commonjs
// wrapper will return the object containing the template.
output.push("module.exports = " + nsInfo.namespace + ";");
}
}
grunt.file.write(f.dest, output.join(grunt.util.normalizelf(options.separator)));
grunt.log.writeln('File ' + chalk.cyan(f.dest) + ' created.');
}
Expand Down
11 changes: 11 additions & 0 deletions test/expected/commonjs_wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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
};

module.exports = this["JST"];
7 changes: 7 additions & 0 deletions test/expected/commonjs_wrapper_no_ns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function(obj) {
var __t, __p = '', __e = _.escape;
__p += '<head><title>' +
((__t = ( obj.title )) == null ? '' : __t) +
'</title></head>';
return __p
}
5 changes: 5 additions & 0 deletions test/expected/pretty_commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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};

module.exports = this["JST"];
14 changes: 13 additions & 1 deletion test/jst_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports['jst'] = {

var expect, result;

test.expect(10);
test.expect(13);

expect = grunt.file.read("test/expected/jst.js");
result = grunt.file.read("tmp/jst.js");
Expand Down Expand Up @@ -48,6 +48,18 @@ exports['jst'] = {
result = grunt.file.read("tmp/local_scope.js");
test.equal(expect, result, "should add `with` block when templateSettings.variable is undefined");

expect = grunt.file.read("test/expected/commonjs_wrapper.js");
result = grunt.file.read("tmp/commonjs_wrapper.js");
test.equal(expect, result, "should export the templates using the CommonJS pattern");

expect = grunt.file.read("test/expected/commonjs_wrapper_no_ns.js");
result = grunt.file.read("tmp/commonjs_wrapper_no_ns.js");
test.equal(expect, result, "should export the templates using the CommonJS pattern and return the function itself with no namespace");

expect = grunt.file.read("test/expected/pretty_commonjs.js");
result = grunt.file.read("tmp/pretty_commonjs.js");
test.equal(expect, result, "should make the CommonJS wrapper output pretty");

test.done();
}
};