Skip to content

Instantly share code, notes, and snippets.

@hamishhossack
Last active February 25, 2017 10:57
Show Gist options
  • Save hamishhossack/53add5c2e92f44e0dba0 to your computer and use it in GitHub Desktop.
Save hamishhossack/53add5c2e92f44e0dba0 to your computer and use it in GitHub Desktop.
Symfony 2 with Gulp for Front-End Assets (optimize & make universal)
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var uglifycss = require('gulp-uglifycss');
var less = require('gulp-less');
var rjs = require('requirejs').optimize;
var concat = require('gulp-concat');
var bower = require ('gulp-bower');
var watch = require('gulp-watch');
var batch = require('gulp-batch');
var path = require('path');
var exec = require('child_process').exec;
var env = process.env.NODE_ENV || 'development';
var locale = "en";
var paths = {
root: './web/',
styles: {
root: './src/*/Bundle/*/Resources/public/css/',
main: './src/*/Bundle/*/Resources/public/css/specific/jv1tus/style.less',
output: {
dev: './web/css/jv1tus',
prod: './web/dist/css/jv1tus'
}
},
javascripts: {
root: './src/*/Bundle/*/Resources/marionette',
app: './src/*/Bundle/*/Resources/marionette/apps/**/*.js',
output: {
dev: './web/js/',
prod: './web/js/app/dist'
}
}
};
gulp.task('clean', function () {
return exec('sudo rm -rf web/js/' +
'&& sudo rm -rf web/css/' +
'&& sudo rm -rf web/bundles/');
});
gulp.task('symfony', ['clean'], function () {
var cacheClear = exec('app/console ca:cl ' +
'&& app/console ca:cl --env=prod --no-debug ');
cacheClear.stdout.on('data', function (data) {
console.log(data);
});
cacheClear.stderr.on('data', function (data) {
console.log('ERROR -> ', data);
});
cacheClear.on('exit', function () {
console.log('Process Complete, cache was reset.');
});
return cacheClear;
});
gulp.task('less', function() {
var config = {
less: {
paths: [ path.join(__dirname, 'less', 'includes') ]
},
output: paths.styles.output.dev
};
if(/prod/.test(env)) {
config.output = paths.styles.output.prod;
config.less.compress = 'true';
uglifycss();
}
return gulp.src([paths.styles.main])
.pipe(less(config.less))
.pipe(gulp.dest(config.output));
});
gulp.task('bower', function() {
return bower().pipe(gulp.dest(paths.javascripts.vendors))
});
gulp.task('js', function(cb){
var options = {
appDir: paths.javascripts.root,
mainConfigFile: paths.javascripts.root + '/common.js',
baseUrl: './',
dir: paths.javascripts.output.dev,
skipDirOptimize: true,
keepBuildDir: true,
optimize: "none",
locale: "en_US",
removeCombined: true,
modules: [
{
name: 'common',
include: [
'jquery',
'bootstrap',
'underscore',
'backbone',
'marionette',
'underscore.string',
'parsley',
'jquery-cookie',
'bootstrap-multiselect'
]
},
{
name: 'apps/login/login_app',
exclude: ['common']
}
]
};
if(/prod/.test(env)) {
options.dir = paths.javascripts.output.prod;
options.removeCombined = true;
options.skipDirOptimize = false;
options.keepBuildDir = false;
options.uglify2 = {
output: {
beautify: false
},
compress: {
sequences: true
},
warnings: true,
mangle: true
};
}
rjs(options, function (buildResponse) {
console.log('build response', buildResponse);
cb();
}, cb);
});
gulp.task('watch', function () {
var onChange = function (event) {
console.log('File '+event.path+' has been '+event.type);
};
gulp.watch(paths.styles.root + '/**/*.less', ['less'])
.on('change', onChange);
gulp.watch(paths.javascripts.root + '/**/*.js', ['js'])
.on('change', onChange);
});
gulp.task('default', ['less', 'js']);
gulp.task('newcache', ['symfony', 'less', 'js']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment