Skip to content

Commit

Permalink
Update svg-sprite to v3.0.0-rc1 and drop Node.js < 16 support
Browse files Browse the repository at this point in the history
Also, switch to 2-space indentation like upstream
  • Loading branch information
XhmikosR committed Mar 22, 2024
1 parent 227cca3 commit 3095b3d
Show file tree
Hide file tree
Showing 11 changed files with 3,793 additions and 4,529 deletions.
3 changes: 1 addition & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
Expand All @@ -15,7 +15,6 @@ indent_size = 2

[*.md]
trim_trailing_whitespace = false
indent_size = 2

[test/fixtures/*]
insert_final_newline = false
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
node: [12, 14, 16, 18, 20]
node: [16, 18, 20]
include:
- os: macos-latest
node: 18
Expand Down
18 changes: 9 additions & 9 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ const { src, dest, parallel } = require('gulp');
const svgSprite = require('./index.js');

const svgspriteConfig = {
mode: {
css: {
render: {
css: true
}
}
mode: {
css: {
render: {
css: true
}
}
}
};

function buildSvg() {
return src('test/fixtures/**/*.svg')
.pipe(svgSprite(svgspriteConfig))
.pipe(dest('tmp/gulp'));
return src('test/fixtures/**/*.svg')
.pipe(svgSprite(svgspriteConfig))
.pipe(dest('tmp/gulp'));
}

exports.default = parallel(buildSvg);
96 changes: 48 additions & 48 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

/** @typedef {import('stream').TransformOptions} TransformOptions */

const { Transform } = require('stream');
const { format } = require('util');
const { Transform } = require('node:stream');
const { format } = require('node:util');
const PluginError = require('plugin-error');
const SVGSpriter = require('svg-sprite');

Expand All @@ -26,28 +26,28 @@ const PLUGIN_NAME = 'gulp-svg-sprite';
* @param {TransformOptions['flush']} flush
*/
function transfob(transform, flush) {
return new Transform({
flush,
transform,
objectMode: true
});
return new Transform({
flush,
transform,
objectMode: true
});
}

// Extend plugin error
function createExtendedPluginError(...args) {
const error = args.find(a => a instanceof Error);
let { message } = error;
const error = args.find(a => a instanceof Error);
let { message } = error;

if (args.length > 1 && typeof args[0] === 'string') {
message = format(...args);
}
if (args.length > 1 && typeof args[0] === 'string') {
message = format(...args);
}

const pluginError = new PluginError(PLUGIN_NAME, message || 'Unspecified error');
if (error) {
pluginError.name = error.name;
}
const pluginError = new PluginError(PLUGIN_NAME, message || 'Unspecified error');
if (error) {
pluginError.name = error.name;
}

return pluginError;
return pluginError;
}

/**
Expand All @@ -56,42 +56,42 @@ function createExtendedPluginError(...args) {
* @param {Object} config SVGSpriter main configuration
*/
function gulpSVGSprite(config) {
// Instantiate spriter instance
const spriter = new SVGSpriter(config);
let shapes = 0;
// Instantiate spriter instance
const spriter = new SVGSpriter(config);
let shapes = 0;

// Intercept error log and convert to plugin errors
spriter.config.log.error = function(...args) {
this.emit('error', createExtendedPluginError(...args));
};
// Intercept error log and convert to plugin errors
spriter.config.log.error = function(...args) {
this.emit('error', createExtendedPluginError(...args));
};

return transfob((file, encoding, callback) => {
let error = null;
try {
spriter.add(file);
++shapes;
} catch (error_) {
error = (!error_.plugin || (error_.plugin !== PLUGIN_NAME)) ?
createExtendedPluginError(error_) :
error_;
}
return transfob((file, encoding, callback) => {
let error = null;
try {
spriter.add(file);
++shapes;
} catch (error_) {
error = (!error_.plugin || (error_.plugin !== PLUGIN_NAME)) ?
createExtendedPluginError(error_) :
error_;
}

return callback(error);
}, function(callback) {
spriter.compile((error, result) => {
if (error) {
this.emit('error', new PluginError(PLUGIN_NAME, error));
} else if (shapes > 0) {
for (const mode of Object.values(result)) {
for (const resource of Object.values(mode)) {
this.push(resource);
}
}
}
return callback(error);
}, function(callback) {
spriter.compile((error, result) => {
if (error) {
this.emit('error', new PluginError(PLUGIN_NAME, error));
} else if (shapes > 0) {
for (const mode of Object.values(result)) {
for (const resource of Object.values(mode)) {
this.push(resource);
}
}
}

callback();
});
callback();
});
});
}

module.exports = gulpSVGSprite;
Loading

0 comments on commit 3095b3d

Please sign in to comment.