Skip to content

Commit

Permalink
Add trailing separator to file.base if not present (#165)
Browse files Browse the repository at this point in the history
* Add trailing separator to file.base if not present

* Comment explaining need for #165
  • Loading branch information
dmellstrom authored and simonua committed Nov 6, 2018
1 parent af0f6ff commit 4149664
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ function templateCacheFiles(root, base, templateBody, transformUrl, escapeOption
if (typeof base === 'function') {
url = path.join(root, base(file));
} else {
url = path.join(root, file.path.replace(base || file.base, ''));
if (base === undefined) {
// Make sure file.base ends in a separator, to ensure correct relative
// path construction (provides compatibility with Gulp 4/Vinyl 2)
base = file.base += file.base.substr(-1) === path.sep ? "" : path.sep;
}
url = path.join(root, file.path.replace(base, ''));
}

if (root === '.' || root.indexOf('./') === 0) {
Expand Down
20 changes: 10 additions & 10 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('gulp-angular-templatecache', function () {
stream.on('data', function (file) {
assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js'));
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/template-a.html\',\'<h1 id="template-a">I\\\'m template A!</h1>\');\n$templateCache.put(\'/template-b.html\',\'<h1 id="template-b">I\\\'m template B!</h1>\');}]);');
assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'template-a.html\',\'<h1 id="template-a">I\\\'m template A!</h1>\');\n$templateCache.put(\'template-b.html\',\'<h1 id="template-b">I\\\'m template B!</h1>\');}]);');
cb();
});

Expand Down Expand Up @@ -155,7 +155,7 @@ describe('gulp-angular-templatecache', function () {
stream.on('data', function (file) {
assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js'));
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/tpl-a.html\',\'<h1 id="template-a">I\\\'m template A!</h1>\');}]);');
assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'tpl-a.html\',\'<h1 id="template-a">I\\\'m template A!</h1>\');}]);');
cb();
});

Expand Down Expand Up @@ -205,7 +205,7 @@ describe('gulp-angular-templatecache', function () {
stream.on('data', function (file) {
assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js'));
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\', []).run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/template-a.html\',\'<h1 id="template-a">I\\\'m template A!</h1>\');}]);');
assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\', []).run([\'$templateCache\', function($templateCache) {$templateCache.put(\'template-a.html\',\'<h1 id="template-a">I\\\'m template A!</h1>\');}]);');
cb();
});

Expand Down Expand Up @@ -331,7 +331,7 @@ describe('gulp-angular-templatecache', function () {
stream.on('data', function (file) {
assert.equal(file.path, path.normalize(__dirname + '/templates.js'));
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), '\'use strict\'; module.exports = angular.module(\'templates\', []).run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/template-a.html\',\'<h1 id="template-a">I\\\'m template A!</h1>\');}]);');
assert.equal(file.contents.toString('utf8'), '\'use strict\'; module.exports = angular.module(\'templates\', []).run([\'$templateCache\', function($templateCache) {$templateCache.put(\'template-a.html\',\'<h1 id="template-a">I\\\'m template A!</h1>\');}]);');
cb();
});

Expand All @@ -352,7 +352,7 @@ describe('gulp-angular-templatecache', function () {
stream.on('data', function (file) {
assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js'));
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'define([\'angular\'], function(angular) { \'use strict\'; return angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/template-a.html\',\'<h1 id="template-a">I\\\'m template A!</h1>\');}]);});');
assert.equal(file.contents.toString('utf8'), 'define([\'angular\'], function(angular) { \'use strict\'; return angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'template-a.html\',\'<h1 id="template-a">I\\\'m template A!</h1>\');}]);});');
cb();
});

Expand All @@ -373,7 +373,7 @@ describe('gulp-angular-templatecache', function () {
stream.on('data', function (file) {
assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js'));
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'import angular from \'angular\'; export default angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/template-a.html\',\'<h1 id="template-a">I\\\'m template A!</h1>\');}]);');
assert.equal(file.contents.toString('utf8'), 'import angular from \'angular\'; export default angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'template-a.html\',\'<h1 id="template-a">I\\\'m template A!</h1>\');}]);');
cb();
});

Expand All @@ -394,7 +394,7 @@ describe('gulp-angular-templatecache', function () {
stream.on('data', function (file) {
assert.equal(path.normalize(file.path), path.normalize(__dirname + '/templates.js'));
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), '(function(){\'use strict\';angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/template-a.html\',\'<h1 id="template-a">I\\\'m template A!</h1>\');}]);})();');
assert.equal(file.contents.toString('utf8'), '(function(){\'use strict\';angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'template-a.html\',\'<h1 id="template-a">I\\\'m template A!</h1>\');}]);})();');
cb();
});

Expand All @@ -420,7 +420,7 @@ describe('gulp-angular-templatecache', function () {
stream.on('data', function (file) {
assert.equal(file.path, path.normalize(__dirname + '/templates.js'));
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'var template = "$templateCache.put(\'/template-a.html\',\'yoo\');";');
assert.equal(file.contents.toString('utf8'), 'var template = "$templateCache.put(\'template-a.html\',\'yoo\');";');
cb();
});

Expand All @@ -442,7 +442,7 @@ describe('gulp-angular-templatecache', function () {
stream.on('data', function (file) { assert
assert.equal(file.path, path.normalize(__dirname + '/templates.js'));
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), '$templateCache.put(\'/template-a.html\',\'yoo\');');
assert.equal(file.contents.toString('utf8'), '$templateCache.put(\'template-a.html\',\'yoo\');');
cb();
});

Expand All @@ -466,7 +466,7 @@ describe('gulp-angular-templatecache', function () {
stream.on('data', function (file) {
assert.equal(file.path, path.normalize(__dirname + '/templates.js'));
assert.equal(file.relative, 'templates.js');
assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'/template-a.html\',\'yoo\');}]);');
assert.equal(file.contents.toString('utf8'), 'angular.module(\'templates\').run([\'$templateCache\', function($templateCache) {$templateCache.put(\'template-a.html\',\'yoo\');}]);');
cb();
});

Expand Down

0 comments on commit 4149664

Please sign in to comment.