-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsvgr.config.js
96 lines (88 loc) · 2.85 KB
/
svgr.config.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* eslint-disable global-require */
const path = require('path');
const template = require('@babel/template');
const indexTemplate = (filePaths) => {
const exportEntries = filePaths.map(({ path: filePath }) => {
const basename = path.basename(filePath, path.extname(filePath));
const exportName = `${basename.replace(/react/i, 'Icon')}`;
return `export { default as ${exportName} } from './${basename}'`;
});
return [
`// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file is created with 'yarn generate-icons'.`,
]
.concat(exportEntries)
.join('\n');
};
const annotationsToRemove = [
'/* eslint-disable @typescript-eslint/no-unused-vars */',
'// @ts-ignore',
];
// https://react-svgr.com/docs/custom-templates/
// https://github.com/gregberge/svgr/blob/c57ee04b19c15a76ae4caf40d1bb82c210d6c398/packages/babel-plugin-transform-svg-component/src/types.ts#L18
const styledIconsTemplate = (variables, context) => {
const svgComponentName = context.options.state.componentName.replace(
/react/i,
''
);
const reactComponentName = `${svgComponentName.replace(/svg/i, '')}Icon`;
const babelOptions = {
preserveComments: true,
placeholderPattern: false,
placeholderWhitelist: new Set(['JSX']),
plugins: ['jsx', 'typescript'],
};
const templateOptions = { JSX: variables.jsx };
const code = `// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
// This file is created with 'yarn generate-icons'.
// Original SVG file: ${context.options.state.filePath}
import { ClassNames } from '@emotion/react';
import {
getIconStyles,
type TIconProps,
type TSVGProps,
} from '@commercetools-uikit/design-system';
const ${svgComponentName} = (props: TSVGProps) => JSX;
${svgComponentName}.displayName = "${svgComponentName}";
const ${reactComponentName} = (props: TIconProps) => (
<ClassNames>
{({ css: createClass }) =>
<${svgComponentName} {...props} className={createClass(getIconStyles(props))} />
}
</ClassNames>
);
${reactComponentName}.displayName = "${reactComponentName}";
export default ${reactComponentName};
`;
const codeWithoutUnnecessaryAnnotations = annotationsToRemove.reduce(
(finalStringTemplate, annotationToRemove) =>
finalStringTemplate.replace(new RegExp(annotationToRemove, 'g'), ''),
code
);
return template.smart(
codeWithoutUnnecessaryAnnotations,
babelOptions
)(templateOptions);
};
module.exports = {
icon: false,
svgoConfig: {
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeViewBox: false,
},
},
},
'prefixIds',
],
},
svgProps: { 'aria-hidden': 'true' },
typescript: true,
// same as the rollup plugin
plugins: ['@svgr/plugin-svgo', '@svgr/plugin-jsx'],
indexTemplate,
template: styledIconsTemplate,
};