官术网_书友最值得收藏!

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).

主站蜘蛛池模板: 葫芦岛市| 万源市| 霍州市| 浦城县| 宜春市| 西乌珠穆沁旗| 汉川市| 深泽县| 广东省| 广宗县| 宝丰县| 岚皋县| 故城县| 昌宁县| 长治市| 辰溪县| 玛曲县| 宜兴市| 通海县| 大兴区| 华亭县| 浦江县| 武山县| 北安市| 新绛县| 历史| 新河县| 金昌市| 威信县| 泾阳县| 合江县| 灵武市| 山丹县| 田阳县| 会东县| 兴仁县| 合川市| 沙洋县| 江都市| 西青区| 崇信县|