Last active
August 29, 2015 14:07
-
-
Save stryju/0dbdcaa99c4540b64841 to your computer and use it in GitHub Desktop.
sample partials
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gulp.task( 'templates', function () { | |
// this is the partials.js | |
var wrapper = require( './partials' ); | |
return gulp.src( 'assets/partials/**/*.html' ) | |
.pipe( $.plumber() ) | |
.pipe( wrapper({ | |
// this will be added to the file path (template id) | |
root : '', | |
// this will be stripped away from the file path (template id) | |
base : 'assets/templates', | |
})) | |
.pipe( $.util.buffer( function ( err, files ) { | |
var partials = files.map( function ( file ) { | |
return file.contents.toString(); | |
}).join( '\n ' ); | |
return gulp.src( 'assets/templates/index.html' ) | |
.pipe( $.template({ | |
url : config.urls, | |
partials : partials | |
})) | |
.pipe( gulp.dest( 'build' ) ); | |
})); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jshint node:true | |
var gutil = require( 'gulp-util' ); | |
var through = require( 'through2' ); | |
var path = require( 'path' ); | |
function wrap( file, url ) { | |
'use strict'; | |
var template = '<script type="text/ng-template" id="${ url }">${ contents }</script>'; | |
return gutil.template( template, { | |
url : url, | |
contents : file.contents.toString(), | |
file : file | |
}); | |
} | |
function partial( options ) { | |
'use strict'; | |
options = options || {}; | |
var root = options.root || ''; | |
var base = options.base || ''; | |
if ( base && base.substr( -1 ) !== path.sep ) { | |
base += path.sep; | |
} | |
return through.obj( function ( file, enc, cb ) { | |
if ( file.isNull() ) { | |
this.push( file ); | |
return cb(); | |
} | |
if ( file.isStream() ) { | |
this.emit( 'error', new gutil.PluginError( 'gulp-partial', 'Streaming not supported' )); | |
return cb(); | |
} | |
var url = path.join( root, file.path.replace( path.resolve( base || file.base ), '' ) ); | |
if ( process.platform === 'win32' ) { | |
url = url.replace( /\\/g, '/' ); | |
} | |
try { | |
file.contents = new Buffer( wrap( file, url )); | |
} catch ( err ) { | |
this.emit( 'error', new gutil.PluginError( 'gulp-partial', err )); | |
} | |
this.push( file ); | |
cb(); | |
}); | |
} | |
module.exports = partial; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if few ppl like it, i'll turn it into a proper gulp plugin :-)