简体   繁体   English

有效地在 create-react-app 中导入多个相似的 svg 源?

[英]Import multiple similar svg sources in create-react-app efficiently?

I'm going to import a bunch of SVG illustrations into my project as inline SVG components using the new feature of create-react-app @2.0.0.我将使用 create-react-app @2.0.0 的新功能将一堆 SVG 插图作为内联 SVG 组件导入到我的项目中。

Here is my svg-import.js:这是我的 svg-import.js:

import React from 'react';

import { ReactComponent as StartLogo } from '../images/start/logo.svg';
import { ReactComponent as StartTitle } from '../images/start/title.svg';
...

import { ReactComponent as IntroAvatar } from '../images/intro/avatar.svg';
import { ReactComponent as IntroDialog } from '../images/intro/dialog.svg';
...

const svgIllustrations = {
  "start": {
    "logo": <StartLogo className="svg svg-start-logo"/>,
    "title": <StartTitle className="svg svg-start-title"/>,
    ...
  },
  "intro": {
    "avatar": <IntroAvatar className="svg svg-intro-avatar"/>,
    "dialog": <IntroDialog className="svg svg-intro-dialog"/>,
    ...
  }
}

export default svgIllustrations;

When I want to use them:当我想使用它们时:

import svgIllustrations from '../svg-import.js';

...
render() {
  return (
    {svgIllustrations.start["logo"]}
  )
}

The code works.该代码有效。

However, since there're lots of SVG files to be imported and all of them are named and structured under a certain rule, I'm wondering if there's a better way to do something like this.但是,由于要导入大量 SVG 文件,并且所有这些文件都根据特定规则命名和结构化,因此我想知道是否有更好的方法来执行此类操作。

If it were me personally, I probably would do away with the intermediary svgIllustrations and just import the svgs directly from the images directory wherever I want to use them.如果是我个人,我可能会取消中间的 svgIllustrations,直接从我想使用的图像目录中导入 svgs。 Your object basically replicates the file structure, so I'm not seeing what pulling all the svg's in a single place is really buying you.您的对象基本上复制了文件结构,所以我没有看到将所有 svg 拉到一个地方真正为您买单的是什么。 Importing them directly also might help when it comes to code splitting;在代码拆分方面,直接导入它们也可能有所帮助。 in the current setup you propose, you have to load all of the svg's in order to display one of them.在您建议的当前设置中,您必须加载所有 svg 才能显示其中之一。 If you import them directly from the images directory, you could split out a page and you would only be loading the svg's for that page.如果您直接从图像目录导入它们,您可以拆分一个页面,并且您只会加载该页面的 svg。 This may not be a concern depending on the size of your app and the number of svg's you are loading.这可能不是问题,具体取决于您的应用程序的大小和您正在加载的 svg 数量。

Having said that, if you still really want this object representation for all your svg's, the only thing I would say you might want to do is generate your svgIllustrations from your images directory.话虽如此,如果您仍然真的想要所有 svg 的对象表示,我想说您可能想做的唯一一件事就是从您的图像目录生成您的 svgIllustrations。 Since your object structure is basically a replica of your file structure, it would likely be pretty easy to do so and maybe save you a little bit of time.由于您的对象结构基本上是文件结构的副本,因此这样做可能非常容易,并且可能会为您节省一点时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM