Skip to content

Commit

Permalink
fix(v4): remove is-svg dependency (#1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
AviVahl committed Apr 6, 2021
1 parent d0f65e2 commit 83985ca
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 2,467 deletions.
855 changes: 9 additions & 846 deletions packages/cssnano-preset-advanced/yarn.lock

Large diffs are not rendered by default.

735 changes: 6 additions & 729 deletions packages/cssnano-preset-default/yarn.lock

Large diffs are not rendered by default.

871 changes: 5 additions & 866 deletions packages/cssnano/yarn.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion packages/postcss-svgo/package.json
Expand Up @@ -33,7 +33,6 @@
},
"repository": "cssnano/cssnano",
"dependencies": {
"is-svg": "^3.0.0",
"postcss": "^7.0.0",
"postcss-value-parser": "^3.0.0",
"svgo": "^1.0.0"
Expand Down
13 changes: 7 additions & 6 deletions packages/postcss-svgo/src/__tests__/index.js
Expand Up @@ -152,6 +152,13 @@ test(
'h1{background-image:url("data:image/svg;charset=US-ASCII,<?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve"><circle cx="50" cy="50" r="40" fill="yellow" /><!--test comment--></svg>")}',
);

test('should warn on SVG containing unclosed tags', async (t) => {
const css =
'h1{background:url(data:image/svg+xml;charset=utf-8,<svg>style type="text/css"><![CDATA[ svg { fill: red; } ]]></style></svg>)}';
const result = await postcss(plugin()).process(css, {from: undefined});
t.is(result.messages.length, 1);
t.is(result.messages[0].type, 'warning');
});

test(
'should pass through links to svg files',
Expand All @@ -166,12 +173,6 @@ test(
`h1{background-image: url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" width="120" height="32" viewBox="0 0 1200 320"><path d="M137.189 140V17.17h-36.676L73.871 31.832z" fill="%23639"/></svg>');}`
);

test('should reject on malformed svgs', async t => {
const css = 'h1{background:url(data:image/svg+xml;charset=utf-8,<svg>style type="text/css"><![CDATA[ svg { fill: red; } ]]></style></svg>)}';
const error = await t.throws(postcss(plugin()).process(css));
t.truthy(error);
});

test('should not crash on malformed urls when encoded', t => {
const svg = encode(file(`${__dirname}/border.svg`, 'utf-8'));
t.notThrows(() => decode(svg));
Expand Down
17 changes: 8 additions & 9 deletions packages/postcss-svgo/src/index.js
@@ -1,14 +1,13 @@
import postcss from 'postcss';
import valueParser from 'postcss-value-parser';
import SVGO from 'svgo';
import isSvg from 'is-svg';
import {encode, decode} from './lib/url';

const PLUGIN = 'postcss-svgo';
const dataURI = /data:image\/svg\+xml(;((charset=)?utf-8|base64))?,/i;
const dataURIBase64 = /data:image\/svg\+xml;base64,/i;

function minifyPromise (decl, getSvgo, opts) {
function minifyPromise (decl, getSvgo, opts, postcssResult) {
const promises = [];
const parsed = valueParser(decl.value);

Expand Down Expand Up @@ -44,13 +43,13 @@ function minifyPromise (decl, getSvgo, opts) {
}
}

if (!isSvg(svg)) {
return;
}

promises.push(
getSvgo().optimize(svg)
.then(result => {
if (result.error) {
decl.warn(postcssResult, `${result.error}`);
return;
}
let data, optimizedValue;

if (isBase64) {
Expand All @@ -75,7 +74,7 @@ function minifyPromise (decl, getSvgo, opts) {
});
})
.catch(error => {
throw new Error(`${PLUGIN}: ${error}`);
decl.warn(postcssResult, `${error}`);
})
);

Expand All @@ -96,7 +95,7 @@ export default postcss.plugin(PLUGIN, (opts = {}) => {
return svgo;
};

return css => {
return (css, result) => {
return new Promise((resolve, reject) => {
const svgoQueue = [];

Expand All @@ -105,7 +104,7 @@ export default postcss.plugin(PLUGIN, (opts = {}) => {
return;
}

svgoQueue.push(minifyPromise(decl, getSvgo, opts));
svgoQueue.push(minifyPromise(decl, getSvgo, opts, result));
});

return Promise.all(svgoQueue).then(resolve, reject);
Expand Down
10 changes: 0 additions & 10 deletions packages/postcss-svgo/yarn.lock
Expand Up @@ -802,10 +802,6 @@ home-or-tmp@^2.0.0:
os-homedir "^1.0.0"
os-tmpdir "^1.0.1"

html-comment-regex@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/html-comment-regex/-/html-comment-regex-1.1.1.tgz#668b93776eaae55ebde8f3ad464b307a4963625e"

iconv-lite@^0.4.4:
version "0.4.24"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
Expand Down Expand Up @@ -979,12 +975,6 @@ is-regex@^1.0.4:
dependencies:
has "^1.0.1"

is-svg@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/is-svg/-/is-svg-3.0.0.tgz#9321dbd29c212e5ca99c4fa9794c714bcafa2f75"
dependencies:
html-comment-regex "^1.1.0"

is-symbol@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-symbol/-/is-symbol-1.0.2.tgz#a055f6ae57192caee329e7a860118b497a950f38"
Expand Down

0 comments on commit 83985ca

Please sign in to comment.