Explorar o código

Minify CSS and templates

Gerald %!s(int64=10) %!d(string=hai) anos
pai
achega
c967b3c43d
Modificáronse 4 ficheiros con 65 adicións e 32 borrados
  1. 8 31
      gulpfile.js
  2. 2 1
      package.json
  3. 19 0
      scripts/minifyHtml.js
  4. 36 0
      scripts/templateCache.js

+ 8 - 31
gulpfile.js

@@ -1,39 +1,12 @@
 'use strict';
 
 const gulp = require('gulp');
-const gutil = require('gulp-util');
-const minifyCss = require('gulp-minify-css');
-const minifyHtml = require('gulp-minify-html');
-const through = require('through2');
 const concat = require('gulp-concat');
 const merge2 = require('merge2');
-const _ = require('underscore');
+const minifyCss = require('gulp-minify-css');
+const gulpFilter = require('gulp-filter');
 const del = require('del');
-
-function templateCache() {
-  const contentTpl = '_.cache.put(<%= name %>, <%= content %>);\n';
-  let content = '/* Below are templates cached from `_.template` with love :) */\n\n';
-  function bufferContents(file, enc, cb) {
-    if (file.isNull()) return cb();
-    if (file.isStream())
-      return this.emit('error', new gutil.PluginError('VM-cache', 'Stream is not supported.'));
-    content += gutil.template(contentTpl, {
-      name: JSON.stringify(('/' + file.relative).replace(/\\/g, '/')),
-      content: _.template(String(file.contents), {variable: 'it'}).source,
-      file: '',
-    });
-    cb();
-  }
-  function endStream(cb) {
-    this.push(new gutil.File({
-      base: '',
-      path: 'template.js',
-      contents: new Buffer(content),
-    }));
-    cb();
-  }
-  return through.obj(bufferContents, endStream);
-}
+const templateCache = require('./scripts/templateCache');
 
 gulp.task('templates', function () {
   return merge2([
@@ -47,13 +20,17 @@ gulp.task('clean', function () {
   return del(['dist']);
 });
 
-gulp.task('copy-files',function(){
+gulp.task('copy-files', function () {
+  const cssFilter = gulpFilter(['**/*.css'], {restore: true});
   return gulp.src([
     'src/**',
     '!src/cache.js',
     '!src/**/templates/**',
     '!src/**/templates',
   ], {base:'src'})
+  .pipe(cssFilter)
+  .pipe(minifyCss())
+  .pipe(cssFilter.restore)
   .pipe(gulp.dest('dist/'));
 });
 

+ 2 - 1
package.json

@@ -6,9 +6,10 @@
     "del": "^2.1.0",
     "gulp": "^3.9.0",
     "gulp-concat": "^2.6.0",
+    "gulp-filter": "^3.0.1",
     "gulp-minify-css": "^1.2.1",
-    "gulp-minify-html": "^1.0.4",
     "gulp-util": "^3.0.7",
+    "html-minifier": "^1.0.0",
     "merge2": "^0.3.6",
     "through2": "^2.0.0",
     "underscore": "^1.8.3"

+ 19 - 0
scripts/minifyHtml.js

@@ -0,0 +1,19 @@
+'use strict';
+
+const minify = require('html-minifier').minify;
+
+module.exports = function (data) {
+  data = String(data);
+  const fragments = [];
+  const html = data.replace(/<%.*?%>/g, function (match) {
+    fragments.push(match);
+    return `__frag_${fragments.length - 1}__`;
+  });
+  return minify(html, {
+    removeComments: true,
+    collapseWhitespace: true,
+    conservativeCollapse: true,
+  }).replace(/__frag_(\d+)__/g, function (match, id) {
+    return fragments[id];
+  });
+};

+ 36 - 0
scripts/templateCache.js

@@ -0,0 +1,36 @@
+'use strict';
+
+const gutil = require('gulp-util');
+const through = require('through2');
+const _ = require('underscore');
+const minified = require('./minifyHtml');
+
+/*function minified(data) {
+  data = String(data);
+  return data.replace(/\s+/g, ' ');
+}*/
+
+module.exports = function templateCache() {
+  const contentTpl = '_.cache.put(<%= name %>, <%= content %>);\n';
+  let content = '/* Below are templates cached from `_.template` with love :) */\n\n';
+  function bufferContents(file, enc, cb) {
+    if (file.isNull()) return cb();
+    if (file.isStream())
+      return this.emit('error', new gutil.PluginError('VM-cache', 'Stream is not supported.'));
+    content += gutil.template(contentTpl, {
+      name: JSON.stringify(('/' + file.relative).replace(/\\/g, '/')),
+      content: _.template(minified(file.contents), {variable: 'it'}).source,
+      file: '',
+    });
+    cb();
+  }
+  function endStream(cb) {
+    this.push(new gutil.File({
+      base: '',
+      path: 'template.js',
+      contents: new Buffer(content),
+    }));
+    cb();
+  }
+  return through.obj(bufferContents, endStream);
+};