Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
The gulp-sass package is a Gulp plugin that allows you to compile Sass (Syntactically Awesome Stylesheets) files into CSS. It integrates seamlessly with Gulp, a toolkit for automating painful or time-consuming tasks in your development workflow.
Compile Sass to CSS
This feature allows you to compile Sass files into CSS. The code sample demonstrates a Gulp task that takes all .scss files from the src/scss directory, compiles them into CSS, and outputs them to the dist/css directory.
const gulp = require('gulp');
const sass = require('gulp-sass')(require('sass'));
gulp.task('sass', function() {
return gulp.src('./src/scss/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./dist/css'));
});
Watch for changes
This feature allows you to watch for changes in your Sass files and automatically compile them to CSS whenever a change is detected. The code sample demonstrates a Gulp task that watches the src/scss directory for changes and runs the 'sass' task whenever a change is detected.
const gulp = require('gulp');
const sass = require('gulp-sass')(require('sass'));
gulp.task('sass', function() {
return gulp.src('./src/scss/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./dist/css'));
});
gulp.task('watch', function() {
gulp.watch('./src/scss/**/*.scss', gulp.series('sass'));
});
Source Maps
This feature allows you to generate source maps for your compiled CSS files, which can be useful for debugging. The code sample demonstrates a Gulp task that initializes source maps, compiles Sass to CSS, writes the source maps to a maps directory, and outputs the CSS to the dist/css directory.
const gulp = require('gulp');
const sass = require('gulp-sass')(require('sass'));
const sourcemaps = require('gulp-sourcemaps');
gulp.task('sass', function() {
return gulp.src('./src/scss/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.write('./maps'))
.pipe(gulp.dest('./dist/css'));
});
node-sass is a library that provides binding for Node.js to LibSass, the C version of the popular stylesheet preprocessor, Sass. It can be used independently or integrated into build tools like Gulp or Grunt. Compared to gulp-sass, node-sass is more low-level and requires additional setup to integrate with Gulp.
sass is the official Dart implementation of Sass, which is the primary implementation used by the Sass team. It can be used with Node.js and is often preferred for its speed and compatibility with the latest Sass features. Like node-sass, it can be integrated into Gulp but requires additional configuration.
gulp-less is a Gulp plugin for compiling Less files to CSS. While it serves a similar purpose to gulp-sass, it is specifically designed for Less, another CSS preprocessor. It offers similar functionality but is not compatible with Sass files.
Sass plugin for Gulp.
Before filing an issue, please make sure you have updated to the latest version of gulp-sass
and have gone through our Common Issues and Their Fixes section.
Migrating your existing project to version 5 or 6? Please read our (short!) migration guides.
Only Active LTS and Current releases are supported.
To use gulp-sass
, you must install both gulp-sass
itself and a Sass compiler. gulp-sass
supports both Embedded Sass, Dart Sass and Node Sass, although Node Sass is deprecated. We recommend that you use Dart Sass for new projects, and migrate Node Sass projects to Dart Sass or Embedded Sass when possible.
Whichever compiler you choose, it's best to install these as dev dependencies:
npm install sass gulp-sass --save-dev
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:
const sass = require('gulp-sass')(require('sass'));
To use gulp-sass
in an ECMAScript module (which is supported in newer Node.js 14 and later), do something like this:
import dartSass from 'sass';
import gulpSass from 'gulp-sass';
const sass = gulpSass(dartSass);
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
.
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.
⚠️ 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 sass-embedded
instead.
To render your CSS with a build task, then watch your files for changes, you might write something like this:
'use strict';
const gulp = require('gulp');
const sass = require('gulp-sass')(require('sass'));
function buildStyles() {
return gulp.src('./sass/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./css'));
};
exports.buildStyles = buildStyles;
exports.watch = function () {
gulp.watch('./sass/**/*.scss', buildStyles);
};
With synchronous rendering, that Gulp task looks like this:
function buildStyles() {
return gulp.src('./sass/**/*.scss')
.pipe(sass.sync().on('error', sass.logError))
.pipe(gulp.dest('./css'));
};
To change the final output of your CSS, you can pass an options object to your renderer. gulp-sass
supports Sass's JS API compile options, with a few usage notes:
syntax
option is set to indented
automatically for files with the .sass
extensionsourceMap
and sourceMapIncludeSources
options are set for you when using gulp-sourcemaps
For example, to compress your CSS, you can call sass({style: 'compressed'}
. In the context of a Gulp task, that looks like this:
function buildStyles() {
return gulp.src('./sass/**/*.scss')
.pipe(sass({style: 'compressed'}).on('error', sass.logError))
.pipe(gulp.dest('./css'));
};
exports.buildStyles = buildStyles;
Or this for synchronous rendering:
function buildStyles() {
return gulp.src('./sass/**/*.scss')
.pipe(sass.sync({style: 'compressed'}).on('error', sass.logError))
.pipe(gulp.dest('./css'));
};
exports.buildStyles = buildStyles;
gulp-sass
can be used in tandem with 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.
const 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;
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.
const 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;
gulp-sass
version 6 uses the new compile function internally by default. If you use any options, for instance custom importers, please compare the new options with the legacy options in order to migrate. For instance, the outputStyle
option is now called style
.
function buildStyles() {
return gulp.src('./sass/**/*.scss')
- .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
+ .pipe(sass({style: 'compressed'}).on('error', sass.logError))
.pipe(gulp.dest('./css'));
};
If you want to keep using the legacy API while it's available, you can.
const sass = require('gulp-sass/legacy')(require('sass'));
If you use source maps, you may see the result change somewhat. The result will typically be absolute file:
URLs, rather than relative ones. The result may also be the source itself, URL encoded. You can optionally add custom importers to adjust the source maps according to your own needs.
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.
As of version 5, gulp-sass
does not include a default Sass compiler, so you must install one (either sass
, sass-embedded
, or node-sass
) along with gulp-sass
.
npm install sass gulp-sass --save-dev
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
.
These changes look something like this:
- const sass = require('gulp-sass'));
- const compiler = require('sass');
- sass.compiler = compiler;
+ const sass = require('gulp-sass')(require('sass'));
If you're migrating an ECMAScript module, that'll look something like this:
import dartSass from 'sass';
- import sass from 'gulp-sass';
- sass.compiler = dartSass;
import dartSass from 'sass';
+ import gulpSass from 'gulp-sass';
+ const sass = gulpSass(dartSass);
If you need to use the deprecated render
Sass API, gulp-sass
still includes legacy support.
'use strict';
const gulp = require('gulp');
const sass = require('gulp-sass/legacy')(require('sass'));
function buildStyles() {
return gulp.src('./sass/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./css'));
};
exports.buildStyles = buildStyles;
exports.watch = function () {
gulp.watch('./sass/**/*.scss', buildStyles);
};
We used to recommend Node fibers as a way to speed up asynchronous rendering with Dart Sass. Unfortunately, Node fibers are 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()
.
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.
If 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.
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.
FAQs
Gulp plugin for sass
We found that gulp-sass demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.