How to fix template paths in templateCache? (gulp-angular-templatecache)

15,951

Solution 1

Figured it out! I should not have been using exact folder names, but globs ** in my config

var config = {
    srcTemplates:[
        'app/**/*.html',            
        'app/dashboard.html',
        '!app/index.html'
    ],
    destPartials: 'app/templates/'
};

The updated gulp.task:

gulp.task('html-templates', function() {
    return gulp.src(config.srcTemplates)
    .pipe(templateCache('templateCache.js', { module:'templateCache', standalone:true })
    ).pipe(gulp.dest(config.destPartials));
});

Now the output is correct:

$templateCache.put("beta/beta.html"...
$templateCache.put("header/control_header/controlHeader.html"...
$templateCache.put("panels/tags/tagsPanel.html"...

Solution 2

I ran into a similar issue, but in my case, it wasn't possible to use the same directory for all the gulp.src entries.

There is a solution that will work all those folders

return gulp.src(['public/assets/app1/**/*.tpl.html', 'public/assets/common_app/**/*.tpl.html'])
    .pipe(templateCache({
        root: "/",
        base: __dirname + "/public",
        module: "App",
        filename: "templates.js"
    }))
    .pipe(gulp.dest('public/assets/templates'))

That presumes the gulpfile is one directory up from the public directory. It will remove the base string from the full path of the files from src. Base can also be a function that will be passed the file object which could be helpful in more complicated situations. It's all around line 60 of the index.js in gulp-angular-templatecache

Solution 3

You can also use this gulp plugin which can read your routes, directives and replace the templateUrl with the template referenced in the templateUrl.

This will remove all headache regarding handling templateUrl in your application. This uses relative url to directive js files instead of absolute url.

src
+-hello-world
  |-hello-world-directive.js
  +-hello-world-template.html

hello-world-directive.js:

angular.module('test').directive('helloWorld', function () {
    return {
        restrict: 'E',
        // relative path to template
        templateUrl: 'hello-world-template.html'
    };
});

hello-world-template.html:

<strong>
    Hello world!
</strong>

gulpfile.js:

var gulp = require('gulp');
var embedTemplates = require('gulp-angular-embed-templates');

gulp.task('js:build', function () {
    gulp.src('src/scripts/**/*.js')
        .pipe(embedTemplates())
        .pipe(gulp.dest('./dist'));
});

gulp-angular-embed-templates will generate the following file:

angular.module('test').directive('helloWorld', function () {
    return {
        restrict: 'E',
        template:'<strong>Hello world!</strong>'
    };
});
Share:
15,951
Leon Gaban
Author by

Leon Gaban

Investor, Powerlifter, Crypto investor and global citizen You can also find me here: @leongaban | github | panga.ventures

Updated on July 25, 2022

Comments

  • Leon Gaban
    Leon Gaban almost 2 years

    I'm using gulp-angular-templatecache to generate a templateCache.js file which combines all my HTML template files into 1. (my full gulpfile)

    After injecting that new module into my app, my Directives will automatically pick up the templates and I won't need to add the partial .html files into my build folder.

    The problem is that the leading folder path is getting cut off, see my example below:

    The paths in my Directives:

    templateUrl : "panels/tags/tagsPanel.html"...
    templateUrl : "header/platform_header/platformHeader.html"...
    

    The paths in my produced templateCache file:

    $templateCache.put("tags/tagsPanel.html"...
    $templateCache.put("platform_header/platformHeader.html"...
    

    ^ panels and header are getting lost.

    enter image description here


    I'm trying to write a function that will fix that in my Gulpfile.

    The config section of my Gulpfile:

    var config = {
        srcPartials:[
            'app/beta/*.html', 
            'app/header/**/*.html',
            'app/help/*.html',
            'app/login/*.html',
            'app/notificaitons/*.html',
            'app/panels/**/*.html',
            'app/popovers/**/*.html',
            'app/popovers/*.html',
            'app/user/*.html',
            'app/dashboard.html'
        ],
        srcPaths:[
            'beta/', 
            'header/',
            'help/',
            'login/',
            'notificaitons/',
            'panels/',
            'popovers/',
            'popovers/',
            'user/',
            'dashboard.html'
        ],
        destPartials: 'app/templates/'
    };
    

    My html-templates gulp.task

    gulp.task('html-templates', function() {
        return gulp.src(config.srcPartials)
        .pipe(templateCache('templateCache.js', {
            root: updateRoot(config.srcPaths)
        },
        { module:'templateCache', standalone:true })
        ).pipe(gulp.dest(config.destPartials));
    });
    
    function updateRoot(paths) {
        for (var i = 0; i < paths.length; i++) {
            // console.log(paths);
            console.log(paths[i]);
            return paths[i];
        }
    }
    

    ^ The above is working, in that it uses the root option in gulp-angular-templatecache to append a new string in front of the template paths.

    Problem is my code above returns once and updates all the paths to the first item in the paths Array which is beta/.

    How would you write this so that it correctly replaces the path for each file?