diff --git a/src/index.js b/src/index.js index 2934b6c..b5e0599 100644 --- a/src/index.js +++ b/src/index.js @@ -24,24 +24,21 @@ export default declare(({ EXPORT_FILENAME, SVG_NAME, SVG_CODE, - SVG_DEFAULT_PROPS_CODE, }) => { const namedTemplate = ` var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; }; - ${SVG_DEFAULT_PROPS_CODE ? 'SVG_NAME.defaultProps = SVG_DEFAULT_PROPS_CODE;' : ''} ${IS_EXPORT ? 'export { SVG_NAME };' : ''} `; const anonymousTemplate = ` var Component = function (props) { return SVG_CODE; }; - ${SVG_DEFAULT_PROPS_CODE ? 'Component.defaultProps = SVG_DEFAULT_PROPS_CODE;' : ''} Component.displayName = 'EXPORT_FILENAME'; export default Component; `; if (SVG_NAME !== 'default') { - return template(namedTemplate)({ SVG_NAME, SVG_CODE, SVG_DEFAULT_PROPS_CODE }); + return template(namedTemplate)({ SVG_NAME, SVG_CODE }); } - return template(anonymousTemplate)({ SVG_CODE, SVG_DEFAULT_PROPS_CODE, EXPORT_FILENAME }); + return template(anonymousTemplate)({ SVG_CODE, EXPORT_FILENAME }); }; function applyPlugin(importIdentifier, importPath, path, state, isExport, exportFilename) { @@ -95,32 +92,8 @@ export default declare(({ EXPORT_FILENAME: exportFilename, }; - // Move props off of element and into defaultProps - if (svgCode.openingElement.attributes.length > 1) { - const keepProps = []; - const defaultProps = []; - - 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)); - } - }); - - svgCode.openingElement.attributes = keepProps; - opts.SVG_DEFAULT_PROPS_CODE = t.objectExpression(defaultProps); - } - const svgReplacement = buildSvg(opts); - if (opts.SVG_DEFAULT_PROPS_CODE) { - [newPath] = path.replaceWithMultiple(svgReplacement); - } else { - newPath = path.replaceWith(svgReplacement); - } + [newPath] = path.replaceWithMultiple(svgReplacement); file.get('ensureReact')(); file.set('ensureReact', () => {}); diff --git a/test/sanity.js b/test/sanity.js index c397fba..3ed82f1 100644 --- a/test/sanity.js +++ b/test/sanity.js @@ -14,12 +14,6 @@ function assertReactImport(result) { } } -function validateDefaultProps(result) { - if (!(/'data-name':/g).test(result.code)) { - throw new Error('data-* props need to be quoted'); - } -} - transformFile('test/fixtures/test-import.jsx', { babelrc: false, presets: ['@babel/preset-react'], @@ -29,7 +23,6 @@ transformFile('test/fixtures/test-import.jsx', { }, (err, result) => { if (err) throw err; assertReactImport(result); - validateDefaultProps(result); console.log('test/fixtures/test-import.jsx', result.code); }); @@ -42,7 +35,6 @@ transformFile('test/fixtures/test-multiple-svg.jsx', { }, (err, result) => { if (err) throw err; assertReactImport(result); - validateDefaultProps(result); console.log('test/fixtures/test-multiple-svg.jsx', result.code); }); @@ -56,7 +48,6 @@ transformFile('test/fixtures/test-no-react.jsx', { if (err) throw err; console.log('test/fixtures/test-no-react.jsx', result.code); assertReactImport(result); - validateDefaultProps(result); }); transformFile('test/fixtures/test-no-duplicate-react.jsx', { @@ -80,7 +71,6 @@ transformFile('test/fixtures/test-no-duplicate-react.jsx', { if (err) throw err; console.log('test/fixtures/test-no-duplicate-react.jsx', result.code); assertReactImport(result); - validateDefaultProps(result); }); if (fs.existsSync(path.resolve('./PACKAGE.JSON'))) {