javascript - combine html template files into one JS file -


  1. i have html template files (underscore template syntax)
  2. these files saved in html format easy edit (ide syntax highlight)
  3. i don't want fetch them ajax, rather combine them , include them ajs file.
  4. using gulp task-runner, somehow combine all html this, javascript file include in build process:

template_file_name html file name.

var templates = {    template_file_name : '...template html string...',    template_file_name2 : '...template html string...',    template_file_name3 : '...template html string...' } 

i don't know how approach this, , how create such text files..yes can convert each individual file string, how can put inside object?


update - oct 25, 15 - es6 modules:

for want templates es6 modules, have created gulp-file-contents-to-modules

demo output:

export var file_name = "this bar."; export var file_name2 = "this foo.\r\n"; export var my-folder__file_name = "this baz.\r\n"; 

all npm packages related combining template files using gulp:

  1. https://www.npmjs.com/package/gulp-file-contents-to-keys
  2. https://www.npmjs.com/package/gulp-file-contents-to-modules
  3. https://www.npmjs.com/package/gulp-template-compile-es6

i've found wonderful tool want:

https://www.npmjs.org/package/gulp-template-compile

usage (as simple as):

gulp.task('templates', function () {     gulp.src('./views/templates/**/*.html')         .pipe(template()) // converts html js         .pipe(concat('templates.js'))         .pipe(gulp.dest('./js/dist/')) }); 

then can access key/value object window.jst. values functions (i don't know why, it's that)

update - august 21, 2015

i've decided use use gulp-file-contents-to-json simple thing possible generating json files' contents.

update - july 19, 2016

i've created 3 npm packages (might handy someone):

  1. https://www.npmjs.com/package/gulp-file-contents-to-keys
  2. https://www.npmjs.com/package/gulp-file-contents-to-modules
  3. https://www.npmjs.com/package/gulp-template-compile-es6

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -