-
Notifications
You must be signed in to change notification settings - Fork 7
/
fileTransformer.js
41 lines (38 loc) · 1.06 KB
/
fileTransformer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const path = require("path");
/**
* (thuang): Inspired by https://github.com/cwmoo740/jest-svg-transformer/issues/3#issuecomment-1229928189
* We export as { ReactComponent }, because we import svg that way
* E.g., import { ReactComponent as IconBacteriaLarge }
*/
module.exports = {
process(_, filePath) {
if (path.extname(filePath) !== ".svg") {
return {
code: `module.exports = ${JSON.stringify(path.basename(filePath))};`,
};
}
const name = `${path.basename(filePath, ".svg")}`
.split(/\W+/)
.map((x) => `${x.charAt(0).toUpperCase()}${x.slice(1)}`)
.join("");
return {
code: `
const React = require('react');
function ${name}(props) {
return React.createElement(
'svg',
Object.assign(
{},
props,
{
'data-file-name': ${name}.name,
'data-testid': ${name}.name,
}
)
);
}
module.exports = { ReactComponent: ${name} };
`,
};
},
};