Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to major version 5 #802

Merged
merged 22 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ const chalk = require('chalk');
const PluginError = require('plugin-error');
const replaceExtension = require('replace-ext');
const stripAnsi = require('strip-ansi');
const through = require('through2');
const transfob = require('transfob');
const clonedeep = require('lodash/cloneDeep');
const path = require('path');
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
/// ///////////////////////////
const gulpSass = (options, sync) => transfob((file, enc, cb) => { // eslint-disable-line consistent-return
xzyfer marked this conversation as resolved.
Show resolved Hide resolved
if (file.isNull()) {
return cb(null, file);
}
Expand Down Expand Up @@ -59,9 +59,9 @@ const gulpSass = (options, sync) => through.obj((file, enc, cb) => { // eslint-d
opts.sourceMapContents = true;
}

//////////////////////////////
/// ///////////////////////////
// Handles returning the file to the stream
//////////////////////////////
/// ///////////////////////////
const filePush = (sassObj) => {
let sassMap;
let sassMapFile;
Expand All @@ -88,7 +88,7 @@ const gulpSass = (options, sync) => through.obj((file, enc, cb) => { // eslint-d
}

// Remove 'stdin' from souces and replace with filenames!
sassMap.sources = sassMap.sources.filter(src => src !== 'stdin' && src);
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');
Expand All @@ -106,9 +106,9 @@ const gulpSass = (options, sync) => through.obj((file, enc, cb) => { // eslint-d
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);
Expand All @@ -123,9 +123,9 @@ const gulpSass = (options, sync) => through.obj((file, enc, cb) => { // eslint-d
};

if (sync !== true) {
//////////////////////////////
/// ///////////////////////////
// Async Sass render
//////////////////////////////
/// ///////////////////////////
const callback = (error, obj) => { // eslint-disable-line consistent-return
if (error) {
return errorM(error);
Expand All @@ -135,9 +135,9 @@ const gulpSass = (options, sync) => through.obj((file, enc, cb) => { // eslint-d

gulpSass.compiler.render(opts, callback);
} else {
//////////////////////////////
/// ///////////////////////////
// Sync Sass render
//////////////////////////////
/// ///////////////////////////
try {
filePush(gulpSass.compiler.renderSync(opts));
} catch (error) {
Expand All @@ -146,23 +146,35 @@ const gulpSass = (options, sync) => through.obj((file, enc, cb) => { // eslint-d
}
});

//////////////////////////////
/// ///////////////////////////
// Sync Sass render
//////////////////////////////
gulpSass.sync = options => gulpSass(options, true);
/// ///////////////////////////
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');

module.exports = gulpSass;
module.exports = (compiler) => {
if (!compiler || !compiler.render) {
const message = new PluginError(
PLUGIN_NAME,
'\n'
+ 'gulp-sass 5 does not have a default Sass compiler; please set one yourself.\n'
+ 'Both the `sass` and `node-sass` packages are permitted.\n'

+ 'For example, in your gulpfile:\n\n'
+ ' var sass = require(\'gulp-sass\')(require(\'sass\'));\n',
{ showProperties: false },
).toString();
process.stderr.write(`${message}\n`);
process.exit(1);
}
gulpSass.compiler = compiler;
return gulpSass;
};
45 changes: 23 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "gulp-sass",
"version": "4.1.1",
"version": "5.0.0",
"description": "Gulp plugin for sass",
"main": "index.js",
"engines": {
"node": ">=6"
"node": ">=12"
},
"scripts": {
"test": "./node_modules/.bin/mocha test"
"test": "mocha test"
},
"repository": {
"type": "git",
Expand All @@ -24,28 +24,29 @@
"url": "https://github.com/dlmanning/gulp-sass/issues"
},
"dependencies": {
"chalk": "^2.3.0",
"chalk": "^4.1.1",
"lodash": "^4.17.20",
"node-sass": "^4.8.3",
"plugin-error": "^1.0.1",
"replace-ext": "^1.0.0",
"strip-ansi": "^4.0.0",
"through2": "^2.0.0",
"vinyl-sourcemaps-apply": "^0.2.0"
"replace-ext": "^2.0.0",
"strip-ansi": "^6.0.0",
"transfob": "^1.0.0",
"vinyl-sourcemaps-apply": "^0.2.1"
},
"devDependencies": {
"autoprefixer": "^8.1.0",
"eslint": "^4.18.2",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.9.0",
"globule": "^1.0.0",
"gulp": "^3.8.11",
"gulp-postcss": "^7.0.1",
"gulp-sourcemaps": "^2.6.4",
"gulp-tap": "^0.1.3",
"mocha": "^5.0.4",
"rimraf": "^2.4.3",
"should": "^13.2.1",
"vinyl": "^2.1.0"
"autoprefixer": "^10.2.6",
"eslint": "^7.28.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.23.4",
"globule": "^1.3.2",
"gulp": "^4.0.2",
"gulp-postcss": "^9.0.0",
"gulp-sourcemaps": "^3.0.0",
"gulp-tap": "^2.0.0",
"mocha": "^9.0.0",
"node-sass": "^6.0.0",
"postcss": "^8.3.4",
"rimraf": "^3.0.2",
"should": "^13.2.3",
"vinyl": "^2.2.1"
}
}
53 changes: 27 additions & 26 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const should = require('should');
const Vinyl = require('vinyl');
const path = require('path');
const fs = require('fs');
const sass = require('../index');
const rimraf = require('rimraf');
const gulp = require('gulp');
const sourcemaps = require('gulp-sourcemaps');
Expand All @@ -11,6 +10,8 @@ const autoprefixer = require('autoprefixer');
const tap = require('gulp-tap');
const globule = require('globule');

const sass = require('../index')(require('node-sass'));

const createVinyl = (filename, contents) => {
const base = path.join(__dirname, 'scss');
const filePath = path.join(base, filename);
Expand All @@ -23,7 +24,7 @@ const createVinyl = (filename, contents) => {
});
};

const normaliseEOL = str => str.toString('utf8').replace(/\r\n/g, '\n');
const normaliseEOL = (str) => str.toString('utf8').replace(/\r\n/g, '\n');

describe('test helpers', () => {
it('should normalise EOL', (done) => {
Expand Down Expand Up @@ -224,14 +225,14 @@ describe('gulp-sass -- async compile', () => {
it('should work with gulp-sourcemaps', (done) => {
const sassFile = createVinyl('inheritance.scss');

sassFile.sourceMap = '{' +
'"version": 3,' +
'"file": "scss/subdir/multilevelimport.scss",' +
'"names": [],' +
'"mappings": "",' +
'"sources": [ "scss/subdir/multilevelimport.scss" ],' +
'"sourcesContent": [ "@import ../inheritance;" ]' +
'}';
sassFile.sourceMap = '{'
+ '"version": 3,'
+ '"file": "scss/subdir/multilevelimport.scss",'
+ '"names": [],'
+ '"mappings": "",'
+ '"sources": [ "scss/subdir/multilevelimport.scss" ],'
+ '"sourcesContent": [ "@import ../inheritance;" ]'
+ '}';

// Expected sources are relative to file.base
const expectedSources = [
Expand Down Expand Up @@ -425,14 +426,14 @@ describe('gulp-sass -- sync compile', () => {
'includes/_dogs.sass',
];

sassFile.sourceMap = '{' +
'"version": 3,' +
'"file": "scss/subdir/multilevelimport.scss",' +
'"names": [],' +
'"mappings": "",' +
'"sources": [ "scss/subdir/multilevelimport.scss" ],' +
'"sourcesContent": [ "@import ../inheritance;" ]' +
'}';
sassFile.sourceMap = '{'
+ '"version": 3,'
+ '"file": "scss/subdir/multilevelimport.scss",'
+ '"names": [],'
+ '"mappings": "",'
+ '"sources": [ "scss/subdir/multilevelimport.scss" ],'
+ '"sourcesContent": [ "@import ../inheritance;" ]'
+ '}';

const stream = sass.sync();
stream.on('data', (cssFile) => {
Expand Down Expand Up @@ -469,8 +470,8 @@ describe('gulp-sass -- sync compile', () => {
.pipe(tap((file) => {
should.exist(file.sourceMap);
file.sourceMap.sources.should.eql(expectedSourcesAfter);
}))
.on('end', done);
}));
done();
});

it('should work with gulp-sourcemaps and a globbed source', (done) => {
Expand All @@ -491,8 +492,8 @@ describe('gulp-sass -- sync compile', () => {
const actual = normaliseEOL(file.sourceMap.sourcesContent[0]);
const expected = normaliseEOL(filesContent[path.normalize(file.sourceMap.sources[0])]);
actual.should.eql(expected);
}))
.on('end', done);
}));
done();
});

it('should work with gulp-sourcemaps and autoprefixer with different file.base', (done) => {
Expand All @@ -519,8 +520,8 @@ describe('gulp-sass -- sync compile', () => {
.pipe(tap((file) => {
should.exist(file.sourceMap);
file.sourceMap.sources.should.eql(expectedSourcesAfter);
}))
.on('end', done);
}));
done();
});

it('should work with empty files', (done) => {
Expand All @@ -533,7 +534,7 @@ describe('gulp-sass -- sync compile', () => {
} catch (e) {
should.fail(false, true, 'Empty file was produced');
}
}))
.on('end', done);
}));
done();
});
});