Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow generated JS to define new angular module
  • Loading branch information
sidwood committed Jun 7, 2013
1 parent 37f5b47 commit ce54c0c
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 16 deletions.
39 changes: 35 additions & 4 deletions Gruntfile.js
Expand Up @@ -70,13 +70,44 @@ module.exports = function(grunt) {
src: ['test/fixtures/simple.html'],
dest: 'tmp/simple_prepend.js'
},
target: {
moduleString: {
options: {
base: 'test/fixtures',
module: 'ImAModuleNotATarget'
module: 'ImNotATarget'
},
src: ['test/fixtures/simple.html'],
dest: 'tmp/options_module.js'
dest: 'tmp/module_option_string.js'
},
moduleObject: {
options: {
base: 'test/fixtures',
module: {
name: 'ImNotATarget',
define: true
}
},
src: ['test/fixtures/simple.html'],
dest: 'tmp/module_option_object.js'
},
moduleObjectName: {
options: {
base: 'test/fixtures',
module: {
name: 'ImNotATarget'
}
},
src: ['test/fixtures/simple.html'],
dest: 'tmp/module_option_object_name.js'
},
moduleObjectDefine: {
options: {
base: 'test/fixtures',
module: {
define: true
}
},
src: ['test/fixtures/simple.html'],
dest: 'tmp/module_option_object_define.js'
},
concatSimple: {
options: {
Expand All @@ -98,7 +129,7 @@ module.exports = function(grunt) {
noConflict: 'notGlobalAngular'
},
src: 'test/fixtures/simple.html',
dest: 'tmp/options_noConflict_fixture.js'
dest: 'tmp/noConflict_option_fixture.js'
},
}
});
Expand Down
29 changes: 28 additions & 1 deletion README.md
Expand Up @@ -37,7 +37,7 @@ grunt.initConfig({
base: 'src/views', // $templateCache ID will be relative to this folder
prepend: '/static/assets/', // (Optional) Prepend path to $templateCache ID
module: 'App' // (Optional) The module the templates will be added to
// Defaults to target name (e.g. `build`)
// Defaults to target name (e.g. `myapp`)
concat: 'dist/js/app.js' // (Optional) Append to existing `concat` target
noConflict: 'otherAngular' // (Optional) Name of angular.noConflict() app uses
},
Expand Down Expand Up @@ -133,6 +133,33 @@ ngtemplates: {
This will append the output file `dist/js/templates.js` to
`usemin`'s dynamic `concat` task: `dist/js/app.js`.

### Defining an Angular Module

It's possible to define a new angular module in the generated JS file.

```js
ngtemplates: {
myapp: {
options: {
module: {
name: 'templates',
define: true
}
},
src: 'src/views/**.html',
dest: 'dist/templates.js'
}
}
```

This will generate the following at `dist/templates.js`:

```js
angular.module('templates', []).run(['$templateCache', function($templateCache) {
...
}]);
```

## Changelog

### v0.3.8
Expand Down
17 changes: 15 additions & 2 deletions tasks/angular-templates.js
Expand Up @@ -16,14 +16,27 @@ module.exports = function(grunt) {
var compiler = require('./lib/compiler').init(grunt);

grunt.registerMultiTask('ngtemplates', 'Compile AngularJS templates', function() {
var id = this.options().module || this.target;
var id = this.target;
var noConflict = this.options().noConflict || 'angular';
var files = grunt.file.expand(this.files[0].src);
var dest = path.normalize(this.files[0].dest);
var done = this.async();
var options = this.options();
var define = false;

compiler.compile(id, noConflict, options, files, function(err, compiled) {
if (options.module) {
if (typeof options.module === 'string') {
id = options.module;
} else if (options.module.hasOwnProperty('name') && typeof options.module.name === 'string') {
id = options.module.name;
}

if (options.module.hasOwnProperty('define') && options.module.define === true ) {
define = true;
}
}

compiler.compile(id, noConflict, define, options, files, function(err, compiled) {
if (err) {
done(false);
} else {
Expand Down
10 changes: 8 additions & 2 deletions tasks/lib/compiler.js
Expand Up @@ -23,8 +23,14 @@ module.exports.init = function(grunt) {
}, callback);
};

var compile = function(id, noConflict, options, files, callback) {
var template = '<%= noConflict %>.module("<%= id %>").run(["$templateCache", function($templateCache) {\n<%= content %>\n}]);\n';
var compile = function(id, noConflict, define, options, files, callback) {
var template = '<%= noConflict %>.module("<%= id %>"';

if (define) {
template += ', []';
}

template += ').run(["$templateCache", function($templateCache) {\n<%= content %>\n}]);\n';

concat(options, files, function(err, concated) {
var compiled = process(template, id, concated.join(''), noConflict);
Expand Down
42 changes: 36 additions & 6 deletions test/angular-templates_test.js
Expand Up @@ -35,21 +35,51 @@ exports.ngtemplates = {
test.done();
},

module: function(test) {
moduleString: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/options_module.js');
var expected = grunt.file.read('test/expected/options_module.js');
var actual = grunt.file.read('tmp/module_option_string.js');
var expected = grunt.file.read('test/expected/module_option_string.js');

test.equal(expected, actual, 'should set the angular module to the provided options value');
test.equal(expected, actual, 'set angular module name to the one provided');
test.done();
},

moduleObject: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/module_option_object.js');
var expected = grunt.file.read('test/expected/module_option_object.js');

test.equal(expected, actual, ' define new angular module with provided name');
test.done();
},

moduleObjectName: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/module_option_object_name.js');
var expected = grunt.file.read('test/expected/module_option_string.js');

test.equal(expected, actual, ' set angular module name to the one provided');
test.done();
},

moduleObjectDefine: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/module_option_object_define.js');
var expected = grunt.file.read('test/expected/module_option_object_define.js');

test.equal(expected, actual, ' define new angular module with grunt target name');
test.done();
},

noConflict: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/options_noConflict_fixture.js');
var expected = grunt.file.read('test/expected/options_noConflict.js');
var actual = grunt.file.read('tmp/noConflict_option_fixture.js');
var expected = grunt.file.read('test/expected/noConflict_option.js');

test.equal(expected, actual, 'should reference angular by the noConflict options value');
test.done();
Expand Down
@@ -1,4 +1,4 @@
angular.module("ImAModuleNotATarget").run(["$templateCache", function($templateCache) {
angular.module("ImNotATarget", []).run(["$templateCache", function($templateCache) {

$templateCache.put("simple.html",
"Howdy there! \\ Your name is \"{{ name }}\".\n"
Expand Down
7 changes: 7 additions & 0 deletions test/expected/module_option_object_define.js
@@ -0,0 +1,7 @@
angular.module("moduleObjectDefine", []).run(["$templateCache", function($templateCache) {

$templateCache.put("simple.html",
"Howdy there! \\ Your name is \"{{ name }}\".\n"
);

}]);
7 changes: 7 additions & 0 deletions test/expected/module_option_string.js
@@ -0,0 +1,7 @@
angular.module("ImNotATarget").run(["$templateCache", function($templateCache) {

$templateCache.put("simple.html",
"Howdy there! \\ Your name is \"{{ name }}\".\n"
);

}]);
File renamed without changes.

0 comments on commit ce54c0c

Please sign in to comment.