templateCache.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. const gutil = require('gulp-util');
  3. const through = require('through2');
  4. const _ = require('underscore');
  5. const minified = require('./minifyHtml');
  6. /*function minified(data) {
  7. data = String(data);
  8. return data.replace(/\s+/g, ' ');
  9. }*/
  10. module.exports = function templateCache() {
  11. const contentTpl = '_.cache.put(<%= name %>, <%= content %>);\n';
  12. let content = '/* Below are templates cached from `_.template` with love :) */\n\n';
  13. function bufferContents(file, enc, cb) {
  14. if (file.isNull()) return cb();
  15. if (file.isStream())
  16. return this.emit('error', new gutil.PluginError('VM-cache', 'Stream is not supported.'));
  17. content += gutil.template(contentTpl, {
  18. name: JSON.stringify(('/' + file.relative).replace(/\\/g, '/')),
  19. content: _.template(minified(file.contents), {variable: 'it'}).source,
  20. file: '',
  21. });
  22. cb();
  23. }
  24. function endStream(cb) {
  25. this.push(new gutil.File({
  26. base: '',
  27. path: 'template.js',
  28. contents: new Buffer(content),
  29. }));
  30. cb();
  31. }
  32. return through.obj(bufferContents, endStream);
  33. };