Skip to content

Commit

Permalink
Drop Node.js < 8 and related refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Nov 14, 2019
1 parent d30bda3 commit 222deb5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 89 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# next

* Update CSSO to [^4.0.0](https://github.com/css/csso/releases/tag/v4.0.0)
* Plugin never produce a parse error, but drops bad parts of CSS instead
* Drop support for Node.js < 8

# 3.0.1

* Drop dependency on deprecated `gulp-util` (#30, thanks to @TheDancingCode)
Expand Down
80 changes: 19 additions & 61 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,39 @@
'use strict';

var csso = require('csso'),
PluginError = require('plugin-error'),
Transform = require('stream').Transform,
applySourceMap = require('vinyl-sourcemaps-apply');

function processParseError(source, filename, details, message) {
function formatLines(start, end) {
return lines.slice(start, end).map(function(line, idx) {
var num = String(start + idx + 1);

while (num.length < maxNumLength) {
num = ' ' + num;
}

return num + ' |' + line;
}).join('\n');
}

var lines = source.split(/\n|\r\n?|\f/);
var column = details.column;
var line = details.line;
var startLine = Math.max(1, line - 2);
var endLine = Math.min(line + 2, lines.length + 1);
var maxNumLength = Math.max(4, String(endLine).length) + 1;

return [
'CSS parse error ' + filename + ': ' + message,
formatLines(startLine - 1, line),
new Array(column + maxNumLength + 2).join('-') + '^',
formatLines(line, endLine)
].join('\n');
}
const csso = require('csso');
const PluginError = require('plugin-error');
const { Transform } = require('stream');
const applySourceMap = require('vinyl-sourcemaps-apply');

module.exports = function (options) {
var stream = new Transform({ objectMode: true });
const stream = new Transform({ objectMode: true });

stream._transform = function (file, encoding, cb) {
function handleError(error) {
if ('parseError' in error) {
error = processParseError(source, inputFile, error.parseError, error.message);
}

cb(new PluginError('gulp-csso', error));
}

if (file.isNull()) {
return cb(null, file);
}

if (file.isStream()) {
return handleError('Streaming not supported');
return cb(new PluginError('gulp-csso', 'Streaming not supported'));
}

var inputFile = file.relative;
var source = String(file.contents);
var cssoOptions = {
filename: inputFile,
sourceMap: Boolean(file.sourceMap),
restructure: true,
debug: false
};

if (options === undefined || typeof options === 'boolean') {
// for backward capability
cssoOptions.restructure = !options;
} else if (options) {
// extend default csso options
for (var name in options) {
if (options.hasOwnProperty(name) && name !== 'filename') {
cssoOptions[name] = options[name];
}
}
options = {
restructure: !options
};
}

const inputFile = file.relative;
const source = String(file.contents);
const cssoOptions = Object.assign({
sourceMap: Boolean(file.sourceMap),
restructure: true,
debug: false
}, options, { filename: inputFile }); // filename can't be overridden

try {
var result = csso.minify(source, cssoOptions);
const result = csso.minify(source, cssoOptions);

if (result.map) {
applySourceMap(file, result.map.toJSON());
Expand All @@ -86,7 +44,7 @@ module.exports = function (options) {
file.contents = new Buffer(result.css);
cb(null, file);
} catch(error) {
handleError(error);
cb(new PluginError('gulp-csso', error));
}
};

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@
"tape": "^4.2.1",
"vinyl": "^2.1.0"
},
"main": "index.js"
"main": "index.js",
"engines": {
"node": ">=8.0.0"
}
}
69 changes: 42 additions & 27 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
'use strict';

var csso = require('./'),
test = require('tape'),
Stream = require('stream'),
Vinyl = require('vinyl'),
fs = require('fs'),

basestyle = 'h1 { color: yellow; } \n h1 { font-size: 2em; }',
optimalmin = 'h1{color:#ff0;font-size:2em}',
nonoptimal = 'h1{color:#ff0}h1{font-size:2em}',
sourceMapCss = readSourceMapTestFile('css'),
sourceMapCssMin = readSourceMapTestFile('min.css'),
sourceMap = readSourceMapTestFile('map'),
sourceMapInitial = readSourceMapTestFile('initial.map'),
sourceMapContinue = readSourceMapTestFile('continue.map');
const csso = require('./');
const test = require('tape');
const Stream = require('stream');
const Vinyl = require('vinyl');
const fs = require('fs');

const basestyle = 'h1 { color: yellow; } \n h1 { font-size: 2em; }';
const optimalmin = 'h1{color:#ff0;font-size:2em}';
const nonoptimal = 'h1{color:#ff0}h1{font-size:2em}';
const sourceMapCss = readSourceMapTestFile('css');
const sourceMapCssMin = readSourceMapTestFile('min.css');
const sourceMap = readSourceMapTestFile('map');
const sourceMapInitial = readSourceMapTestFile('initial.map');
const sourceMapContinue = readSourceMapTestFile('continue.map');

function readSourceMapTestFile(ext) {
return fs.readFileSync(__dirname + '/test/source-map.' + ext, 'utf8').trimRight();
Expand All @@ -29,7 +29,7 @@ function fixture (contents) {
}

function sourceMapFixture (contents, sourceMap) {
var file = new Vinyl({
const file = new Vinyl({
contents: new Buffer(contents),
cwd: __dirname,
base: __dirname,
Expand All @@ -42,7 +42,7 @@ function sourceMapFixture (contents, sourceMap) {
test('should minify css with csso, performing structural optimisation', function (t) {
t.plan(1);

var stream = csso();
const stream = csso();

stream.on('data', function (file) {
t.equal(String(file.contents), optimalmin);
Expand All @@ -54,7 +54,7 @@ test('should minify css with csso, performing structural optimisation', function
test('should minify css with csso, performing structural optimisation when options is `false` (backward compatibility)', function (t) {
t.plan(1);

var stream = csso(false);
const stream = csso(false);

stream.on('data', function (file) {
t.equal(String(file.contents), optimalmin);
Expand All @@ -66,7 +66,7 @@ test('should minify css with csso, performing structural optimisation when optio
test('should minify css with csso, with no structural optimisation', function (t) {
t.plan(1);

var stream = csso({ restructure: false });
const stream = csso({ restructure: false });

stream.on('data', function (file) {
t.equal(String(file.contents), nonoptimal);
Expand All @@ -78,7 +78,7 @@ test('should minify css with csso, with no structural optimisation', function (t
test('should minify css with csso, with no structural optimisation when options is `true` (backward compatibility)', function (t) {
t.plan(1);

var stream = csso(true);
const stream = csso(true);

stream.on('data', function (file) {
t.equal(String(file.contents), nonoptimal);
Expand All @@ -90,25 +90,25 @@ test('should minify css with csso, with no structural optimisation when options
test('should let null files pass through', function (t) {
t.plan(1);

var stream = csso();
const stream = csso();

stream.on('data', function (data) {
t.equal(data.contents, null, 'should not transform null in any way');
});

var file = fixture(null);
const file = fixture(null);

stream.write(file);
});

test('should throw an error in stream mode', function (t) {
t.plan(1);

var stream = csso();
const stream = csso();

var file = fixture(new Stream.Readable());
const file = fixture(new Stream.Readable());

var write = function () {
const write = function () {
stream.write(file);
file.contents.write(basestyle);
file.contents.end();
Expand All @@ -121,7 +121,22 @@ test('should throw an error in stream mode', function (t) {
test('should generate source map when sourceMap is true', function (t) {
t.plan(2);

var stream = csso({ sourceMap: true });
const stream = csso({ sourceMap: true });

stream.on('data', function (file) {
t.equal(String(file.contents), sourceMapCssMin);
t.deepEqual(file.sourceMap, JSON.parse(sourceMapInitial));
});

stream.write(sourceMapFixture(
sourceMapCss
));
});

test('should not override filename by options', function (t) {
t.plan(2);

const stream = csso({ sourceMap: true, filename: 'foobar' });

stream.on('data', function (file) {
t.equal(String(file.contents), sourceMapCssMin);
Expand All @@ -136,7 +151,7 @@ test('should generate source map when sourceMap is true', function (t) {
test('should generate source map when file has source map', function (t) {
t.plan(2);

var stream = csso();
const stream = csso();

stream.on('data', function (file) {
t.equal(String(file.contents), sourceMapCssMin);
Expand All @@ -152,7 +167,7 @@ test('should generate source map when file has source map', function (t) {
test('should not generate source map when sourceMap setting is false', function (t) {
t.plan(2);

var stream = csso({ sourceMap: false });
const stream = csso({ sourceMap: false });

stream.on('data', function (file) {
t.equal(String(file.contents), sourceMapCssMin);
Expand Down

0 comments on commit 222deb5

Please sign in to comment.