Skip to content

Commit

Permalink
Quite big update:
Browse files Browse the repository at this point in the history
Add gitignore file,
Remove examples and replaced them with comments,
Replaced cssnano with clean-css (seems to be faster...),
Put tasks in "if"s so they're not available,
Optimized a little the code,
Make "dump" use only specified tasks to be faster
  • Loading branch information
Pierstoval committed May 26, 2016
1 parent 220d3a5 commit d36edfd
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/

174 changes: 110 additions & 64 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,99 @@
// There here is YOUR config
// This var is an exemple of the assets you can use in your own application.
// The array KEYS correspond to the OUTPUT files,
// The array VALUES contain a LIST OF SOURCE FILES
/*
There here is YOUR config
This var is an exemple of the assets you can use in your own application.
The array KEYS correspond to the OUTPUT files,
The array VALUES contain a LIST OF SOURCE FILES
*/
var config = {
"output_directory": "web",
"files_to_watch": [
// Here you can add other files to watch when using "gulp watch".
// They will automatically run the "dump" command.
// It is VERY useful when you use massively less/sass "import" rules, for example.
"src/AppBundle/Resources/public/less/*.less"
/**
* Here you can add other files to watch when using "gulp watch".
* They will automatically run the "dump" command when modified.
* It is VERY useful when you use massively less/sass "import" rules, for example.
* Example:
"src/AppBundle/Resources/public/less/*.less"
*/
],
"less": {
"css/main_less.css": [
"web/components/bootstrap/less/bootstrap.less",
"src/AppBundle/Resources/public/less/main.less"
]
/**
* All files from this section are parsed with LESS plugin and dumped into a CSS file.
* If using "--prod" in command line, will minify with "clean-css".
* Example:
"css/main_less.css": [
"web/components/bootstrap/less/bootstrap.less",
"src/AppBundle/Resources/public/less/main.less"
]
*/
},
"sass": {
"css/main_sass.css": [
"src/AppBundle/Resources/public/less/main.scss"
]
/**
* All files from this section are parsed with SASS plugin and dumped into a CSS file.
* If using "--prod" in command line, will minify with "clean-css".
* Example:
"css/main_sass.css": [
"src/AppBundle/Resources/public/less/main.scss"
]
*/
},
"css": {
"css/main.css": [
"src/AppBundle/Resources/public/css/main.css"
]
/**
* All files from this section are just concatenated and dumped into a CSS file.
* If using "--prod" in command line, will minify with "clean-css".
* Example:
"css/main.css": [
"src/AppBundle/Resources/public/css/main.css"
]
*/
},
"js": {
"js/main.js": [
"web/components/bootstrap/dist/bootstrap-src.js",
"src/AppBundle/Resources/public/css/main.js"
]
/**
* All files from this section are just concatenated and dumped into a JS file.
* If using "--prod" in command line, will minify with "uglyflyjs".
* Example:
"js/main.js": [
"web/components/bootstrap/dist/bootstrap-src.js",
"src/AppBundle/Resources/public/css/main.js"
]
*/
}
};

/*************** End config ***************/
// Everything AFTER this line of code is updatable to the latest version of this gulpfile.
// Check it out there if you need: https://gist.github.com/Pierstoval/9d88b0dcb64f30eff4dc

/************* Some polyfills *************/

Object.prototype.p_size = function() {
var size = 0, key;
for (key in this) {
if (this.hasOwnProperty(key)) {
size++;
}
}
return size;
};

Object.prototype.p_forEach = function(callback) {
var key;
for (key in this) {
if (this.hasOwnProperty(key)) {
callback.apply(this, [key, this[key]]);
}
}
return this;
};

/*************** Global vars ***************/

var isProd = process.argv.indexOf('--prod') >= 0,
hasLess = config.less.p_size() > 0,
hasSass = config.sass.p_size() > 0,
hasCss = config.css.p_size() > 0,
hasJs = config.js.p_size() > 0
;

// Required extensions
var gulp = require('gulp'),
gulpif = require('gulp-if'),
Expand All @@ -46,13 +102,11 @@ var gulp = require('gulp'),
sass = require('gulp-sass'),
concat = require('gulp-concat'),
uglyfly = require('gulp-uglyfly'),
cssnano = require('gulp-cssnano'),
cleancss = require('gulp-clean-css'),
sourcemaps = require('gulp-sourcemaps'),
autoprefixer = require('gulp-autoprefixer')
;

var isProd = process.argv.indexOf('--prod') >= 0;

/*************** Gulp tasks ***************/

/**
Expand All @@ -71,7 +125,7 @@ gulp.task('less', function() {
.pipe(less())
.pipe(concat(assets_output))
.pipe(autoprefixer())
.pipe(gulpif(isProd, cssnano()))
.pipe(gulpif(isProd, cleancss()))
.pipe(concat(assets_output))
.pipe(gulp.dest(outputDir))
;
Expand Down Expand Up @@ -99,7 +153,7 @@ gulp.task('sass', function() {
.pipe(sass())
.pipe(concat(assets_output))
.pipe(autoprefixer())
.pipe(gulpif(isProd, cssnano()))
.pipe(gulpif(isProd, cleancss()))
.pipe(concat(assets_output))
.pipe(gulp.dest(outputDir))
;
Expand All @@ -126,7 +180,7 @@ gulp.task('css', function() {
.src(assets)
.pipe(concat(assets_output))
.pipe(autoprefixer())
.pipe(gulpif(isProd, cssnano()))
.pipe(gulpif(isProd, cleancss()))
.pipe(concat(assets_output))
.pipe(gulp.dest(outputDir))
;
Expand Down Expand Up @@ -176,39 +230,26 @@ gulp.task('watch', ['dump'], function() {
console.log('File "' + event.path + '" updated');
},
other_files_to_watch = config.files_to_watch || [],
files_to_watch = [],
i;
files_to_watch = [];

console.info('Night gathers, and now my watch begins...');

for (i in config.less) {
if (!config.less.hasOwnProperty(i)) {
continue;
}
files_less.push(config.less[i]);
files_to_watch.push(config.less[i]);
}
for (i in config.sass) {
if (!config.sass.hasOwnProperty(i)) {
continue;
}
files_sass.push(config.sass[i]);
files_to_watch.push(config.sass[i]);
}
for (i in config.css) {
if (!config.css.hasOwnProperty(i)) {
continue;
}
files_css.push(config.css[i]);
files_to_watch.push(config.css[i]);
}
for (i in config.js) {
if (!config.js.hasOwnProperty(i)) {
continue;
}
files_js.push(config.js[i]);
files_to_watch.push(config.js[i]);
}
config.less.p_forEach(function(key, less){
files_less.push(config.less[less]);
files_to_watch.push(config.less[less]);
});
config.sass.p_forEach(function(key, sass){
files_sass.push(config.sass[sass]);
files_to_watch.push(config.sass[sass]);
});
config.css.p_forEach(function(key, css){
files_css.push(config.css[css]);
files_to_watch.push(config.css[css]);
});
config.js.p_forEach(function(key, js){
files_js.push(config.js[js]);
files_to_watch.push(config.js[js]);
});

if (files_to_watch.length) {
console.info('Watching file(s):');
Expand All @@ -220,24 +261,29 @@ gulp.task('watch', ['dump'], function() {
if (other_files_to_watch.length) {
gulp.watch(other_files_to_watch, ['dump']).on('change', callback);
}
if (files_less.length) {
if (hasLess) {
gulp.watch(files_less, ['less']).on('change', callback);
}
if (files_sass.length) {
if (hasSass) {
gulp.watch(files_sass, ['sass']).on('change', callback);
}
if (files_css.length) {
if (hasCss.length) {
gulp.watch(files_css, ['css']).on('change', callback);
}
if (files_js.length) {
if (hasJs.length) {
gulp.watch(files_js, ['js']).on('change', callback);
}
});

/**
* Runs all the needed commands to dump all assets and manifests
*/
gulp.task('dump', ['less', 'sass', 'css', 'js']);
var dumpTasks = [];
if (hasLess) { dumpTasks.push('less'); }
if (hasSass) { dumpTasks.push('sass'); }
if (hasCss) { dumpTasks.push('css'); }
if (hasJs) { dumpTasks.push('js'); }
gulp.task('dump', dumpTasks);

/**
* Small user guide
Expand All @@ -247,7 +293,7 @@ gulp.task('default', function(){
console.info("usage: gulp [command] [--prod]");
console.info("");
console.info("Options:");
console.info(" --prod If specified, will run cssnano and uglyfyjs when dumping the assets.");
console.info(" --prod If specified, will run clean-css and uglyfyjs when dumping the assets.");
console.info("");
console.info("Commands:");
console.info(" less Dumps the sources in the `config.less` parameter from LESS files.");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"gulp-watch": "^4.3",
"gulp-concat": "^2.6",
"gulp-uglyfly": "^1.4",
"gulp-cssnano": "^2.0",
"gulp-clean-css": "^2.0",
"gulp-sourcemaps": "^1.6",
"gulp-autoprefixer": "^3.1"
},
Expand Down

0 comments on commit d36edfd

Please sign in to comment.