Skip to content

Commit

Permalink
[Fix] Fix crash when svg has a style tag in the root element; pass pa…
Browse files Browse the repository at this point in the history
…th to SVGO
  • Loading branch information
Özgün Yalcin authored and ljharb committed Sep 2, 2022
1 parent 6e130a5 commit a4c1c4c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default declare(({
const rawSource = readFileSync(svgPath, 'utf8');
const optimizedSource = state.opts.svgo === false
? { data: rawSource }
: optimize(rawSource, state.opts.svgo);
: optimize(rawSource, { ...state.opts.svgo, path: svgPath });

const escapeSvgSource = escapeBraces(optimizedSource);

Expand Down Expand Up @@ -103,6 +103,9 @@ export default declare(({
svgCode.openingElement.attributes.forEach((prop) => {
if (prop.type === 'JSXSpreadAttribute') {
keepProps.push(prop);
} else if (prop.value.type === 'JSXExpressionContainer') {
const objectExpression = t.objectExpression(prop.value.expression.properties);
defaultProps.push(t.objectProperty(t.identifier(prop.name.name), objectExpression));
} else {
defaultProps.push(t.objectProperty(t.identifier(prop.name.name), prop.value));
}
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/root-styled.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions test/fixtures/test-root-styled.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import SVG from './root-styled.svg';

export function MyFunctionIcon() {
return <SVG />;
}

export class MyClassIcon extends React.Component {
render() {
return <SVG />;
}
}
10 changes: 10 additions & 0 deletions test/sanity.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ transformFile('test/fixtures/test-export-all-as.jsx', {
console.log('test/fixtures/test-export-all-as.jsx', result.code);
});

transformFile('test/fixtures/test-root-styled.jsx', {
presets: ['airbnb'],
plugins: [
inlineReactSvgPlugin,
],
}, (err, result) => {
if (err) throw err;
console.log('test/fixtures/test-root-styled.jsx', result.code);
});

transformFile('test/fixtures/test-commented.jsx', {
presets: ['airbnb'],
plugins: [
Expand Down

0 comments on commit a4c1c4c

Please sign in to comment.