Exemplo n.º 1
0
/**
 * pre-compiles all nunjucks templates and merges it with the
 * nunjucks-slim bundle. since nunjucks ignores UMD module exports,
 * the final js file cannot be bundled by browserify and we need a workaround:
 * generate a second js with all templates in global scope and include it
 * in the index.html before the primary application js.
 */
function compileTemplates(){
   const precompiledTemplates = gulp.src(paths.src.templates)
       .pipe(nunjucks.precompile())
       .on("error", notifyError);

   const nunjucksSlim = gulp.src(paths.src.nunjucksSlim);
   return merge(nunjucksSlim, precompiledTemplates)
       .pipe(concat("templates.js"))
       .pipe(gulpif(env === "production", uglify()))
       .pipe(gulp.dest(paths.dest.templates));
}
gulp.task('dist-templates', function () {
    return gulp.src(paths.htmlSrc + '**/*.html')
        .pipe(nunjucks.precompile({
            name: function (file) {
                let urlPath = path.dirname(file.relative) + "/" + path.basename(file.relative);
                if (urlPath.indexOf('./') !== -1) {
                    urlPath = urlPath.replace('./', '');
                }
                return urlPath;
            }
        }))
        .pipe(concat('templates.js'))
        .pipe(gulp.dest(paths.distAssetsDir + 'js/'));
});
gulp.task('precompile', () =>
  gulp.src(['views/pages/**/*.html'])
    .pipe(nunjucks.precompile())
    .pipe(concat('templates.js'))
    .pipe(uglify())
    .pipe(gulp.dest('dist/js'))