Skip to content

Use the Svg prefix globally #667

@advename

Description

@advename

🚀 Feature Proposal

Keep it consistent and use the Svg prefix globally for:

  • the file name, e.g. smiley.svg -> SvgSmiley.jsx
  • the component name, e.g. SvgSmiley() (already implemented)
  • the index.js exports, e.g. instead of export { default as Smiley } from './Smiley';, do export { default as SvgSmiley } from './Smiley';

Motivation

Regarding #190, all Component names should be prefixed with Svg.
#383 mentions that the index.jsonly exports with Svg prefix if the component name starts with a number.
I know that it is possible to alias an import, but it's confusing for a new user to understand when/or when not the prefix is used.
End-users of this tool might have to run additional processing. First, after many trials and errors have I noticed the inconsistency.

Monkey Patch

Currently, I have to use a custom index template, e.g.

// svgr.config.js

const path = require('path');

/**
 * Custom index.js template
 */
function defaultIndexTemplate(filePaths) {
  const exportEntries = filePaths.map((filePath) => {
    const basename = path.basename(filePath, path.extname(filePath));
    const exportName = /^\d/.test(basename) ? `Svg${basename}` : basename;
    return `export { default as Svg${exportName} } from './${basename}'`;
  });
  return exportEntries.join('\n');
}

module.exports = {
  indexTemplate: defaultIndexTemplate,
};

and run a command to rename all generated component files with an Svg prefix.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions