Skip to content

Commit

Permalink
Merge pull request #134 from kitcheck/expand-cwd
Browse files Browse the repository at this point in the history
Fixes `cwd` being part of the $templateCache string when `expand:true`
  • Loading branch information
underscorebrody committed Nov 26, 2015
2 parents e1e1293 + 2444359 commit 7b883a3
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ module.exports = function(grunt) {
dest: 'tmp/relative_url.js'
},

// URLs should match path, sans the `cwd`
relative_url_expand: {
expand: true,
cwd: 'test/fixtures',
src: ['three/**/*.html'],
dest: 'tmp',
ext: '.js'
},

// Customize URLs to not have an extension
custom_url: {
src: ['test/fixtures/one.html', 'test/fixtures/two/**/*.html'],
Expand Down
5 changes: 4 additions & 1 deletion tasks/angular-templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ module.exports = function(grunt) {
grunt.log.warn('No templates found');
}

var compiler = new Compiler(grunt, options, file.cwd);
var expanded = file.orig.expand;
var cwd = file.orig.expand ? file.orig.cwd : file.cwd;

var compiler = new Compiler(grunt, options, cwd, expanded);
var appender = new Appender(grunt);
var modules = compiler.modules(file.src);
var compiled = [];
Expand Down
8 changes: 6 additions & 2 deletions tasks/lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var Url = require('url');
* @param {String} cwd Determines if paths are relative or not
* @return {Object}
*/
var Compiler = function(grunt, options, cwd) {
var Compiler = function(grunt, options, cwd, expanded) {

/**
* Wrap individual cache registration script in bootstrap function
Expand All @@ -44,6 +44,10 @@ var Compiler = function(grunt, options, cwd) {
path = path.replace(/\/?$/, '/');
}

if(cwd && expanded){

This comment has been minimized.

Copy link
@sarunas

sarunas Nov 26, 2015

Its a bug, causes all cwd instances to be replaced in path.

This comment has been minimized.

Copy link
@underscorebrody

underscorebrody Nov 26, 2015

Author Collaborator

It should only cause path to be replaced when 'expand: true' is set. Previous behavior was handling those incorrectly and preventing modules from matching. Are you seeing it impact other scenarios?

This comment has been minimized.

Copy link
@sarunas

sarunas Nov 27, 2015

Well its not replaces a path, but all cwd occurrences in path:
cwd: 'app'
path: 'app/views/application.html'
result: 'views/lication.html'

It should replace only start of the path.

This comment has been minimized.

Copy link
@underscorebrody

underscorebrody Nov 27, 2015

Author Collaborator

Oooh okay I see what you're saying now, i'll fix this

This comment has been minimized.

Copy link
@underscorebrody

underscorebrody Nov 27, 2015

Author Collaborator

@sarunas fixed and published as 0.5.9. Sorry about that!

This comment has been minimized.

Copy link
@sarunas

sarunas via email Nov 27, 2015

url = url.replace(cwd, '').replace(/^\//,'');
}

// Append formatted URL
path += Url.format( Url.parse( url.replace(/\\/g, '/') ) );

Expand Down Expand Up @@ -166,7 +170,7 @@ var Compiler = function(grunt, options, cwd) {
* @return {String} Template path
*/
this.path = function(file) {
if (cwd) {
if (cwd && !expanded) {
return cwd + '/' + file;
}

Expand Down
16 changes: 16 additions & 0 deletions test/angular-templates_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,22 @@ exports.ngtemplates = {
test.done();
},

relative_url_expand: function(test) {
test.expect(2);

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

test.equal(expected, actual);

actual = grunt.file.read('tmp/three/three_two.js');
expected = grunt.file.read('test/expected/relative_url_expand_three_two.js');

test.equal(expected, actual);

test.done();
},

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

Expand Down
12 changes: 12 additions & 0 deletions test/expected/relative_url_expand_three.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
angular.module('relative_url_expand').run(['$templateCache', function($templateCache) {
'use strict';

$templateCache.put('three/three.html',
"<h2>Three</h2>\n" +
"\n" +
"<!-- Comment for three -->\n" +
"\n" +
"<textarea readonly=\"readonly\">We are three.</textarea>\n"
);

}]);
12 changes: 12 additions & 0 deletions test/expected/relative_url_expand_three_two.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
angular.module('relative_url_expand').run(['$templateCache', function($templateCache) {
'use strict';

$templateCache.put('three/three_two.html',
"<h2>Three Two</h2>\n" +
"\n" +
"<!-- Comment for three two -->\n" +
"\n" +
"<textarea readonly=\"readonly\">We are three two.</textarea>\n"
);

}]);
5 changes: 5 additions & 0 deletions test/fixtures/three/three.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h2>Three</h2>

<!-- Comment for three -->

<textarea readonly="readonly">We are three.</textarea>
5 changes: 5 additions & 0 deletions test/fixtures/three/three_two.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h2>Three Two</h2>

<!-- Comment for three two -->

<textarea readonly="readonly">We are three two.</textarea>

0 comments on commit 7b883a3

Please sign in to comment.