templateCache.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. const header = `/* Templates cached from \`_.template\` with love :) */
  13. define('templates', function (require, exports, module) {
  14. var cache = require('cache');
  15. `;
  16. const footer = `
  17. });
  18. `;
  19. const contents = [];
  20. function bufferContents(file, enc, cb) {
  21. if (file.isNull()) return cb();
  22. if (file.isStream())
  23. return this.emit('error', new gutil.PluginError('VM-cache', 'Stream is not supported.'));
  24. contents.push(gutil.template(contentTpl, {
  25. name: JSON.stringify(('/' + file.relative).replace(/\\/g, '/')),
  26. content: _.template(minified(file.contents), {variable: 'it'}).source,
  27. file: '',
  28. }));
  29. cb();
  30. }
  31. function endStream(cb) {
  32. this.push(new gutil.File({
  33. base: '',
  34. path: 'template.js',
  35. contents: new Buffer(header + contents.join('') + footer),
  36. }));
  37. cb();
  38. }
  39. return through.obj(bufferContents, endStream);
  40. };