Skip to content

Commit

Permalink
Update svgo
Browse files Browse the repository at this point in the history
  • Loading branch information
bertdeblock committed Aug 17, 2023
1 parent 9f59c06 commit 0ecfee2
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 295 deletions.
47 changes: 13 additions & 34 deletions packages/broccoli-svg-optimizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,7 @@

const PersistentFilter = require('broccoli-persistent-filter');
const stringify = require('safe-stable-stringify');
const DefaultSVGO = require('svgo');

function promisify(optimize) {
return svg => {
return new Promise((resolve, reject) => {
optimize(svg, result => {
if (result.error) {
reject(result.error);
} else {
resolve(result);
}
});
});
};
}

function promisifyIfNeeded(optimize) {
let isPromise = false;

try {
isPromise = 'then' in optimize('');
} catch (e) {
// pass
}

return isPromise ? optimize : promisify(optimize);
}
const { optimize: svgoOptimize } = require('svgo');

class SVGOFilter extends PersistentFilter {
constructor(inputNode, options) {
Expand All @@ -43,17 +17,22 @@ class SVGOFilter extends PersistentFilter {
annotation: options.annotation,
});

let SVGO = options.svgoModule || DefaultSVGO;
let svgo = new SVGO(options.svgoConfig);
let optimize = svgo.optimize.bind(svgo);
this.optimize = promisifyIfNeeded(optimize);
this.svgoConfig = options.svgoConfig || {};
this.svgoOptimize = options.svgoOptimize || svgoOptimize;
this.optionsHash = stringify(options);
}

processString(svg, relativePath) {
return svg
? this.optimize(svg, { path: relativePath }).then(({ data }) => data)
: Promise.resolve('');
if (svg) {
const { data } = this.svgoOptimize(svg, {
...this.svgoConfig,
path: relativePath,
});

return data;
}

return '';
}

cacheKeyProcessString(string, relativePath) {
Expand Down
2 changes: 1 addition & 1 deletion packages/broccoli-svg-optimizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"dependencies": {
"broccoli-persistent-filter": "^3.1.2",
"safe-stable-stringify": "^2.2.0",
"svgo": "1.3.0"
"svgo": "^2.0.0"
},
"devDependencies": {
"broccoli": "^3.1.2",
Expand Down
94 changes: 11 additions & 83 deletions packages/broccoli-svg-optimizer/tests/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const fixture = require('broccoli-fixture');
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const { extendDefaultPlugins } = require('svgo');
const SVGOptimizer = require('../');

const { expect } = chai;
Expand All @@ -15,7 +16,7 @@ describe('broccoli-svg-optimizer', () => {
let outputNode = fixture.build(new SVGOptimizer(inputNode));
let OPTIMIZED_CONTENT =
'<svg viewBox="0 0 13 13" xmlns="http://www.w3.org/2000/svg">' +
'<path d="M7 6V0H6v6H0v1h6v6h1V7h6V6H7z"/></svg>';
'<path d="M7 6V0H6v6H0v1h6v6h1V7h6V6H7Z"/></svg>';
return expect(outputNode).to.eventually.deep.equal({
'test.svg': OPTIMIZED_CONTENT,
});
Expand All @@ -26,7 +27,7 @@ describe('broccoli-svg-optimizer', () => {
let outputNode = fixture.build(new SVGOptimizer(inputNode, options));
let OPTIMIZED_CONTENT =
'<svg viewBox="0 0 13 13" xmlns="http://www.w3.org/2000/svg">' +
'<path d="M7 6V0H6v6H0v1h6v6h1V7h6V6H7z"/></svg>';
'<path d="M7 6V0H6v6H0v1h6v6h1V7h6V6H7Z"/></svg>';

return expect(outputNode).to.eventually.deep.equal({
'test.svg': OPTIMIZED_CONTENT,
Expand All @@ -36,7 +37,10 @@ describe('broccoli-svg-optimizer', () => {
it('accepts SVGO config', () => {
let options = {
svgoConfig: {
plugins: [{ removeTitle: false }, { removeDesc: false }],
plugins: extendDefaultPlugins([
{ active: false, name: 'removeDesc' },
{ active: false, name: 'removeTitle' },
]),
},
persist: false,
};
Expand All @@ -45,97 +49,21 @@ describe('broccoli-svg-optimizer', () => {
'<svg viewBox="0 0 13 13" xmlns="http://www.w3.org/2000/svg">' +
'<title>SVG title</title>' +
'<desc>SVG description</desc>' +
'<path d="M7 6V0H6v6H0v1h6v6h1V7h6V6H7z"/></svg>';
'<path d="M7 6V0H6v6H0v1h6v6h1V7h6V6H7Z"/></svg>';

return expect(outputNode).to.eventually.deep.equal({
'test.svg': OPTIMIZED_CONTENT,
});
});

it('works with callback-based SVGO module - success', () => {
class CustomSVGO {
optimize(svg, callback) {
callback({ data: 'callback result' });
}
}

let options = {
svgoConfig: {},
svgoModule: CustomSVGO,
persist: false,
};

let outputNode = fixture.build(new SVGOptimizer(inputNode, options));

return expect(outputNode).to.eventually.deep.equal({
'test.svg': 'callback result',
});
});

it('works with callback-based SVGO module - fail', () => {
class CustomSVGO {
optimize(svg, callback) {
callback({ error: 'callback error' });
}
}

let options = {
svgoConfig: {},
svgoModule: CustomSVGO,
persist: false,
};

let outputNode = fixture.build(new SVGOptimizer(inputNode, options));
return expect(outputNode).to.be.rejectedWith('callback error');
});

it('works with promise-based SVGO module - success', () => {
class CustomSVGO {
optimize(/* svg */) {
return Promise.resolve({ data: 'promise result' });
}
}

let options = {
svgoConfig: {},
svgoModule: CustomSVGO,
persist: false,
};

let outputNode = fixture.build(new SVGOptimizer(inputNode, options));

return expect(outputNode).to.eventually.deep.equal({
'test.svg': 'promise result',
});
});

it('works with promise-based SVGO module - fail', () => {
class CustomSVGO {
optimize(/* svg */) {
return Promise.reject('promise error');
}
}

let options = {
svgoConfig: {},
svgoModule: CustomSVGO,
persist: false,
};

let outputNode = fixture.build(new SVGOptimizer(inputNode, options));
return expect(outputNode).to.be.rejectedWith('promise error');
});

it('passes file to optimize', () => {
class CustomSVGO {
optimize(svg, options) {
return Promise.resolve({ data: options && options.path });
}
function svgoOptimize(svg, options) {
return { data: options && options.path };
}

let options = {
svgoConfig: {},
svgoModule: CustomSVGO,
svgoOptimize,
persist: false,
};

Expand Down
6 changes: 3 additions & 3 deletions packages/ember-svg-jar/lib/build-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ function buildOptions(app) {
stripPath: true,
optimizer: {
plugins: [
{ removeTitle: false },
{ removeDesc: { removeAny: false } },
{ removeViewBox: false },
{ name: 'removeTitle', active: false },
{ name: 'removeDesc', params: { removeAny: false } },
{ name: 'removeViewBox', active: false },
],
},
persist: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/ember-svg-jar/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ module.exports = {
const SVGOptimizer = require('broccoli-svg-optimizer');

return new SVGOptimizer(originalSvgs, {
svgoConfig: _.omit(optimizerConfig, 'svgoModule'),
svgoModule: optimizerConfig.svgoModule,
svgoConfig: _.omit(optimizerConfig, 'svgoOptimize'),
svgoOptimize: optimizerConfig.svgoOptimize,
persist: this.svgJarOptions.persist,
});
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0ecfee2

Please sign in to comment.