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

added templateWrap callback option and strictMode boolean option #119

Open
wants to merge 5 commits into
base: master
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# grunt-angular-templates

## This is a fork of the original [grunt-angular-templates](https://github.com/ericclemmons/grunt-angular-templates), to support the features added (but not yet merged) in this pull request: [added templateWrap callback option and strictMode boolean option](https://github.com/ericclemmons/grunt-angular-templates/pull/119)

### All credits to the original project collaborators.

[![Build Status](https://travis-ci.org/ericclemmons/grunt-angular-templates.svg)](https://travis-ci.org/ericclemmons/grunt-angular-templates)
[![Dependencies](https://david-dm.org/ericclemmons/grunt-angular-templates.svg)](https://david-dm.org/ericclemmons/grunt-angular-templates)
[![devDependencies](https://david-dm.org/ericclemmons/grunt-angular-templates/dev-status.svg)](https://david-dm.org/ericclemmons/grunt-angular-templates#info=devDependencies&view=table)
Expand Down Expand Up @@ -80,6 +84,24 @@ bootstrap: function(module, script) {
}
```

### templateWrap

> Callback to modify the way each template is registered

By default, the templateWrap wraps each template with

```js
$templateCache.put(path, template);
```

If you want to create your own wrapper so you can use a different caching service, or wrap them in a different manner:

```js
templateWrap: function(path, script, index, files) {
return "$localStorage.put('" + path + "', " + template + ");"
}
```

### concat

> Name of `concat` target to append the compiled template path to.
Expand Down Expand Up @@ -174,6 +196,10 @@ ensures that URLs load via both AJAX and `$templateCache`.
This should be the output path of the compiled JS indicated in your HTML,
such as `path/to/output.js` shown here.

### strictMode

> Boolean indicated if 'use strict' should be attached to the top of the generated templates file.

## Usage


Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
{
"name": "grunt-angular-templates",
"name": "grunt-angular-templates-taylorcode",
"description": "Grunt build task to concatenate & register your AngularJS templates in the $templateCache",
"version": "0.5.7",
"homepage": "https://github.com/ericclemmons/grunt-angular-templates",
"version": "0.5.7-m",
"homepage": "https://github.com/taylorcode/grunt-angular-templates",
"author": {
"name": "Eric Clemmons",
"email": "eric@smarterspam.com"
"name": "Taylor McIntyre",
"email": "taylorsmcintyre@gmail.com"
},
"repository": {
"type": "git",
"url": "git://github.com/ericclemmons/grunt-angular-templates.git"
"url": "git://github.com/taylorcode/grunt-angular-templates.git"
},
"bugs": {
"url": "https://github.com/ericclemmons/grunt-angular-templates/issues"
"url": "https://github.com/taylorcode/grunt-angular-templates/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/ericclemmons/grunt-angular-templates/blob/master/LICENSE-MIT"
"url": "https://github.com/taylorcode/grunt-angular-templates/blob/master/LICENSE-MIT"
}
],
"main": "Gruntfile.js",
Expand Down
6 changes: 6 additions & 0 deletions tasks/angular-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ module.exports = function(grunt) {
return options.angular+".module('"+module+"'"+(options.standalone ? ', []' : '')+").run(['$templateCache', function($templateCache) {\n"+script+"\n}]);\n";
};

var templateWrapper = function(path, template) {
return "\n $templateCache.put('" + path + "',\n " + template + "\n );\n";
};

var ngtemplatesTask = function() {
var options = this.options({
angular: 'angular',
Expand All @@ -28,6 +32,8 @@ module.exports = function(grunt) {
prefix: '',
source: function(source) { return source; },
standalone: false,
strictMode: true,
templateWrap: templateWrapper,
url: function(path) { return path; },
usemin: null,
append: false
Expand Down
17 changes: 11 additions & 6 deletions tasks/lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ var Compiler = function(grunt, options, cwd) {
};

/**
* Wrap HTML template in `$templateCache.put(...)`
* Wrap HTML template in what is returned from `options.templateWrap`
* @param {String} template Multiline HTML template string
* @param {String} url URL to act as template ID
* @return {String} Template wrapped in `$templateCache.put(...)`
* @param
* @return {String} Template wrapped using the `options.templateWrap` function
*/
this.cache = function(template, url, prefix) {
this.cache = function(template, url, prefix, index, files) {
var path = prefix;

// Force trailing slash
Expand All @@ -47,7 +48,7 @@ var Compiler = function(grunt, options, cwd) {
// Append formatted URL
path += Url.format( Url.parse( url.replace(/\\/g, '/') ) );

return "\n $templateCache.put('" + path + "',\n " + template + "\n );\n";
return options.templateWrap(path, template, index, files);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give a reason why the template wrapper function might need the index or the full list of files? It doesn't look like they're used in your examples.

};

/**
Expand All @@ -66,7 +67,11 @@ var Compiler = function(grunt, options, cwd) {
return true;
});

var script = " 'use strict';" + grunt.util.linefeed;
var script = grunt.util.linefeed;

if(options.strictMode) {
script = " 'use strict';" + script;
}

script += paths
.map(this.load)
Expand All @@ -76,7 +81,7 @@ var Compiler = function(grunt, options, cwd) {
}.bind(this))
.map(this.stringify)
.map(function(string, i) {
return this.cache(string, this.url(files[i]), options.prefix);
return this.cache(string, this.url(files[i]), options.prefix, i, files);
}.bind(this))
.map(grunt.util.normalizelf)
.join(grunt.util.linefeed)
Expand Down