Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
Implement Front-end environment
Browse files Browse the repository at this point in the history
* Implemented Gulp taks
  * Fonts task: copy fonts into public folder
  * Images task: in dev simply copy images and in prod compress them
 before
  * CSS task: in dev compile SASS files into CSS with sourcemaps. In
 prod mode no sourcemaps and CSS is compressed
  * JS task: in dev compile ES6 into ES5 with sourcemaps. In prod mode
 no sourcemaps and JS is compressed
  * Build task: build/publish all assets (env or prod mode)
  * Watch tasks available
  * Help task available to list available commands
* Ignore published/builded files
  • Loading branch information
theofidry committed Oct 17, 2015
1 parent f04cbac commit 4b80a2c
Show file tree
Hide file tree
Showing 32 changed files with 350 additions and 5,458 deletions.
23 changes: 23 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
ecmaFeatures:
arrowFunctions: false

env:
browser: true
jquery: true

extends:
- airbnb

rules:
func-names: 0
id-length: 0
indent: [2, 4]
new-cap: 1
prefer-const: 1
strict:
- 2
- global
yoda:
- 2
- always
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Generated assets
web/assets
web/bundles

# Cache file of PHP-CS Fixer
.php_cs.cache

Expand Down
17 changes: 0 additions & 17 deletions .jshintrc

This file was deleted.

9 changes: 9 additions & 0 deletions gulp-tasks/css-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import csscss from 'gulp-csscss';

module.exports = function(gulp, config) {
return function() {
gulp.src(config.src + '/app.css')
.pipe(csscss())
;
};
};
5 changes: 5 additions & 0 deletions gulp-tasks/fonts-watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function(gulp, config) {
return function() {
gulp.watch(config.src + '/fonts/**/*', ['fonts']);
};
};
7 changes: 7 additions & 0 deletions gulp-tasks/fonts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function(gulp, config) {
return function() {
gulp.src(config.src + '/fonts/**/*')
.pipe(gulp.dest(config.dist + '/fonts'))
;
};
};
5 changes: 5 additions & 0 deletions gulp-tasks/images-watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function(gulp, config) {
return function() {
gulp.watch(config.src + '/img/**/*', ['img']);
};
};
24 changes: 24 additions & 0 deletions gulp-tasks/images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import image from 'gulp-image';

function buildDev(gulp, config) {
return function() {
gulp.src(config.src + '/img/**/*')
.pipe(gulp.dest(config.dist + '/img'))
;
};
}

function buildProd(gulp, config) {
return function() {
gulp.src(config.src + '/img/**/*')
.pipe(image())
.pipe(gulp.dest(config.dist + '/img'))
;
};
}

module.exports = function(gulp, config) {
let factory = ('prod' === process.env.NODE_ENV) ? buildProd : buildDev;

return factory(gulp, config);
};
9 changes: 9 additions & 0 deletions gulp-tasks/sass-lint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scsslint from 'gulp-scss-lint';

module.exports = function(gulp, config) {
return function() {
gulp.src(config.src + '/scss/**/*.scss')
.pipe(scsslint())
;
};
};
9 changes: 9 additions & 0 deletions gulp-tasks/sass-watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = function(gulp, config) {
return function() {
gulp.watch(config.src + '/scss/**/*.scss', ['css'])
.on('change', function(event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
})
;
};
};
45 changes: 45 additions & 0 deletions gulp-tasks/sass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import minifyCss from 'gulp-minify-css';
import plumber from 'gulp-plumber';
import sass from 'gulp-sass';
import sourcemaps from 'gulp-sourcemaps';

// https://github.com/sass/node-sass#options
const OPTIONS = {
prod: {
outputStyle: 'compressed',
},
dev: {
outputStyle: 'expanded',
},
};

function buildDev(gulp, config) {
return function() {
gulp.src(config.src + '/scss/app.scss')
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(sass(OPTIONS.dev))
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write())
.pipe(gulp.dest(config.dist))
;
};
}

function buildProd(gulp, config) {
return function() {
gulp.src(config.src + '/scss/app.scss')
.pipe(plumber())
.pipe(sass(OPTIONS.prod))
.pipe(sass().on('error', sass.logError))
.pipe(minifyCss())
.pipe(gulp.dest(config.dist))
;
};
}

module.exports = function(gulp, config) {
let factory = ('prod' === process.env.NODE_ENV) ? buildProd : buildDev;

return factory(gulp, config);
};
21 changes: 21 additions & 0 deletions gulp-tasks/scripts-lint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import eslint from 'gulp-eslint';

const OPTIONS = {
configFile: __dirname + '/../.eslintrc.yml',
};

module.exports = function(gulp, config) {
const LINT_FILES = [
config.src + '/scripts/**/*.js', // application scripts
__dirname + '/*.js', // gulps script files
__dirname + '/../.js', // gulp.js
];

return function() {
gulp.src(LINT_FILES)
.pipe(eslint(OPTIONS))
.pipe(eslint.format())
.pipe(eslint.failAfterError())
;
};
};
6 changes: 6 additions & 0 deletions gulp-tasks/scripts-watch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function(gulp, config) {
return function() {
config.watch = true;
gulp.watch(config.src + '/scripts/**/*.js', ['js']);
};
};
68 changes: 68 additions & 0 deletions gulp-tasks/scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import babelify from 'babelify';
import browserify from 'browserify';
import buffer from 'vinyl-buffer';
import source from 'vinyl-source-stream';
import sourcemaps from 'gulp-sourcemaps';
import uglify from 'gulp-uglify';
import watchify from 'watchify';
import _ from 'lodash';

const OPTIONS = {
debug: false,
transform: [babelify],
};

function logError(err) {
console.error(err);
this.emit('end');
}

function buildDev(gulp, config) {
return function() {
let _bundle;
if (true === config.watch) {
_bundle = watchify(browserify(config.browserify));
_bundle.on('update', buildDev);
} else {
_bundle = browserify(config.browserify);
}

_bundle
.bundle()
.on('error', logError)
.pipe(source('app.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(config.dist))
;
};
}

function buildProd(gulp, config) {
return function() {
browserify(config.browserify)
.bundle()
.on('error', logError)
.pipe(source('app.js'))
.pipe(buffer())
.pipe(uglify())
.on('error', logError)
.pipe(gulp.dest(config.dist))
;
};
}

module.exports = function(gulp, config) {
let factory = buildProd;

OPTIONS.entries = config.src + '/scripts/app.js';
config.browserify = _.assign(watchify.args, OPTIONS);

if ('prod' !== process.env.NODE_ENV) {
config.browserify.debug = true;
factory = buildDev;
}

return factory(gulp, config);
};
11 changes: 11 additions & 0 deletions gulp-tasks/shell-lint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import spellcheck from 'gulp-spellcheck';

const LINT_FILES = __dirname + '/../scripts/**/*.sh';

module.exports = function(gulp) {
return function() {
gulp.src(LINT_FILES)
.pipe(spellcheck())
;
};
};
17 changes: 17 additions & 0 deletions gulp-tasks/yaml-lint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import yaml from 'gulp-yaml-validate';

// Will not lint Symfony YAML files as it should be done via Symfony YAML linter
const LINT_FILES = [
__dirname + '/../.scrutinizer.yml',
__dirname + '/../.styleci.yml',
];

module.exports = function(gulp) {
return function() {
LINT_FILES.forEach(function(file) {
gulp.src(file)
.pipe(yaml())
;
});
};
};
48 changes: 48 additions & 0 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

var options = {
options: {
'env=prod': 'Assets build and processing are production ready'
}
};

var gulp = require('gulp-help')(require('gulp'), options);
var argv = require('yargs').argv;

var config = {
src: 'src/FrontBundle/Resources/assets',
dist: 'web/assets'
};

// Sets environment to production if "--env=prod" ou "--production" option
// is passed as a parameter. Otherwise default set to dev.
if ('prod' === argv.env || 'production' === argv.env) {
process.env.NODE_ENV = 'prod';
} else {
process.env.NODE_ENV = 'dev';
}

gulp.task('lint:yaml', 'Lint YAML files', require('./gulp-tasks/yaml-lint')(gulp, config));
gulp.task('lint:shell', 'Lint Bash shell files', require('./gulp-tasks/shell-lint')(gulp, config));

gulp.task('css', 'Build the CSS assets', require('./gulp-tasks/sass')(gulp, config));
gulp.task('check:css', 'Check CSS duplications', require('./gulp-tasks/css-check')(gulp, config));
gulp.task('lint:css', 'Lint SASS files', require('./gulp-tasks/sass-lint')(gulp, config));
gulp.task('watch:css', 'Watch CSS files to build on change', require('./gulp-tasks/sass-watch')(gulp, config));

gulp.task('fonts', 'Publish the fonts assets', require('./gulp-tasks/fonts')(gulp, config));
gulp.task('watch:fonts', 'Watch fonts files to publish on change', require('./gulp-tasks/fonts-watch')(gulp, config));

gulp.task('img', 'Process the images', require('./gulp-tasks/images')(gulp, config));
gulp.task('watch:img', 'Watch images to process on change', require('./gulp-tasks/images-watch')(gulp, config));

gulp.task('js', 'Build the JavaScript assets', require('./gulp-tasks/scripts')(gulp, config));
gulp.task('lint:js', 'Lint JavaScript files', require('./gulp-tasks/scripts-lint')(gulp, config));
gulp.task('watch:js', 'Watch JavaScript files to build on change', require('./gulp-tasks/scripts-watch')(gulp, config));

gulp.task('build', 'Build and publish all assets', ['css', 'fonts', 'img', 'js']);
gulp.task('watch', 'Watch all assets for build on change', ['watch:css', 'watch:fonts', 'watch:img', 'watch:js']);

gulp.task('start', 'Build/publish all assets and watch files for any change', ['build', 'watch']);

gulp.task('default', ['help']);
Loading

0 comments on commit 4b80a2c

Please sign in to comment.