Skip to content

Commit 785dd47

Browse files
dallonfiansu
authored andcommitted
Generate SVG component name in Jest fileTransform (facebook#6706)
1 parent 18b399b commit 785dd47

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

packages/react-scripts/config/jest/fileTransform.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const path = require('path');
4+
const camelcase = require('camelcase');
45

56
// This is a custom Jest transformer turning file imports into filenames.
67
// http://facebook.github.io/jest/docs/en/webpack.html
@@ -10,19 +11,27 @@ module.exports = {
1011
const assetFilename = JSON.stringify(path.basename(filename));
1112

1213
if (filename.match(/\.svg$/)) {
14+
// Based on how SVGR generates a component name:
15+
// https://github.com/smooth-code/svgr/blob/01b194cf967347d43d4cbe6b434404731b87cf27/packages/core/src/state.js#L6
16+
const pascalCaseFileName = camelcase(path.parse(filename).name, {
17+
pascalCase: true,
18+
});
19+
const componentName = `Svg${pascalCaseFileName}`;
1320
return `const React = require('react');
1421
module.exports = {
1522
__esModule: true,
1623
default: ${assetFilename},
17-
ReactComponent: React.forwardRef((props, ref) => ({
18-
$$typeof: Symbol.for('react.element'),
19-
type: 'svg',
20-
ref: ref,
21-
key: null,
22-
props: Object.assign({}, props, {
23-
children: ${assetFilename}
24-
})
25-
})),
24+
ReactComponent: React.forwardRef(function ${componentName}(props, ref) {
25+
return {
26+
$$typeof: Symbol.for('react.element'),
27+
type: 'svg',
28+
ref: ref,
29+
key: null,
30+
props: Object.assign({}, props, {
31+
children: ${assetFilename}
32+
})
33+
};
34+
}),
2635
};`;
2736
}
2837

packages/react-scripts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"babel-loader": "8.0.5",
3838
"babel-plugin-named-asset-import": "^0.3.2",
3939
"babel-preset-react-app": "^8.0.0",
40+
"camelcase": "^5.2.0",
4041
"case-sensitive-paths-webpack-plugin": "2.2.0",
4142
"css-loader": "2.1.1",
4243
"dotenv": "6.2.0",

0 commit comments

Comments
 (0)