Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Professional CSS3

You're reading from   Professional CSS3 Harness the power of CSS3 to design stunning, modern websites

Arrow left icon
Product type Paperback
Published in May 2016
Publisher Packt
ISBN-13 9781785880940
Length 362 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Piotr Sikora Piotr Sikora
Author Profile Icon Piotr Sikora
Piotr Sikora
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Foundations and Tools 2. Mastering of Fundamentals FREE CHAPTER 3. Mastering of Pseudoelements and Pseudoclasses 4. Responsive Websites – Prepare Your Code for Specific Devices 5. Using Background Images in CSS 6. Styling Forms 7. Resolving Classic Problems 8. Usage of Flexbox Transform 9. Calc, Gradients, and Shadows 10. Don't Repeat Yourself – Let's Create a Simple CSS Framework 11. Mailers Fundamentals 12. Scalability and Modularity 13. Code Optimization 14. Final Automatization and Processes Optimization Index

Simple automatization (with Gulp)

Every time we are compiling project files (for example, Compass, Jade, image optimization, and so on), we are thinking about how we can automatize and speed up the process. The first idea—some terminal snippets and compiling invokers. But we can use grunt.js and gulp.js. What are Grunt and Gulp? In short—task runners. You can define a list of tasks, which you repeat all the time, group them into some logical structure, and run.

In most projects, you can use them to automatize a process of SASS/Compass compilation.

I assume that you have installed Node.js, Ruby, sass, and Compass. If not, I recommend you to do this first. To install all of the listed software, you need to visit:

On these pages, you can find guides and tutorials on how to install all of this software.

Then you will need to create a basic structure for your project. It is best to create folders:

  • src: In this folder we will keep our source files
  • dist: In this folder we will keep our compiled files

In the src folder, please create a css folder, which will keep our SASS files.

Then in the root folder, run the following command line:

npm init
npm install gulp-compass gulp --save-dev

In gulpfile.js add the following lines of code:

var gulp = require('gulp'),
    compass = require('gulp-compass');

gulp.task('compass', function () {
    return gulp.src('src/styles/main.sass')
        .pipe(compass({
            sass: 'src/styles',
            image: 'src/images',
            css: 'dist/css',
            sourcemap: true,
            style: 'compressed'
        }));
});

gulp.task('default', function () {
    gulp.watch('src/css/**/*.sass', ['compass']);
});

Now you can run your automatizer with the following in your command line:

gulp

This will run the default task from your gulpfile.js, which will add a watcher to the files with .sass extensions, which are located in the src/css folder. Every time you change any file in this location, your task compass will run. It means that it will run the compass task and create a sourcemap for us. We could use a default compass command, but gulp.js is a part of the modern frontend developer workflow. We will be adding new functions to this automatizer in the next chapters.

Let's analyze the code a little deeper:

gulp.task('default', function () {
    gulp.watch('src/css/**/*.sass', ['compass']);
});

The preceding code defines the default task. It appends a watcher, which checks the src/css/**/*.sass location for sass files. It means that every file in a src/css folder and any subsequent folder, for example, src/css/folder/file.sass, will have a watcher. When files in this location are changed, the task defined in the array [compass]will run. Our task compass is the only element in the array but it, of course, can be extended (we will do this in the next chapters).

Now let's analyze the task compass:

gulp.task('compass', function () {
    return gulp.src('src/styles/main.sass')
        .pipe(compass({
            sass: 'src/styles',
            image: 'src/images',
            css: 'dist/css',
            sourcemap: true,
            style: 'compressed'
      }));
});

It will compile the gulp.src('src/styles/main.sass)file and save the compiled file in pipe (gulp.dest('style.css')). The compass task is defined in pipe:

.pipe(compass({
            sass: 'src/styles',
            image: 'src/images',
            css: 'dist/css',
            sourcemap: true,
            style: 'compressed'
      }))

The first line of this task defines the source folder for SASS files. The second line defines the images folder. The third line sets the destination of the CSS file. The fourth line is set to generate a source map for the file (for easier debugging).The fifth line defines the style of the saved CSS file; in this case, it will be compressed (it means that it will be ready for production code).

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime