@mr-hope/gulp-sass

Gulp plugin for sass

Usage no npm install needed!

<script type="module">
  import mrHopeGulpSass from 'https://cdn.skypack.dev/@mr-hope/gulp-sass';
</script>

README

@mr-hope/gulp-sass

CodeQL Test and coverage codecov

Sass plugin for gulp.

Node Support

Only Active LTS and Current releases are supported.

Install

yarn add -D @mr-hope/gulp-sass

Basic Usage

Something like this will compile your Sass files:

const { dest, src, watch } = require("gulp");
const { sass } = require("@mr-hope/gulp-sass");

const build = src("./styles/**/*.scss")
  .pipe(sass().on("error", sass.logError))
  .pipe(dest("./css"));

exports.build = build;
exports.watch = watch("./styles/**/*.scss", build);

You can also compile synchronously, doing something like this:

const { dest, src, watch } = require("gulp");
const { sassSync } = require("@mr-hope/gulp-sass");

const build = src("./styles/**/*.scss")
  .pipe(sassSync().on("error", sassSync.logError))
  .pipe(dest("./css"));

exports.build = build;
exports.watch = watch("./styles/**/*.scss", build);

Note that 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 package to call asynchronous importers from the synchronous code path. To enable this, pass the Fiber class to the fiber option:

const { dest, src, watch } = require("gulp");
const { sass } = require("@mr-hope/gulp-sass");
const fiber = require("fibers");

const build = src("./styles/**/*.scss")
  .pipe(sass({ fiber }).on("error", sass.logError))
  .pipe(dest("./css"));

exports.build = build;
exports.watch = watch("./styles/**/*.scss", build);

Options

Pass in options just like you would for Dart Sass; they will be passed along just as if you were using sass. Except for the data option which is used by @mr-hope/gulp-sass internally. Using the file option is also unsupported and results in undefined behaviour that may change without notice.

For example:

exports.build = src("./styles/**/*.scss")
  .pipe(sass({ outputStyle: "compressed" }).on("error", sass.logError))
  .pipe(dest("./css"));

Or this for synchronous code:

exports.build = src("./styles/**/*.scss")
  .pipe(sassSync({ outputStyle: "compressed" }).on("error", sassSync.logError))
  .pipe(dest("./css"));

Source Maps

@mr-hope/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 prior to running @mr-hope/gulp-sass and write the source maps after.

const sourcemaps = require("gulp-sourcemaps");

exports.build = src("./styles/**/*.scss")
  .pipe(sourcemaps.init())
  .pipe(sass({ outputStyle: "compressed" }).on("error", sass.logError))
  .pipe(sourcemaps.write())
  .pipe(dest("./css"));

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");

exports.build = src("./styles/**/*.scss")
  .pipe(sourcemaps.init())
  .pipe(sass({ outputStyle: "compressed" }).on("error", sass.logError))
  .pipe(sourcemaps.write("./maps"))
  .pipe(dest("./css"));

Issues

@mr-hope/gulp-sass is a very light-weight wrapper around Dart Sass. Because of this, the issue you're having likely isn't a @mr-hope/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 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 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.