This is a sample project that uses Gulp to:
- Copy files to a specific folder
- Optimize images
- Create your own webfont from SVG icons
- Compile SASS and JavaScript (Browserify)
- Concatenate files
- Add CSS browser prefixes
- Minify CSS / Uglify JS
- Version files (
styles.min.css
>>styles.min-69fb84d1.css
) - Sync changes to CSS/JS/HTML/PHP between browsers/devices (BrowserSync)
- Read more...
You can also keep watch on your PHPSpec tests. These have their own separate tasks.
To use Gulp, you will need to have a few programs installed on your machine:
- NodeJS
- Git Bash Console (recommended if you're on Windows)
If you want to run PHPSpec, then you will also need to install:
First, install Gulp globally on your machine: (only once)
npm install -g gulp
Install Bower globally on your machine: (only once)
npm install -g bower
With Bower you can easily manage third party JS and CSS packages. Thanks to the debowerify package, you can require Bower dependencies with Browserify:
var $ = require('jquery'); //=> Bower Package
module.exports = function() {
$('div').animate({
width: "200px"
}, 500);
}
IMPORTANT: Note that this only works if the package has set a main script in their
bower.json
file:main: "dist/jquery.js"
!
If you want to use BrowserSync, install it globally aswell:
npm install -g browser-sync
With BrowserSync all browsers and devices that are connected to your BrowserSync URL will be automatically updated on HTML, CSS and JS changes. Scrolling will also be synced on all connected devices!
Next, install Gulp and our dependencies in this project:
npm install
TIP: When
npm install
has finished it will automatically triggerbower install
.
If you have PHP and Composer installed, you can install PHPSpec, which is already added as a dependency in the composer.json
file.
composer install
This is the default project structure, but all folders can be changed in gulpfile.js
.
gulpfile.js
: It's all about this script!package.json
: NodeJS packages (Gulp dependencies, etc.)bower.json
: Frontend dependencies (jQuery, etc.)composer.json
: PHP dependencies (PHPSpec, etc.)phpspec.yml
: PHPSpec configuration fileassets/images/
: Original images, to be optimizedassets/icon-font/
: SVG icons that will be compiled into a webfontassets/sass/_icon-font.scss
: Generated SASS file for the webfontassets/sass/
: SASS source filesassets/js/
: JavaScript source filescompiled/
: Compiled SASS and JavaScript files (not yet concatenated)compiled/dist/
: Concatenated filespublic/images
: Optimized imagespublic/fonts/
: Webfont generated from SVG iconspublic/css/
: CSS production file (compiled, concatenated, versioned, ...)public/js/
: JavaScript production file (compiled, concatenated, versioned, ...)src/
: PHP Classes (PSR-4 autloaded with theApp
namespace)spec/
: PHPSpec tests
The webfont's SASS file is generated from a template in the
assets
folder. Example icon made by Freepik from Flaticon is licensed under CC BY 3.0.
TIP: if you don't want all the "Starting" and "Finished" task logging in the console window, then use the
--silent
option when you rungulp
.
- Copy files to a specific folder
- Optimize images
- Compile SVG icons into a webfont and create a SASS file
- Compile SASS and JS (Browserify)
- Concatenate with vendor CSS and JS files
- Combine CSS media queries
- Add browser prefixes to CSS
- Uglify JS
- Minify CSS
- Version CSS and JS files
- Remove compiled CSS and JS files and only keep the one used for production
- Use source maps for debugging
- DO NOT combine CSS media queries (does not work well with source maps)
- DO NOT Uglify JS / Minify CSS
- DO NOT remove compiled CSS and JS files
- Start watching SVG, SASS and JS files for changes
- Start local server and sync changes to CSS/JS/HTML/PHP between browsers/devices (BrowserSync)
- Run PHPSpec tests and start watching for changes
gulp serve
: Start local server and sync changes to CSS/JS/HTML/PHP between browsers/devices (BrowserSync)
gulp copy
: Copy files to a specific foldergulp images
: Optimize imagesgulp icon-font
: Compile SVG icons into a webfont and create the SASS filegulp css
: Run all CSS tasksgulp js
: Run all JS tasksgulp version
: Version files (after runninggulp css
andgulp js
)
gulp watch-copy
: Watch specified files and copy them to a foldergulp watch-images
: Watch image assets and optimize themgulp watch-icon-font
: Watch SVG icons, compile them into a webfontgulp watch-css
: Watch SASS files for changes and run all CSS tasksgulp watch-js
: Watch JS files for changes and run all JS tasks
gulp sass
: Compile SASS, combine media queries, add prefixes, minifygulp watch-sass
: Watch SASS files for changes and compile SASS, combine media queries, add prefixes, minifygulp browserify
: Compile JS, uglifygulp watch-browserify
: Watch JS files for changes, compile JS, uglify
gulp concat-css
: Concatenate CSS files, combine media queries, add prefixes, minifygulp concat-js
: Concatenate JS files, uglify
gulp minify-css
: Minify an array of CSS files and save them with a.min
suffixgulp uglify-js
: Uglify an array of JS files and save them with a.min
suffix
gulp cleanup-css
: Remove compiled CSS file and only keep the one used for productiongulp cleanup-js
: Remove compiled JS file and only keep the one used for production
TIP: You can use
--dev
to prevent minification, save source maps and keep compiled files. This behavior is configurable ingulpfile.js
.