Home Web Front-end JS Tutorial Detailed example of how Webpack actually loads SVG

Detailed example of how Webpack actually loads SVG

Dec 27, 2017 pm 01:15 PM
web webpack load

This article mainly introduces the actual method of loading SVG with Webpack. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

As a standard format for vector graphics, SVG has been supported by major browsers, and it has also become synonymous with vector graphics on the Web. Using SVG instead of bitmaps in web pages has the following benefits:

  1. SVG is clearer than bitmaps and will not destroy the clarity of graphics under arbitrary scaling. SVG can facilitate It effectively solves the problem of unclear image display on high-resolution screens.

  2. When the graphic lines are relatively simple, the size of the SVG file is smaller than the bitmap. Today, when flat UI is popular, SVG will be smaller in most cases.

  3. SVG with the same graphics has better rendering performance than the corresponding high-definition graphics.

  4. SVG uses XML syntax description consistent with HTML, and is highly flexible.

The drawing tool can export .svg files one by one. The import method of SVG is similar to that of pictures. It can be used directly in CSS as follows:


body {
 background-image: url(./svgs/activity.svg);
}
Copy after login

can also be used in HTML:


<img src="./svgs/activity.svg"/>
Copy after login

That is to say, the SVG file can be used directly as a picture. The method is as follows It's exactly the same when using images. Therefore, the two methods introduced in 3-19 Loading Images using file-loader and using url-loader are equally effective for SVG. You only need to change the file suffix in the Loader test configuration to .svg. The code is as follows:


module.exports = {
 module: {
  rules: [
   {
    test: /\.svg/,
    use: [&#39;file-loader&#39;]
   }
  ]
 },
};
Copy after login

Since SVG is a text format file, there are other methods besides the above two methods, which will be explained one by one below.

Use raw-loader

raw-loader can read the contents of the text file and inject it into JavaScript or CSS.

For example, write this in JavaScript:


import svgContent from &#39;./svgs/alert.svg&#39;;
Copy after login

The output code after raw-loader processing is as follows:

module.exports = "<svg xmlns=\"http://www.w3.org/2000/svg\"... </svg>" // 末尾省略 SVG 内容
Copy after login


That is to say, the content of svgContent is equal to SVG in the form of a string. Since SVG itself is an HTML element, after obtaining the SVG content, you can directly insert the SVG into the web page through the following code:


window.document.getElementById(&#39;app&#39;).innerHTML = svgContent;
Copy after login

The relevant Webpack configuration when using raw-loader is as follows:


module.exports = {
 module: {
  rules: [
   {
    test: /\.svg$/,
    use: [&#39;raw-loader&#39;]
   }
  ]
 }
};
Copy after login

Because raw-loader will directly return the text content of SVG , and the text content of SVG cannot be displayed through CSS, so SVG cannot be imported into CSS after using this method. That is to say, code such as background-image: url(./svgs/activity.svg) cannot appear in CSS, because background-image: url(...) is illegal of.

This example provides the complete code of the project

Using svg-inline-loader

svg-inline-loader and the raw-loader mentioned above are very Similar, but the difference is that svg-inline-loader will analyze the content of SVG and remove unnecessary code to reduce the file size of SVG.

After using drawing tools such as Adobe Illustrator and Sketch to create SVG, these tools will generate unnecessary code for the web page to run when exporting. For example, the following is the SVG code exported by Sketch:


<svg class="icon" verison="1.1" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"
   stroke="#000">
 <circle cx="12" cy="12" r="10"/>
</svg>
Copy after login

will be simplified to the following after being processed by svg-inline-loader:


<svg viewBox="0 0 24 24" stroke="#000"><circle cx="12" cy="12" r="10"/></svg>
Copy after login

That is to say, svg-inline-loader adds a compression function for SVG.

The relevant Webpack configuration when using svg-inline-loader is as follows:


module.exports = {
 module: {
  rules: [
   {
    test: /\.svg$/,
    use: [&#39;svg-inline-loader&#39;]
   }
  ]
 }
};
Copy after login

This example provides the complete code of the project

Related recommendations:

Detailed example of Webpack optimization configuration to narrow the file search scope

Detailed explanation of the core concepts of Webpack framework

Detailed explanation of webpack+express multi-page site development examples

The above is the detailed content of Detailed example of how Webpack actually loads SVG. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Error loading plugin in Illustrator [Fixed] Error loading plugin in Illustrator [Fixed] Feb 19, 2024 pm 12:00 PM

When launching Adobe Illustrator, does a message about an error loading the plug-in pop up? Some Illustrator users have encountered this error when opening the application. The message is followed by a list of problematic plugins. This error message indicates that there is a problem with the installed plug-in, but it may also be caused by other reasons such as a damaged Visual C++ DLL file or a damaged preference file. If you encounter this error, we will guide you in this article to fix the problem, so continue reading below. Error loading plug-in in Illustrator If you receive an "Error loading plug-in" error message when trying to launch Adobe Illustrator, you can use the following: As an administrator

Stremio subtitles not working; error loading subtitles Stremio subtitles not working; error loading subtitles Feb 24, 2024 am 09:50 AM

Subtitles not working on Stremio on your Windows PC? Some Stremio users reported that subtitles were not displayed in the videos. Many users reported encountering an error message that said "Error loading subtitles." Here is the full error message that appears with this error: An error occurred while loading subtitles Failed to load subtitles: This could be a problem with the plugin you are using or your network. As the error message says, it could be your internet connection that is causing the error. So please check your network connection and make sure your internet is working properly. Apart from this, there could be other reasons behind this error, including conflicting subtitles add-on, unsupported subtitles for specific video content, and outdated Stremio app. like

PHP implements infinite scroll loading PHP implements infinite scroll loading Jun 22, 2023 am 08:30 AM

With the development of the Internet, more and more web pages need to support scrolling loading, and infinite scrolling loading is one of them. It allows the page to continuously load new content, allowing users to browse the web more smoothly. In this article, we will introduce how to implement infinite scroll loading using PHP. 1. What is infinite scroll loading? Infinite scroll loading is a method of loading web content based on scroll bars. Its principle is that when the user scrolls to the bottom of the page, background data is asynchronously retrieved through AJAX to continuously load new content. This kind of loading method

Outlook freezes when inserting hyperlink Outlook freezes when inserting hyperlink Feb 19, 2024 pm 03:00 PM

If you encounter freezing issues when inserting hyperlinks into Outlook, it may be due to unstable network connections, old Outlook versions, interference from antivirus software, or add-in conflicts. These factors may cause Outlook to fail to handle hyperlink operations properly. Fix Outlook freezes when inserting hyperlinks Use the following fixes to fix Outlook freezes when inserting hyperlinks: Check installed add-ins Update Outlook Temporarily disable your antivirus software and then try creating a new user profile Fix Office apps Program Uninstall and reinstall Office Let’s get started. 1] Check the installed add-ins. It may be that an add-in installed in Outlook is causing the problem.

How to solve the problem that css cannot be loaded How to solve the problem that css cannot be loaded Oct 20, 2023 am 11:29 AM

The solutions to the problem that CSS cannot be loaded include checking the file path, checking the file content, clearing the browser cache, checking the server settings, using developer tools and checking the network connection. Detailed introduction: 1. Check the file path. First, please make sure the path of the CSS file is correct. If the CSS file is located in a different part or subdirectory of the website, you need to provide the correct path. If the CSS file is located in the root directory, the path should be direct. ; 2. Check the file content. If the path is correct, the problem may lie in the CSS file itself. Open the CSS file to check, etc.

How to implement form validation for web applications using Golang How to implement form validation for web applications using Golang Jun 24, 2023 am 09:08 AM

Form validation is a very important link in web application development. It can check the validity of the data before submitting the form data to avoid security vulnerabilities and data errors in the application. Form validation for web applications can be easily implemented using Golang. This article will introduce how to use Golang to implement form validation for web applications. 1. Basic elements of form validation Before introducing how to implement form validation, we need to know what the basic elements of form validation are. Form elements: form elements are

Using Jetty7 for Web server processing in Java API development Using Jetty7 for Web server processing in Java API development Jun 18, 2023 am 10:42 AM

Using Jetty7 for Web Server Processing in JavaAPI Development With the development of the Internet, the Web server has become the core part of application development and is also the focus of many enterprises. In order to meet the growing business needs, many developers choose to use Jetty for web server development, and its flexibility and scalability are widely recognized. This article will introduce how to use Jetty7 in JavaAPI development for We

What are web standards? What are web standards? Oct 18, 2023 pm 05:24 PM

Web standards are a set of specifications and guidelines developed by W3C and other related organizations. It includes standardization of HTML, CSS, JavaScript, DOM, Web accessibility and performance optimization. By following these standards, the compatibility of pages can be improved. , accessibility, maintainability and performance. The goal of web standards is to enable web content to be displayed and interacted consistently on different platforms, browsers and devices, providing better user experience and development efficiency.

See all articles