Skip to content

Commit

Permalink
Make this package implementation-agnostic
Browse files Browse the repository at this point in the history
Rather than always loading Node Sass, this now requires users to pass
in either Dart Sass or Node Sass to the require() call.

Closes #672
  • Loading branch information
nex3 authored and xzyfer committed Oct 16, 2018
1 parent 643f73b commit 7cc2db1
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 140 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Gulp Sass Changelog

## v5.0.0

https://github.com/dlmanning/gulp-sass/releases/tag/v5.0.0

## v3.2.0
**March 11, 2018**

Expand Down
9 changes: 7 additions & 2 deletions CONTRIBUTING.md
@@ -1,10 +1,15 @@
# Contributing to Gulp Sass

Gulp Sass is a very light-weight [Gulp](https://github.com/gulpjs/gulp) wrapper for [`node-sass`](https://github.com/sass/node-sass), which in turn is a Node binding for [`libsass`](https://github.com/sass/libsass), which in turn is a port of [`Sass`](https://github.com/sass/sass).
`gulp-sass` is a very light-weight wrapper around either [Dart Sass][] or [Node Sass][] (which in turn is a Node binding for [LibSass][]. All of these are implementations of the [Sass][] language.

[Dart Sass]: http://sass-lang.com/dart-sass
[Node Sass]: https://github.com/sass/node-sass
[LibSass]: https://sass-lang.com/libsass
[Sass]: https://sass-lang.com

## Submitting Issues

* Before creating a new issue, perform a [cursory search](https://github.com/issues?utf8=%E2%9C%93&q=repo%3Adlmanning%2Fgulp-sass+repo%3Asass%2Fnode-sass+repo%3Asass%2Flibsass+repo%3Asass%2Fsass+repo%3Asass-eyeglass%2Feyeglass) in the Gulp Sass, Node Sass, Libsass, and main Sass repos to see if a similar issue has already been submitted. Please also refer to our [Common Issues and Their Fixes](https://github.com/dlmanning/gulp-sass/wiki/Common-Issues-and-Their-Fixes) page for some basic troubleshooting.
* Before creating a new issue, perform a [cursory search](https://github.com/issues?utf8=%E2%9C%93&q=repo%3Adlmanning%2Fgulp-sass+repo%3Asass%2Fdart-sass+repo%3Asass%2Fnode-sass+repo%3Asass%2Flibsass+repo%3Asass%2Fsass+repo%3Asass-eyeglass%2Feyeglass) in the Gulp Sass, Dart Sass, Node Sass, Libsass, and main Sass repos to see if a similar issue has already been submitted. Please also refer to our [Common Issues and Their Fixes](https://github.com/dlmanning/gulp-sass/wiki/Common-Issues-and-Their-Fixes) page for some basic troubleshooting.
* You can create an issue [here](https://github.com/dlmanning/gulp-sass/issues). Please include as many details as possible in your report.
* Issue titles should be descriptive, explaining at the high level what it is about.
* Please include the version of `gulp-sass`, Node, and NPM you are using, as well as what operating system you are having a problem on.
Expand Down
22 changes: 15 additions & 7 deletions README.md
Expand Up @@ -13,7 +13,7 @@ Only [Active LTS and Current releases][1] are supported.
# Install

```
npm install gulp-sass --save-dev
npm install sass gulp-sass --save-dev
```

# Basic Usage
Expand All @@ -24,7 +24,7 @@ Something like this will compile your Sass files:
'use strict';

var gulp = require('gulp');
var sass = require('gulp-sass');
var sass = require('gulp-sass')(require('sass'));

gulp.task('sass', function () {
return gulp.src('./sass/**/*.scss')
Expand All @@ -43,7 +43,7 @@ You can also compile synchronously, doing something like this:
'use strict';

var gulp = require('gulp');
var sass = require('gulp-sass');
var sass = require('gulp-sass')(require('sass'));

gulp.task('sass', function () {
return gulp.src('./sass/**/*.scss')
Expand All @@ -58,7 +58,12 @@ gulp.task('sass:watch', function () {

## 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.
`gulp-sass` supports both [Dart Sass][] and [Node Sass][]. You choose which one to use by writing either `require('gulp-sass')(require('sass'))` for Dart Sass or `require('gulp-sass')(require('node-sass'))` for Node Sass. One or the other must be passed in.

[Dart Sass]: http://sass-lang.com/dart-sass
[Node Sass]: https://github.com/sass/node-sass

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.

For example:

Expand Down Expand Up @@ -111,10 +116,13 @@ gulp.task('sass', function () {

# Issues

`gulp-sass` is a very light-weight wrapper around [`node-sass`](https://github.com/sass/node-sass), which in turn is a Node binding for [`libsass`](https://github.com/sass/libsass), which in turn is a port of [`Sass`](https://github.com/sass/sass). Because of this, the issue you're having likely isn't a `gulp-sass` issue, but an issue with one of those three projects.
`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.

[LibSass]: https://sass-lang.com/libsass
[Sass]: https://sass-lang.com

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 `libsass` or `Sass` issue and you should file your issue with one of those projects.
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 `node-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.
261 changes: 133 additions & 128 deletions index.js
Expand Up @@ -9,156 +9,161 @@ const applySourceMap = require('vinyl-sourcemaps-apply');

const PLUGIN_NAME = 'gulp-sass';

//////////////////////////////
// Main Gulp Sass function
//////////////////////////////
const gulpSass = (options, sync) => through.obj((file, enc, cb) => { // eslint-disable-line consistent-return
if (file.isNull()) {
return cb(null, file);
module.exports = (compiler) => {
if (!compiler || !compiler.render) {
throw new PluginError(
PLUGIN_NAME,
'gulp-sass 5 requires you to pass in a Sass implementation. For example:\n\n' +
' var sass = require(\'gulp-sass\')(require(\'node-sass\'));\n',
);
}

if (file.isStream()) {
return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
//////////////////////////////
// Main Gulp Sass function
//////////////////////////////
const gulpSass = (options, sync) => through.obj((file, enc, cb) => { // eslint-disable-line consistent-return
if (file.isNull()) {
return cb(null, file);
}

if (path.basename(file.path).indexOf('_') === 0) {
return cb();
}
if (file.isStream()) {
return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
}

if (!file.contents.length) {
file.path = replaceExtension(file.path, '.css'); // eslint-disable-line no-param-reassign
return cb(null, file);
}
if (path.basename(file.path).indexOf('_') === 0) {
return cb();
}

const opts = clonedeep(options || {});
opts.data = file.contents.toString();
if (!file.contents.length) {
file.path = replaceExtension(file.path, '.css'); // eslint-disable-line no-param-reassign
return cb(null, file);
}

// we set the file path here so that libsass can correctly resolve import paths
opts.file = file.path;
const opts = clonedeep(options || {});
opts.data = file.contents.toString();

// Ensure `indentedSyntax` is true if a `.sass` file
if (path.extname(file.path) === '.sass') {
opts.indentedSyntax = true;
}
// we set the file path here so that libsass can correctly resolve import paths
opts.file = file.path;

// Ensure file's parent directory in the include path
if (opts.includePaths) {
if (typeof opts.includePaths === 'string') {
opts.includePaths = [opts.includePaths];
// Ensure `indentedSyntax` is true if a `.sass` file
if (path.extname(file.path) === '.sass') {
opts.indentedSyntax = true;
}
} else {
opts.includePaths = [];
}

opts.includePaths.unshift(path.dirname(file.path));

// Generate Source Maps if plugin source-map present
if (file.sourceMap) {
opts.sourceMap = file.path;
opts.omitSourceMapUrl = true;
opts.sourceMapContents = true;
}

//////////////////////////////
// Handles returning the file to the stream
//////////////////////////////
const filePush = (sassObj) => {
let sassMap;
let sassMapFile;
let sassFileSrc;
let sassFileSrcPath;
let sourceFileIndex;

// Build Source Maps!
if (sassObj.map) {
// Transform map into JSON
sassMap = JSON.parse(sassObj.map.toString());
// Grab the stdout and transform it into stdin
sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin');
// Grab the base file name that's being worked on
sassFileSrc = file.relative;
// Grab the path portion of the file that's being worked on
sassFileSrcPath = path.dirname(sassFileSrc);
if (sassFileSrcPath) {
// Prepend the path to all files in the sources array except the file that's being worked on
sourceFileIndex = sassMap.sources.indexOf(sassMapFile);
sassMap.sources = sassMap.sources.map((source, index) => { // eslint-disable-line arrow-body-style
return index === sourceFileIndex ? source : path.join(sassFileSrcPath, source);
});
// Ensure file's parent directory in the include path
if (opts.includePaths) {
if (typeof opts.includePaths === 'string') {
opts.includePaths = [opts.includePaths];
}

// Remove 'stdin' from souces and replace with filenames!
sassMap.sources = sassMap.sources.filter(src => src !== 'stdin' && src);

// Replace the map file with the original file name (but new extension)
sassMap.file = replaceExtension(sassFileSrc, '.css');
// Apply the map
applySourceMap(file, sassMap);
} else {
opts.includePaths = [];
}

file.contents = sassObj.css; // eslint-disable-line no-param-reassign
file.path = replaceExtension(file.path, '.css'); // eslint-disable-line no-param-reassign

cb(null, file);
};

//////////////////////////////
// Handles error message
//////////////////////////////
const errorM = (error) => {
const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path;
const relativePath = path.relative(process.cwd(), filePath);
const message = [chalk.underline(relativePath), error.formatted].join('\n');

error.messageFormatted = message; // eslint-disable-line no-param-reassign
error.messageOriginal = error.message; // eslint-disable-line no-param-reassign
error.message = stripAnsi(message); // eslint-disable-line no-param-reassign
error.relativePath = relativePath; // eslint-disable-line no-param-reassign
opts.includePaths.unshift(path.dirname(file.path));

return cb(new PluginError(PLUGIN_NAME, error));
};
// Generate Source Maps if plugin source-map present
if (file.sourceMap) {
opts.sourceMap = file.path;
opts.omitSourceMapUrl = true;
opts.sourceMapContents = true;
}

if (sync !== true) {
//////////////////////////////
// Async Sass render
// Handles returning the file to the stream
//////////////////////////////
const callback = (error, obj) => { // eslint-disable-line consistent-return
if (error) {
return errorM(error);
const filePush = (sassObj) => {
let sassMap;
let sassMapFile;
let sassFileSrc;
let sassFileSrcPath;
let sourceFileIndex;

// Build Source Maps!
if (sassObj.map) {
// Transform map into JSON
sassMap = JSON.parse(sassObj.map.toString());
// Grab the stdout and transform it into stdin
sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin');
// Grab the base file name that's being worked on
sassFileSrc = file.relative;
// Grab the path portion of the file that's being worked on
sassFileSrcPath = path.dirname(sassFileSrc);
if (sassFileSrcPath) {
// Prepend the path to all files in the sources array except the file that's being worked on
sourceFileIndex = sassMap.sources.indexOf(sassMapFile);
sassMap.sources = sassMap.sources.map((source, index) => { // eslint-disable-line arrow-body-style
return index === sourceFileIndex ? source : path.join(sassFileSrcPath, source);
});
}

// Remove 'stdin' from souces and replace with filenames!
sassMap.sources = sassMap.sources.filter(src => src !== 'stdin' && src);

// Replace the map file with the original file name (but new extension)
sassMap.file = replaceExtension(sassFileSrc, '.css');
// Apply the map
applySourceMap(file, sassMap);
}
filePush(obj);

file.contents = sassObj.css; // eslint-disable-line no-param-reassign
file.path = replaceExtension(file.path, '.css'); // eslint-disable-line no-param-reassign

cb(null, file);
};

gulpSass.compiler.render(opts, callback);
} else {
//////////////////////////////
// Sync Sass render
// Handles error message
//////////////////////////////
try {
filePush(gulpSass.compiler.renderSync(opts));
} catch (error) {
return errorM(error);
const errorM = (error) => {
const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path;
const relativePath = path.relative(process.cwd(), filePath);
const message = [chalk.underline(relativePath), error.formatted].join('\n');

error.messageFormatted = message; // eslint-disable-line no-param-reassign
error.messageOriginal = error.message; // eslint-disable-line no-param-reassign
error.message = stripAnsi(message); // eslint-disable-line no-param-reassign
error.relativePath = relativePath; // eslint-disable-line no-param-reassign

return cb(new PluginError(PLUGIN_NAME, error));
};

if (sync !== true) {
//////////////////////////////
// Async Sass render
//////////////////////////////
const callback = (error, obj) => { // eslint-disable-line consistent-return
if (error) {
return errorM(error);
}
filePush(obj);
};

compiler.render(opts, callback);
} else {
//////////////////////////////
// Sync Sass render
//////////////////////////////
try {
filePush(compiler.renderSync(opts));
} catch (error) {
return errorM(error);
}
}
}
});

//////////////////////////////
// Sync Sass render
//////////////////////////////
gulpSass.sync = options => gulpSass(options, true);

//////////////////////////////
// Log errors nicely
//////////////////////////////
gulpSass.logError = function logError(error) {
const message = new PluginError('sass', error.messageFormatted).toString();
process.stderr.write(`${message}\n`);
this.emit('end');
};
});

//////////////////////////////
// Store compiler in a prop
//////////////////////////////
gulpSass.compiler = require('node-sass');
//////////////////////////////
// Sync Sass render
//////////////////////////////
gulpSass.sync = options => gulpSass(options, true);

module.exports = gulpSass;
//////////////////////////////
// Log errors nicely
//////////////////////////////
gulpSass.logError = function logError(error) {
const message = new PluginError('sass', error.messageFormatted).toString();
process.stderr.write(`${message}\n`);
this.emit('end');
};

return gulpSass;
};
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "gulp-sass",
"version": "4.0.1",
"version": "5.0.0",
"description": "Gulp plugin for sass",
"main": "index.js",
"engines": {
Expand All @@ -26,7 +26,6 @@
"dependencies": {
"chalk": "^2.3.0",
"lodash.clonedeep": "^4.3.2",
"node-sass": "^4.8.3",
"plugin-error": "^1.0.1",
"replace-ext": "^1.0.0",
"strip-ansi": "^4.0.0",
Expand All @@ -44,6 +43,7 @@
"gulp-sourcemaps": "^2.6.4",
"gulp-tap": "^0.1.3",
"mocha": "^5.0.4",
"node-sass": "^4.8.3",
"rimraf": "^2.4.3",
"should": "^13.2.1",
"vinyl": "^2.1.0"
Expand Down

0 comments on commit 7cc2db1

Please sign in to comment.