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

Provide option for spreading props rather than static assignment #86

Draft
wants to merge 1 commit into
base: master
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ npm install --save-dev babel-plugin-inline-react-svg

- `ignorePattern` - A pattern that imports will be tested against to selectively ignore imports.
- `caseSensitive` - A boolean value that if true will require file paths to match with case-sensitivity. Useful to ensure consistent behavior if working on both a case-sensitive operating system like Linux and a case-insensitive one like OS X or Windows.
- *`spreadDefaultProps`* - A boolean value that if `true` will spread additional props, rather than setting them as `defaultProps` static assignment. This is important for tree shaking, as static assignments prevent efficient dead code removal.
- `svgo` - svgo options (`false` to disable). Example:
```json
{
Expand Down
26 changes: 22 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,26 @@ export default declare(({
`;

if (SVG_NAME !== 'default') {
return template(namedTemplate)({ SVG_NAME, SVG_CODE, SVG_DEFAULT_PROPS_CODE });
const substitutions = { SVG_CODE, SVG_NAME };

// If a key is present in the substitutions object, but is unused in the template, even if
// even if it's value is undefined, Babel will throw an error.
if (SVG_DEFAULT_PROPS_CODE) {
substitutions.SVG_DEFAULT_PROPS_CODE = SVG_DEFAULT_PROPS_CODE;
}

return template(namedTemplate)(substitutions);
}
return template(anonymousTemplate)({ SVG_CODE, SVG_DEFAULT_PROPS_CODE, EXPORT_FILENAME });

const substitutions = { SVG_CODE, EXPORT_FILENAME };

// If a key is present in the substitutions object, but is unused in the template, even if
// even if it's value is undefined, Babel will throw an error.
if (SVG_DEFAULT_PROPS_CODE) {
substitutions.SVG_DEFAULT_PROPS_CODE = SVG_DEFAULT_PROPS_CODE;
}

return template(anonymousTemplate)(substitutions);
};

function applyPlugin(importIdentifier, importPath, path, state, isExport, exportFilename) {
Expand Down Expand Up @@ -95,8 +112,9 @@ export default declare(({
EXPORT_FILENAME: exportFilename,
};

// Move props off of element and into defaultProps
if (svgCode.openingElement.attributes.length > 1) {
// Move props off of element and into defaultProps, but only if
// the "spreadDefaultProps" argument is not true
if (state.opts.spreadDefaultProps !== true && svgCode.openingElement.attributes.length > 1) {
const keepProps = [];
const defaultProps = [];

Expand Down
25 changes: 24 additions & 1 deletion test/sanity.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@ function assertReactImport(result) {
}
}

function assertSpreadProps(result) {
const hasSpreadProps = result.code.match(/_extends\(\{.*"'data-name'": "Livello 1".*\}, props\)/gs);
if (!hasSpreadProps) {
throw new Error('Spread props were not found');
}

const hasDefaultProps = result.code.match(/\.defaultProps/g);
if (hasDefaultProps) {
throw new Error('Default props were found, when spread should have been used instead');
}
}

function validateDefaultProps(result) {
if (!(/'data-name':/g).test(result.code)) {
if (!(/'data-name':?/g).test(result.code)) {
throw new Error('data-* props need to be quoted');
}
}
Expand Down Expand Up @@ -226,3 +238,14 @@ transformFile('test/fixtures/test-commented.jsx', {
console.log('test/fixtures/test-commented.jsx', result.code);
});
*/
transformFile('test/fixtures/test-export-default-as.jsx', {
presets: ['airbnb'],
plugins: [
[inlineReactSvgPlugin, { spreadDefaultProps: true }],
],
}, (err, result) => {
if (err) throw err;
console.log('test/fixtures/test-export-default-as.jsx', result.code);
validateDefaultProps(result);
assertSpreadProps(result);
});
Loading