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 svg-sprite to v3.0.0-rc2 and drop Node.js < 18 support #145

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
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: [18, 20]
include:
- os: macos-latest
node: 18
Expand Down
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

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