From 24443596056542e699fff5e460d96c5d51e4451f Mon Sep 17 00:00:00 2001 From: Eric Brody Date: Wed, 4 Nov 2015 11:31:38 -0500 Subject: [PATCH] Fixes `cwd` being part of the $templateCache string when `expand:true` --- Gruntfile.js | 9 +++++++++ tasks/angular-templates.js | 5 ++++- tasks/lib/compiler.js | 8 ++++++-- test/angular-templates_test.js | 16 ++++++++++++++++ test/expected/relative_url_expand_three.js | 12 ++++++++++++ test/expected/relative_url_expand_three_two.js | 12 ++++++++++++ test/fixtures/three/three.html | 5 +++++ test/fixtures/three/three_two.html | 5 +++++ 8 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 test/expected/relative_url_expand_three.js create mode 100644 test/expected/relative_url_expand_three_two.js create mode 100644 test/fixtures/three/three.html create mode 100644 test/fixtures/three/three_two.html diff --git a/Gruntfile.js b/Gruntfile.js index f56c287..4431b3a 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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'], diff --git a/tasks/angular-templates.js b/tasks/angular-templates.js index 3f7904f..c85589c 100644 --- a/tasks/angular-templates.js +++ b/tasks/angular-templates.js @@ -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 = []; diff --git a/tasks/lib/compiler.js b/tasks/lib/compiler.js index 90a5d1e..b559719 100644 --- a/tasks/lib/compiler.js +++ b/tasks/lib/compiler.js @@ -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 @@ -44,6 +44,10 @@ var Compiler = function(grunt, options, cwd) { path = path.replace(/\/?$/, '/'); } + if(cwd && expanded){ + url = url.replace(cwd, '').replace(/^\//,''); + } + // Append formatted URL path += Url.format( Url.parse( url.replace(/\\/g, '/') ) ); @@ -165,7 +169,7 @@ var Compiler = function(grunt, options, cwd) { * @return {String} Template path */ this.path = function(file) { - if (cwd) { + if (cwd && !expanded) { return cwd + '/' + file; } diff --git a/test/angular-templates_test.js b/test/angular-templates_test.js index 4806924..d381cae 100644 --- a/test/angular-templates_test.js +++ b/test/angular-templates_test.js @@ -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); diff --git a/test/expected/relative_url_expand_three.js b/test/expected/relative_url_expand_three.js new file mode 100644 index 0000000..a8ba9cf --- /dev/null +++ b/test/expected/relative_url_expand_three.js @@ -0,0 +1,12 @@ +angular.module('relative_url_expand').run(['$templateCache', function($templateCache) { + 'use strict'; + + $templateCache.put('three/three.html', + "

Three

\n" + + "\n" + + "\n" + + "\n" + + "\n" + ); + +}]); diff --git a/test/expected/relative_url_expand_three_two.js b/test/expected/relative_url_expand_three_two.js new file mode 100644 index 0000000..1201605 --- /dev/null +++ b/test/expected/relative_url_expand_three_two.js @@ -0,0 +1,12 @@ +angular.module('relative_url_expand').run(['$templateCache', function($templateCache) { + 'use strict'; + + $templateCache.put('three/three_two.html', + "

Three Two

\n" + + "\n" + + "\n" + + "\n" + + "\n" + ); + +}]); diff --git a/test/fixtures/three/three.html b/test/fixtures/three/three.html new file mode 100644 index 0000000..16f592c --- /dev/null +++ b/test/fixtures/three/three.html @@ -0,0 +1,5 @@ +

Three

+ + + + diff --git a/test/fixtures/three/three_two.html b/test/fixtures/three/three_two.html new file mode 100644 index 0000000..53e05c0 --- /dev/null +++ b/test/fixtures/three/three_two.html @@ -0,0 +1,5 @@ +

Three Two

+ + + +