Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: 858 Index template improvements #861

Merged
merged 1 commit into from
May 9, 2023

Conversation

balsick
Copy link
Contributor

@balsick balsick commented Apr 20, 2023

index-template now receives an array of objects containing both the created component path (path) and the original svg path (originalPath).
It is a response to issue #858.

Summary

As before-mentioned, it responds to issue #858, that is, allow index templates to know the original file handled's path.

Test plan

You can use this template. It should create an index.js file containing an object map containing the mapping between the original svg file name and the react component.

const path = require('path');

function defaultIndexTemplate(filePaths) {
  const entries = filePaths.map(({path: filePath, originalPath}) => {
    const originalFileName = path.basename(
      originalPath,
      path.extname(originalPath),
    );
    const basename = path.basename(filePath, path.extname(filePath));
    const exportName = /^\d/.test(basename) ? `Svg${basename}` : basename;
    const importLine = `import ${exportName} from './${basename}';`;
    const mapLine = `${
      /.*[.-].*/.test(originalFileName)
        ? `'${originalFileName}'`
        : originalFileName
    }: ${exportName}`;
    return [importLine, mapLine];
  });
  return `${entries.map(([e]) => e).join('\n')}
export const map = {
${entries.map(([, e]) => e).join(',\n')}
}
`;
}

module.exports = defaultIndexTemplate;

index-template now receives an array of objects containing both the created
component path (`path`) and the original svg path (`originalPath`)
@vercel
Copy link

vercel bot commented Apr 20, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
svgr ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 20, 2023 3:04pm

@codecov
Copy link

codecov bot commented Apr 24, 2023

Codecov Report

Merging #861 (b8059fe) into main (428b0c7) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main     #861   +/-   ##
=======================================
  Coverage   92.82%   92.82%           
=======================================
  Files          32       32           
  Lines         753      753           
  Branches      250      250           
=======================================
  Hits          699      699           
  Misses         53       53           
  Partials        1        1           

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants