From 046a419c3116b5236fd8375db43e35963400fcff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Goetz?= Date: Tue, 21 Jul 2015 22:37:29 +0200 Subject: [PATCH] Passed configuration to gulp, due to grunt limitations --- Gruntfile.js | 28 +-------- gulpfile.js | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 25 ++++---- 3 files changed, 182 insertions(+), 38 deletions(-) create mode 100644 gulpfile.js diff --git a/Gruntfile.js b/Gruntfile.js index b80e9cf..b1e5c7d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -2,8 +2,6 @@ module.exports = function (grunt) { 'use strict'; grunt.loadNpmTasks('grunt-php'); - grunt.loadNpmTasks('grunt-contrib-less'); - grunt.loadNpmTasks('grunt-contrib-watch'); grunt.initConfig({ php: { @@ -15,32 +13,8 @@ module.exports = function (grunt) { router: "index.php" } } - }, - less: { - development: { - options: { - cleancss: true, - report: 'min' - }, - files: { - "resources/themes/daux-blue/css/theme.min.css": "resources/themes/daux-blue/less/theme.less", - "resources/themes/daux-green/css/theme.min.css": "resources/themes/daux-green/less/theme.less", - "resources/themes/daux-navy/css/theme.min.css": "resources/themes/daux-navy/less/theme.less", - "resources/themes/daux-red/css/theme.min.css": "resources/themes/daux-red/less/theme.less" - } - } - }, - watch: { - scripts: { - files: ['templates/default/theme/**/*.less'], - tasks: ['less'], - options: { - nospawn: true - }, - }, - }, + } }); - //grunt.registerTask('default', ['less', 'watch']); grunt.registerTask('default', ['php']); }; diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..25ebd24 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,167 @@ + +var gulp = require('gulp'), + php = require('gulp-connect-php'), + less = require('gulp-less'), + rename = require('gulp-rename'), + plumber = require('gulp-plumber'), + postcss = require('gulp-postcss'); + +var resources = { + daux_blue:{source: "resources/themes/daux-blue/less/theme.less", dest: "resources/themes/daux-blue/css/"}, + daux_green:{source: "resources/themes/daux-green/less/theme.less", dest: "resources/themes/daux-green/css/"}, + daux_navy:{source: "resources/themes/daux-navy/less/theme.less", dest: "resources/themes/daux-navy/css/"}, + daux_red:{source: "resources/themes/daux-red/less/theme.less", dest: "resources/themes/daux-red/css/"} +}; + +var unusedRules = [ + //We only use one glyphicon ... + ".glyphicon-", + "!.glyphicon-chevron-right", + + //we dont need all buttons + ".btn-", + "!.btn-primary", + "!.btn-secondary", + "!.btn-hero", + "!.btn-sidebar", + ".caret", + + //Typography + ".h1", + ".h2", + ".h3", + ".h4", + ".h5", + ".h6", + ".small", + + // We need only small columns + ".col-", + "!.col-sm", + + // We don't use a lot of navs and navbars + ".navbar-fixed", + ".navbar-inverse", + ".navbar-default", + ".nav-pills", + ".nav-tabs", + ".nav-stacked", + ".nav-justified", + + // And a few others we don't use + ".bg-", + ".table" +]; + +function prepare_rules(rules) { + var regexes = {inclusion: [], exclusion: []}, rule, pattern, regex, exclusion; + + for (rule in rules) { + if (!rules.hasOwnProperty(rule)) continue; + + pattern = rules[rule]; + + exclusion = pattern.indexOf('!') === 0; + if (exclusion) { pattern = pattern.slice(1); } + + regex = pattern.replace('.', '\\.').replace('*', '(.*)'); + + if (exclusion) { + regexes.exclusion.push(new RegExp(regex)); + } else { + regexes.inclusion.push(new RegExp(regex)); + } + } + + return regexes; +} + +function processPatterns(patterns, string) { + var i; + + for (i in patterns.exclusion) { + if (!patterns.exclusion.hasOwnProperty(i)) continue; + if (string.match(patterns.exclusion[i])) return false; + } + + for (i in patterns.inclusion) { + if (!patterns.inclusion.hasOwnProperty(i)) continue; + if (string.match(patterns.inclusion[i])) return true; + } +} + +function removeUnusedRules(rules) { + var regexes = prepare_rules(rules); + + return function(css) { + css.eachRule(function (rule) { + var removedSome = false, + selectors = rule.selectors, + i; + + for (i = 0; i < selectors.length; i++) { + if (processPatterns(regexes, selectors[i])) { + selectors.splice(i, 1); + i--; + removedSome = true; + } + } + + if(removedSome) { + if (selectors.length == 0) { + rule.removeSelf(); + } else { + rule.selectors = selectors; + } + } + }); + + return css; + } +} + + + +function createTask(source, dest) { + return function() { + return gulp.src(source) + .pipe(less()) + .pipe(postcss([ + removeUnusedRules(unusedRules), + require('csswring')({ + preserveHacks: true + }) + ])) + .pipe(rename({suffix: '.min'})) + .pipe(gulp.dest(dest)); + } +} + + + +var style_tasks = []; +for (var style in resources) { + gulp.task('style_' + style, createTask(resources[style].source, resources[style].dest)); + style_tasks.push('style_' + style); +} + +gulp.task("styles", style_tasks); + + +gulp.task('watch', function() { + + // Watch .less files + gulp.watch('resources/themes/**/*.less', ['styles']); + +}); + +gulp.task('php', function() { + php.server({ + keepalive: true, + open: true, + port: 8085, + router: "index.php" + }); +}); + +gulp.task('default', ['php']); diff --git a/package.json b/package.json index cc420ba..d22fe17 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,16 @@ { - "name": "daux.io", - "version": "0.1.1", - "private": true, - "dependencies": { - "grunt": "~0.4.1", - "grunt-php": "~0.3.0", - "grunt-contrib-less": "~0.9.0" - }, - "devDependencies": { - "grunt-contrib-watch": "~0.5.0" - } + "name": "daux.io", + "version": "0.1.1", + "private": true, + "devDependencies": { + "grunt": "~0.4.1", + "grunt-php": "~0.3.0", + "csswring": "^3.0.5", + "gulp": "^3.9.0", + "gulp-connect-php": "0.0.5", + "gulp-less": "^3.0.3", + "gulp-plumber": "^1.0.1", + "gulp-postcss": "^5.1.10", + "gulp-rename": "^1.2.2" + } }