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

[Fix] throw a better error if no svg is found; add tests for multiple SVGs. #39

Merged
merged 3 commits into from
May 17, 2018
Merged
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
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export default ({ types: t }) => ({
if (caseSensitive && !fileExistsWithCaseSync(svgPath)) {
throw new Error(`File path didn't match case of file on disk: ${svgPath}`);
}
if (!svgPath) {
throw new Error(`File path does not exist: ${importPath}`);
}
const rawSource = readFileSync(svgPath, 'utf8');
const optimizedSource = state.opts.svgo === false
? rawSource
Expand Down Expand Up @@ -108,6 +111,7 @@ export default ({ types: t }) => ({
path.replaceWith(svgReplacement);
}
file.get('ensureReact')();
file.set('ensureReact', () => {});
}
},
},
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/close2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions test/fixtures/test-multiple-svg.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import MySvg from './close.svg';
import MySvg2 from './close.svg';
import MySvg3 from './close2.svg';

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

export class MyClassIcon extends React.Component {
render() {
return (
<div>
<MySvg />
<MySvg2 />
<MySvg3 />
</div>
);
}
}
16 changes: 14 additions & 2 deletions test/sanity.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { transformFile } from 'babel-core';

function assertReactImport(result) {
const match = result.code.match(/import React from 'react'/);
const match = result.code.match(/import React from 'react'/g);
if (!match) {
throw new Error('no React import found');
}
if (match.length !== 1) {
throw new Error('more or less than one match found');
throw new Error(`more or less than one match found: ${match}\n${result.code}`);
}
}

Expand All @@ -22,6 +22,18 @@ transformFile('test/fixtures/test.jsx', {
console.log('test/fixtures/test.jsx', result.code);
});

transformFile('test/fixtures/test-multiple-svg.jsx', {
babelrc: false,
presets: ['react'],
plugins: [
'../../src/index',
],
}, (err, result) => {
if (err) throw err;
assertReactImport(result);
console.log('test/fixtures/test-multiple-svg.jsx', result.code);
});

transformFile('test/fixtures/test-no-react.jsx', {
babelrc: false,
presets: ['react'],
Expand Down