UNPKG

8.59 kBMarkdownView Raw
1# gulp-sass ![npm package version](https://img.shields.io/npm/v/gulp-sass?label=npm%20version) [![Build Status](https://img.shields.io/github/workflow/status/dlmanning/gulp-sass/CI/master)](https://github.com/dlmanning/gulp-sass/actions?query=workflow%3ACI+branch%3Amaster) [![Join the chat at https://gitter.im/dlmanning/gulp-sass](https://img.shields.io/gitter/room/dlmanning/gulp-sass?color=%2346b091&label=chat&logo=gitter)](https://gitter.im/dlmanning/gulp-sass) ![Node.js support](https://img.shields.io/node/v/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 version of `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**Migrating your existing project to version 5? Please read our (short!) [migration guide](#migrating-to-version-5).**
8
9## Support
10
11Only [Active LTS and Current releases](https://github.com/nodejs/Release#release-schedule) are supported.
12
13## Installation
14
15To use `gulp-sass`, you must install both `gulp-sass` itself *and* a Sass compiler. `gulp-sass` supports both [Dart Sass][] and [Node Sass][], although Node Sass is [deprecated](https://sass-lang.com/blog/libsass-is-deprecated). We recommend that you use Dart Sass for new projects, and migrate Node Sass projects to Dart Sass when possible.
16
17Whichever compiler you choose, it's best to install these as dev dependencies:
18
19```sh
20npm install sass gulp-sass --save-dev
21```
22
23### Importing it into your project
24
25`gulp-sass` must be imported into your gulpfile, where you provide it the compiler of your choice. To use `gulp-sass` in a CommonJS module (which is most Node.js environments), do something like this:
26
27```js
28const sass = require('gulp-sass')(require('sass'));
29```
30
31To use `gulp-sass` in an ECMAScript module (which is supported in newer Node.js 14 and later), do something like this:
32
33```js
34import dartSass from 'sass';
35import gulpSass from 'gulp-sass';
36const sass = gulpSass(dartSass);
37```
38
39## Usage
40
41**Note:** These examples are written for CommonJS modules and assume you're using Gulp 4. For examples that work with Gulp 3, [check the docs for an earlier version of `gulp-sass`](https://github.com/dlmanning/gulp-sass/tree/v4.1.1).
42
43`gulp-sass` must be used in a Gulp task. Your task can call `sass()` (to asynchronously render your CSS), or `sass.sync()` (to synchronously render your CSS). Then, export your task with the `export` keyword. We'll show some examples of how to do that.
44
45**⚠️ Note:** When using Dart Sass, **synchronous rendering is twice as fast as asynchronous rendering**. The Sass team is exploring ways to improve asynchronous rendering with Dart Sass, but for now, you will get the best performance from `sass.sync()`. If performance is critical, you can use `node-sass` instead, but bear in mind that `node-sass` may not support modern Sass features you rely on.
46
47### Render your CSS
48
49To render your CSS with a build task, then watch your files for changes, you might write something like this:
50
51```js
52'use strict';
53
54const gulp = require('gulp');
55const sass = require('gulp-sass')(require('sass'));
56
57function buildStyles() {
58 return gulp.src('./sass/**/*.scss')
59 .pipe(sass().on('error', sass.logError))
60 .pipe(gulp.dest('./css'));
61};
62
63exports.buildStyles = buildStyles;
64exports.watch = function () {
65 gulp.watch('./sass/**/*.scss', ['sass']);
66};
67```
68
69With synchronous rendering, that Gulp task looks like this:
70
71```js
72function buildStyles() {
73 return gulp.src('./sass/**/*.scss')
74 .pipe(sass.sync().on('error', sass.logError))
75 .pipe(gulp.dest('./css'));
76};
77```
78
79### Render with options
80
81To change the final output of your CSS, you can pass an options object to your renderer. `gulp-sass` supports [Node Sass's render options](https://github.com/sass/node-sass#options), with two unsupported exceptions:
82
83- The `data` option, which is used by `gulp-sass` internally.
84- The `file` option, which has undefined behavior that may change without notice.
85
86For example, to compress your CSS, you can call `sass({outputStyle: 'compressed'}`. In the context of a Gulp task, that looks like this:
87
88```js
89function buildStyles() {
90 return gulp.src('./sass/**/*.scss')
91 .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
92 .pipe(gulp.dest('./css'));
93};
94
95exports.buildStyles = buildStyles;
96```
97
98Or this for synchronous rendering:
99
100```js
101function buildStyles() {
102 return gulp.src('./sass/**/*.scss')
103 .pipe(sass.sync({outputStyle: 'compressed'}).on('error', sass.logError))
104 .pipe(gulp.dest('./css'));
105};
106
107exports.buildStyles = buildStyles;
108```
109
110### Include a source map
111
112`gulp-sass` can be used in tandem with [`gulp-sourcemaps`](https://github.com/gulp-sourcemaps/gulp-sourcemaps) to generate source maps for the Sass-to-CSS compilation. You will need to initialize `gulp-sourcemaps` _before_ running `gulp-sass`, and write the source maps after.
113
114```js
115const sourcemaps = require('gulp-sourcemaps');
116
117function buildStyles() {
118 return gulp.src('./sass/**/*.scss')
119 .pipe(sourcemaps.init())
120 .pipe(sass().on('error', sass.logError))
121 .pipe(sourcemaps.write())
122 .pipe(gulp.dest('./css'));
123}
124
125exports.buildStyles = buildStyles;
126```
127
128By default, `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.
129
130```js
131const sourcemaps = require('gulp-sourcemaps');
132
133function buildStyles() {
134 return gulp.src('./sass/**/*.scss')
135 .pipe(sourcemaps.init())
136 .pipe(sass().on('error', sass.logError))
137 .pipe(sourcemaps.write('./maps'))
138 .pipe(gulp.dest('./css'));
139};
140
141exports.buildStyles = buildStyles;
142```
143
144<h2 id="migrating-to-version-5">Migrating to version 5</h2>
145
146`gulp-sass` version 5 requires Node.js 12 or later, and introduces some breaking changes. Additionally, changes in Node.js itself mean that Node fibers can no longer be used to speed up Dart Sass in Node.js 16.
147
148### Setting a Sass compiler
149
150As of version 5, `gulp-sass` _does not include a default Sass compiler_, so you must install one (either `node-sass` or `sass`) along with `gulp-sass`.
151
152```sh
153npm install sass gulp-sass --save-dev
154```
155
156Then, you must explicitly set that compiler in your gulpfille. Instead of setting a `compiler` prop on the `gulp-sass` instance, you pass the compiler into a function call when instantiating `gulp-sass`.
157
158These changes look something like this:
159
160```diff
161- const sass = require('gulp-sass'));
162- const compiler = require('sass');
163- sass.compiler = compiler;
164+ const sass = require('gulp-sass')(require('sass'));
165```
166
167If you're migrating an ECMAScript module, that'll look something like this:
168
169```diff
170import dartSass from 'sass';
171- import sass from 'gulp-sass';
172- sass.compiler = dartSass;
173
174import dartSass from 'sass';
175+ import gulpSass from 'gulp-sass';
176+ const sass = gulpSass(dartSass);
177```
178
179### What about fibers?
180
181We used to recommend Node fibers as a way to speed up asynchronous rendering with Dart Sass. Unfortunately, [Node fibers are discontinued](https://sass-lang.com/blog/node-fibers-discontinued) and will not work in Node.js 16. The Sass team is exploring its options for future performance improvements, but for now, you will get the best performance from `sass.sync()`.
182
183## Issues
184
185`gulp-sass` is a light-weight wrapper around either [Dart Sass][] or [Node Sass][] (which in turn is a Node.js 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.
186
187If you have a feature request/question about 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.
188
189If 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.
190
191We 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.
192
193[Dart Sass]: https://sass-lang.com/dart-sass
194[LibSass]: https://sass-lang.com/libsass
195[Node Sass]: https://github.com/sass/node-sass
196[Sass]: https://sass-lang.com