gulp-inject-template

0.2.2 • Public • Published

gulp-inject-template

Build Status npm version

Replaces require calls to html files with precompiled lodash/underscore template functions in your JS files.

Example

Input

var template = require('./template.html');
<!-- ./template.html -->
<h1>Template</h1>
With variable: <%= data %>

Output

var template = function(obj) {
obj || (obj = {});
 
var __t, __p = '';
with (obj) {
__p += '<h1>Template</h1>\nWith variable: ' +
((__t = ( data )) == null ? '' : __t);
 
}
return __p
};

Usage

gulp.task('build', function() {
  return gulp.src('src/**/*.js', {buffer: false})
    .pipe(injectTemplate(options))
    .pipe(gulp.dest('./dist'));
});

Arguments

Argument Type Description
options optional, Object Object that will be passed down to lodash template function as options. More details in the lodash documentation.

Using gulp-inject-template with buffers

gulp-inject-template currently does not support buffers.

If you want to use it with Babel or another module that does not support streams, you can wrap those plugins using gulp-streamify.

var streamify = require('gulp-streamify');
 
gulp.task('build', function() {
  return gulp.src('src/**/*.js', {buffer: false})
    .pipe(streamify(babel()))
    .pipe(injectTemplate({variable: 'data'}))
    .pipe(gulp.dest(dist))
  ;
});

Todo

  • Support source maps
  • Support buffers

Thanks & dependencies

Package Sidebar

Install

npm i gulp-inject-template

Weekly Downloads

1

Version

0.2.2

License

MIT

Last publish

Collaborators

  • alexandremeunier