UNPKG

5.94 kBMarkdownView Raw
1# gulp-sass [![Build Status](https://travis-ci.org/dlmanning/gulp-sass.svg?branch=master)](https://travis-ci.org/dlmanning/gulp-sass) [![Join the chat at https://gitter.im/dlmanning/gulp-sass](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/dlmanning/gulp-sass?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![npm version](https://badge.fury.io/js/gulp-sass.svg)](http://badge.fury.io/js/gulp-sass)
2
3Sass plugin for [Gulp](https://github.com/gulpjs/gulp).
4
5**_Before filing an issue, please make sure you have [Updated to the latest Gulp Sass](https://github.com/dlmanning/gulp-sass/wiki/Update-to-the-latest-Gulp-Sass) and have gone through our [Common Issues and Their Fixes](https://github.com/dlmanning/gulp-sass/wiki/Common-Issues-and-Their-Fixes) section._**
6
7# Support
8
9Only [Active LTS and Current releases][1] are supported.
10
11[1]: https://github.com/nodejs/Release#release-schedule
12
13# Install
14
15```
16npm install node-sass gulp-sass --save-dev
17```
18
19# Basic Usage
20
21Something like this will compile your Sass files:
22
23```javascript
24'use strict';
25
26var gulp = require('gulp');
27var sass = require('gulp-sass');
28
29sass.compiler = require('node-sass');
30
31gulp.task('sass', function () {
32 return gulp.src('./sass/**/*.scss')
33 .pipe(sass().on('error', sass.logError))
34 .pipe(gulp.dest('./css'));
35});
36
37gulp.task('sass:watch', function () {
38 gulp.watch('./sass/**/*.scss', ['sass']);
39});
40```
41
42You can also compile synchronously, doing something like this:
43
44```javascript
45'use strict';
46
47var gulp = require('gulp');
48var sass = require('gulp-sass');
49
50sass.compiler = require('node-sass');
51
52gulp.task('sass', function () {
53 return gulp.src('./sass/**/*.scss')
54 .pipe(sass.sync().on('error', sass.logError))
55 .pipe(gulp.dest('./css'));
56});
57
58gulp.task('sass:watch', function () {
59 gulp.watch('./sass/**/*.scss', ['sass']);
60});
61```
62
63You can choose whether to use [Dart Sass][] or [Node Sass][] by setting the `sass.compiler` property. Node Sass will be used by default, but it's strongly recommended that you set it explicitly for forwards-compatibility in case the default ever changes.
64
65[Dart Sass]: http://sass-lang.com/dart-sass
66[Node Sass]: https://github.com/sass/node-sass
67
68Note that when using Dart Sass, **synchronous compilation is twice as fast as asynchronous compilation** by default, due to the overhead of asynchronous callbacks. To avoid this overhead, you can use the [`fibers`](https://www.npmjs.com/package/fibers) package to call asynchronous importers from the synchronous code path. To enable this, pass the `Fiber` class to the `fiber` option:
69
70```javascript
71'use strict';
72
73var Fiber = require('fibers');
74var gulp = require('gulp');
75var sass = require('gulp-sass');
76
77sass.compiler = require('sass');
78
79gulp.task('sass', function () {
80 return gulp.src('./sass/**/*.scss')
81 .pipe(sass({fiber: Fiber}).on('error', sass.logError))
82 .pipe(gulp.dest('./css'));
83});
84
85gulp.task('sass:watch', function () {
86 gulp.watch('./sass/**/*.scss', ['sass']);
87});
88```
89
90## Options
91
92Pass in options just like you would for [Node Sass](https://github.com/sass/node-sass#options); they will be passed along just as if you were using Node Sass. Except for the `data` option which is used by gulp-sass internally. Using the `file` option is also unsupported and results in undefined behaviour that may change without notice.
93
94For example:
95
96```javascript
97gulp.task('sass', function () {
98 return gulp.src('./sass/**/*.scss')
99 .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
100 .pipe(gulp.dest('./css'));
101});
102```
103
104Or this for synchronous code:
105
106```javascript
107gulp.task('sass', function () {
108 return gulp.src('./sass/**/*.scss')
109 .pipe(sass.sync({outputStyle: 'compressed'}).on('error', sass.logError))
110 .pipe(gulp.dest('./css'));
111});
112```
113
114## Source Maps
115
116`gulp-sass` can be used in tandem with [gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps) to generate source maps for the Sass to CSS compilation. You will need to initialize [gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps) prior to running `gulp-sass` and write the source maps after.
117
118```javascript
119var sourcemaps = require('gulp-sourcemaps');
120
121gulp.task('sass', function () {
122 return gulp.src('./sass/**/*.scss')
123 .pipe(sourcemaps.init())
124 .pipe(sass().on('error', sass.logError))
125 .pipe(sourcemaps.write())
126 .pipe(gulp.dest('./css'));
127});
128```
129
130By default, [gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps) writes the source maps inline in the compiled CSS files. To write them to a separate file, specify a path relative to the `gulp.dest()` destination in the `sourcemaps.write()` function.
131
132```javascript
133var sourcemaps = require('gulp-sourcemaps');
134gulp.task('sass', function () {
135 return gulp.src('./sass/**/*.scss')
136 .pipe(sourcemaps.init())
137 .pipe(sass().on('error', sass.logError))
138 .pipe(sourcemaps.write('./maps'))
139 .pipe(gulp.dest('./css'));
140});
141```
142
143# Issues
144
145`gulp-sass` is a very light-weight wrapper around either [Dart Sass][] or [Node Sass][] (which in turn is a Node binding for [LibSass][]). Because of this, the issue you're having likely isn't a `gulp-sass` issue, but an issue with one those projects or with [Sass][] as a whole.
146
147[LibSass]: https://sass-lang.com/libsass
148[Sass]: https://sass-lang.com
149
150If you have a feature request/question how Sass works/concerns on how your Sass gets compiled/errors in your compiling, it's likely a Dart Sass or LibSass issue and you should file your issue with one of those projects.
151
152If you're having problems with the options you're passing in, it's likely a Dart Sass or Node Sass issue and you should file your issue with one of those projects.
153
154We may, in the course of resolving issues, direct you to one of these other projects. If we do so, please follow up by searching that project's issue queue (both open and closed) for your problem and, if it doesn't exist, filing an issue with them.