-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update node-sass to 6.0.0 Also moves node-sass to devDependencies and peerDependencies. This helps prevent dupes and conflicts in packages using gulp-sass * Make mocha test compatible with gulp@4 * Update package version, engines * Remove node-sass as peer dependency * Replace through2 with transfob * Uninstall through2 * Throw error if compiler not set * Fix casing of Sass Co-authored-by: Michael Mifsud <[email protected]> * Use function arg to set compiler, use transfob package * Update test to new API signature * Update package versions * Fix eslint errors * Incorporate previously proposed documentation changes Copied from #694 * Update document outline, apply copyedits * Copyedit: overall clarifications * Call out migration guide, update badges * Document Gulp 4 usage * smol clarification * Copyedit some headings * Yet Another Copyedit™ * Update README.md Co-authored-by: Michael Mifsud <[email protected]>
- Loading branch information
Showing
5 changed files
with
194 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,154 +1,170 @@ | ||
# 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) | ||
# gulp-sass ![Build status](https://img.shields.io/travis/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) ![Node support](https://img.shields.io/node/v/gulp-sass) ![NPM package version](https://img.shields.io/npm/v/gulp-sass?label=npm%20version) | ||
|
||
Sass plugin for [Gulp](https://github.com/gulpjs/gulp). | ||
|
||
**_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._** | ||
**_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._** | ||
|
||
# Support | ||
**Migrating your existing project to version 5? Please read our (short!) [migration guide](#migrating-to-version-5).** | ||
|
||
## Support | ||
|
||
Only [Active LTS and Current releases][1] are supported. | ||
|
||
[1]: https://github.com/nodejs/Release#release-schedule | ||
|
||
# Install | ||
## Installation | ||
|
||
To use `gulp-sass`, you must install both `gulp-sass` itself *and* a Sass compiler. `gulp-sass` supports both [Dart Sass][] and [Node Sass][], but 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. | ||
|
||
Whichever compiler you choose, it's best to install these as dev dependencies: | ||
|
||
``` | ||
npm install node-sass gulp-sass --save-dev | ||
npm install sass gulp-sass --save-dev | ||
``` | ||
|
||
# Basic Usage | ||
## Usage | ||
|
||
**Note:** These examples 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). | ||
|
||
`gulp-sass` runs inside of Gulp tasks. No matter what else you do with `gulp-sass`, you must first import it into your gulpfile, making sure to pass it the compiler of your choice. From there, create a Gulp task that calls either `sass()` (to asynchronously render your CSS), or `sass.sync()` (to render it synchronously). Then, export your task with the `export` keyword. We'll show some examples of how to do that. | ||
|
||
**⚠️ Note:** With 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()` | ||
|
||
### Render your CSS | ||
|
||
Something like this will compile your Sass files: | ||
To render your CSS with a build task, then watch your files for changes, you might write something like this.: | ||
|
||
```javascript | ||
'use strict'; | ||
|
||
var gulp = require('gulp'); | ||
var sass = require('gulp-sass'); | ||
var sass = require('gulp-sass')(require('sass')); | ||
|
||
sass.compiler = require('node-sass'); | ||
|
||
gulp.task('sass', function () { | ||
function buildStyles() { | ||
return gulp.src('./sass/**/*.scss') | ||
.pipe(sass().on('error', sass.logError)) | ||
.pipe(gulp.dest('./css')); | ||
}); | ||
}; | ||
|
||
gulp.task('sass:watch', function () { | ||
exports.buildStyles = buildStyles; | ||
exports.watch = function () { | ||
gulp.watch('./sass/**/*.scss', ['sass']); | ||
}); | ||
}; | ||
``` | ||
|
||
You can also compile synchronously, doing something like this: | ||
|
||
```javascript | ||
'use strict'; | ||
|
||
var gulp = require('gulp'); | ||
var sass = require('gulp-sass'); | ||
|
||
sass.compiler = require('node-sass'); | ||
With synchronous rendering, that Gulp task looks like this: | ||
|
||
gulp.task('sass', function () { | ||
``` javascript | ||
function buildStyles() { | ||
return gulp.src('./sass/**/*.scss') | ||
.pipe(sass.sync().on('error', sass.logError)) | ||
.pipe(gulp.dest('./css')); | ||
}); | ||
|
||
gulp.task('sass:watch', function () { | ||
gulp.watch('./sass/**/*.scss', ['sass']); | ||
}); | ||
}; | ||
``` | ||
|
||
You 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. | ||
### Render with options | ||
|
||
[Dart Sass]: http://sass-lang.com/dart-sass | ||
[Node Sass]: https://github.com/sass/node-sass | ||
To 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: | ||
|
||
Note 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: | ||
- The `data` option, which is used by `gulp-sass` internally. | ||
- The `file` option, which has undefined behavior that may change without notice. | ||
|
||
For example, to compress your CSS, you can call `sass({outputStyle: 'compressed'}`. In the context of a Gulp task, that looks like this: | ||
|
||
```javascript | ||
'use strict'; | ||
function buildStyles() { | ||
return gulp.src('./sass/**/*.scss') | ||
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError)) | ||
.pipe(gulp.dest('./css')); | ||
}; | ||
|
||
var Fiber = require('fibers'); | ||
var gulp = require('gulp'); | ||
var sass = require('gulp-sass'); | ||
exports.buildStyles = buildStyles; | ||
``` | ||
|
||
sass.compiler = require('sass'); | ||
Or this for synchronous rendering: | ||
|
||
gulp.task('sass', function () { | ||
```javascript | ||
function buildStyles() { | ||
return gulp.src('./sass/**/*.scss') | ||
.pipe(sass({fiber: Fiber}).on('error', sass.logError)) | ||
.pipe(sass.sync({outputStyle: 'compressed'}).on('error', sass.logError)) | ||
.pipe(gulp.dest('./css')); | ||
}); | ||
}; | ||
|
||
gulp.task('sass:watch', function () { | ||
gulp.watch('./sass/**/*.scss', ['sass']); | ||
}); | ||
exports.buildStyles = buildStyles; | ||
``` | ||
|
||
## Options | ||
|
||
Pass 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. | ||
### Include a source map | ||
|
||
For example: | ||
`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` _before_ running `gulp-sass`, and write the source maps after. | ||
|
||
```javascript | ||
gulp.task('sass', function () { | ||
return gulp.src('./sass/**/*.scss') | ||
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError)) | ||
.pipe(gulp.dest('./css')); | ||
}); | ||
var sourcemaps = require('gulp-sourcemaps'); | ||
|
||
function buildStyles() { | ||
return gulp.src('./sass/**/*.scss') | ||
.pipe(sourcemaps.init()) | ||
.pipe(sass().on('error', sass.logError)) | ||
.pipe(sourcemaps.write()) | ||
.pipe(gulp.dest('./css')); | ||
} | ||
|
||
exports.buildStyles = buildStyles; | ||
``` | ||
|
||
Or this for synchronous code: | ||
By 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. | ||
|
||
```javascript | ||
gulp.task('sass', function () { | ||
return gulp.src('./sass/**/*.scss') | ||
.pipe(sass.sync({outputStyle: 'compressed'}).on('error', sass.logError)) | ||
.pipe(gulp.dest('./css')); | ||
}); | ||
var sourcemaps = require('gulp-sourcemaps'); | ||
|
||
function buildStyles() { | ||
return gulp.src('./sass/**/*.scss') | ||
.pipe(sourcemaps.init()) | ||
.pipe(sass().on('error', sass.logError)) | ||
.pipe(sourcemaps.write('./maps')) | ||
.pipe(gulp.dest('./css')); | ||
}; | ||
|
||
exports.buildStyles = buildStyles; | ||
``` | ||
|
||
## Source Maps | ||
<h2 id="migrating-to-version-5">Migrating to version 5</h2> | ||
|
||
`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. | ||
`gulp-sass` version 5 requires Node 12 or later, and introduces some breaking changes. Additionally, changes in Node itself mean that we should no longer use Node fibers to speed up asynchronous rendering with Dart Sass. | ||
|
||
```javascript | ||
var sourcemaps = require('gulp-sourcemaps'); | ||
### Setting a Sass compiler | ||
|
||
gulp.task('sass', function () { | ||
return gulp.src('./sass/**/*.scss') | ||
.pipe(sourcemaps.init()) | ||
.pipe(sass().on('error', sass.logError)) | ||
.pipe(sourcemaps.write()) | ||
.pipe(gulp.dest('./css')); | ||
}); | ||
As 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`. | ||
|
||
``` | ||
npm install sass gulp-sass --save-dev | ||
``` | ||
|
||
By 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. | ||
Then, 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`. | ||
|
||
```javascript | ||
var sourcemaps = require('gulp-sourcemaps'); | ||
gulp.task('sass', function () { | ||
return gulp.src('./sass/**/*.scss') | ||
.pipe(sourcemaps.init()) | ||
.pipe(sass().on('error', sass.logError)) | ||
.pipe(sourcemaps.write('./maps')) | ||
.pipe(gulp.dest('./css')); | ||
}); | ||
These changes look something like this: | ||
|
||
``` diff | ||
- var sass = require('gulp-sass')); | ||
- var compiler = require('sass'); | ||
- sass.compiler = compiler; | ||
+ var sass = require('gulp-sass')(require('sass')); | ||
``` | ||
|
||
# Issues | ||
### What about fibers? | ||
|
||
`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. | ||
We 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). The Sass team is exploring its optons for future performance improvements, but for now you will get the best performance from `sass.sync()`. | ||
|
||
[LibSass]: https://sass-lang.com/libsass | ||
[Sass]: https://sass-lang.com | ||
## Issues | ||
|
||
`gulp-sass` is a 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. | ||
|
||
If 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. | ||
|
||
If 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. | ||
|
||
We 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. | ||
|
||
[Dart Sass]: http://sass-lang.com/dart-sass | ||
[LibSass]: https://sass-lang.com/libsass | ||
[Node Sass]: https://github.com/sass/node-sass | ||
[Sass]: https://sass-lang.com |
Oops, something went wrong.