Skip to content

Commit

Permalink
fix(jest): fix convert svg file import issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Apr 3, 2023
1 parent a0bb654 commit b89279f
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion packages/jest/src/transform/file.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import path from 'path';
// import camelcase from 'camelcase';
import camelcase from 'camelcase';
// This is a custom Jest transformer turning file imports into filenames.
// http://facebook.github.io/jest/docs/en/webpack.html

export default {
process(sourceText: string, sourcePath: string, options: any) {
const assetFilename = JSON.stringify(path.basename(sourcePath));
if (sourcePath.match(/\.svg$/)) {
// Based on how SVGR generates a component name:
// https://github.com/smooth-code/svgr/blob/01b194cf967347d43d4cbe6b434404731b87cf27/packages/core/src/state.js#L6
const pascalCaseFilename = camelcase(path.parse(sourcePath).name, {
pascalCase: true,
});
const componentName = `Svg${pascalCaseFilename}`;
return `const React = require('react');
module.exports = {
__esModule: true,
default: ${assetFilename},
ReactComponent: React.forwardRef(function ${componentName}(props, ref) {
return {
$$typeof: Symbol.for('react.element'),
type: 'svg',
ref: ref,
key: null,
props: Object.assign({}, props, {
children: ${assetFilename}
})
};
}),
};`;
}
return {
code: `module.exports = ${JSON.stringify(path.basename(sourcePath))};`,
};
Expand Down

0 comments on commit b89279f

Please sign in to comment.