Skip to content

Commit

Permalink
Deduplicate getComponentName, svgr has the same function
Browse files Browse the repository at this point in the history
  • Loading branch information
coolreader18 committed Feb 28, 2022
1 parent 279d8f8 commit e10c41b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
3 changes: 1 addition & 2 deletions packages/transformers/svg-react/package.json
Expand Up @@ -23,7 +23,6 @@
"@parcel/plugin": "2.3.2",
"@svgr/core": "^6.2.0",
"@svgr/plugin-jsx": "^6.2.0",
"@svgr/plugin-svgo": "^6.2.0",
"camelcase": "^6.3.0"
"@svgr/plugin-svgo": "^6.2.0"
}
}
20 changes: 4 additions & 16 deletions packages/transformers/svg-react/src/SvgReactTransformer.js
Expand Up @@ -2,39 +2,27 @@

import {Transformer} from '@parcel/plugin';

import path from 'path';
import camelcase from 'camelcase';
import svgoPlugin from '@svgr/plugin-svgo';
import jsxPlugin from '@svgr/plugin-jsx';
import {transform} from '@svgr/core';

function getComponentName(filePath) {
let validCharacters = /[^a-zA-Z0-9_-]/g;
let name = path.parse(filePath).name.replace(validCharacters, '');
return camelcase(name, {
pascalCase: true,
});
}

export default (new Transformer({
async loadConfig({config}) {
let { contents } = await config.getConfig(['.svgrrc', '.svgrrc.json']);
return contents;
let result = await config.getConfig(['.svgrrc', '.svgrrc.json']);
return result?.contents ?? {};
},

async transform({asset,config}) {
async transform({asset, config}) {
let code = await asset.getCode();
let componentName = getComponentName(asset.filePath);

const jsx = await transform(
code,
{ ...config, runtimeConfig: false },
{...config, runtimeConfig: false},
{
caller: {
name: '@parcel/transformer-svg-react',
defaultPlugins: [svgoPlugin, jsxPlugin],
},
componentName,
filePath: asset.filePath,
},
);
Expand Down

0 comments on commit e10c41b

Please sign in to comment.