gulp-domly

DOMly plugin for gulp

gulp-domly

Plugin for gulp 3 to precompile DOMly templates

Install gulp-domly as a development dependency:

npm install --save-dev gulp-domly

gulp-declare can be used to safely declare template namespaces and make templates available for use in the browser.

First, install development dependencies:

npm install --save-dev gulp-handlebars gulp-declare gulp-concat

Given the following directory structure:

├── gulpfile.js               # Your gulpfile
└── source/                   # Your application's source files
    └── templates/            # Templates named with dot notation
        └── app.html          # Delcared as MyApp.templates.app
        └── app.header.html   # Delcared as MyApp.templates.app.header

To compile all templates in source/templates/ to build/js/templates.js under the MyApp.templates namespace:

var domly = require('gulp-domly');
var declare = require('gulp-declare');
var concat = require('gulp-concat');
 
gulp.task('templates', function(){
  gulp.src(['source/templates/*.html'])
    .pipe(domly())
    .pipe(declare({
      namespace: 'MyApp.templates',
      noRedeclare: true, // Avoid duplicate declarations 
    }))
    .pipe(concat('templates.js'))
    .pipe(gulp.dest('build/js/'));
});

The template's filename is combined with the namespace, so the resulting build/js/templates.js would look like:

this["MyApp"] = this["MyApp"] || {};
this["MyApp"]["templates"] = this["MyApp"]["templates"] || {};
this["MyApp"]["templates"]["app"] = function() { /* compiled */ };
this["MyApp"]["templates"]["app"] = this["MyApp"]["templates"]["app"] || {};
this["MyApp"]["templates"]["app"]["header"] = function() { /* compiled */ };

You can then use your templates:

document.body.appendChild(MyApp.templates.app(data));

See the gulp-define-module documentation for details on how to define templates as AMD, Node, CommonJS, and hybrid modules.

First, install development dependencies:

npm install --save-dev gulp-handlebars gulp-define-module

To compile all templates in source/templates/ to build/js/templates/ as separate modules:

var domly = require('gulp-domly');
var defineModule = require('gulp-define-module');
 
gulp.task('templates', function(){
  gulp.src(['source/templates/*.html'])
    .pipe(domly())
    .pipe(defineModule('amd'))
    .pipe(gulp.dest('build/js/templates/'));
});

You can pass any compiler options accepted by domly.precompile():

gulp.task('templates', function(){
  gulp.src(['source/templates/*.html'])
    .pipe(domly({
      compilerOptions: {
        stripWhitespace: true,
        preserveComments: true
      }  
    }))
    .pipe(defineModule('amd'))
    .pipe(gulp.dest('build/js/templates/'));
});

You can use different versions of DOMly by specifying the version in your package.json and passing it as options.domly:

{
  "devDependencies": {
    "domly": "^0.0.7"
  }
}
gulp.task('templates', function(){
  gulp.src('source/templates/*.html')
    .pipe(domly({
      domly: require('domly')
    }))
    .pipe(defineModule('amd'))
    .pipe(gulp.dest('build/js/templates/'));
});

Type: Object

Compiler options to pass to domly.precompile().

Type: Object

DOMly library to use for precompilation. By default, the latest stable version of DOMly is used.