Detailed example of how Webpack actually loads SVG
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:
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.
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.
SVG with the same graphics has better rendering performance than the corresponding high-definition graphics.
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); }
can also be used in HTML:
<img src="./svgs/activity.svg"/>
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: ['file-loader'] } ] }, };
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 './svgs/alert.svg';
The output code after raw-loader processing is as follows:
module.exports = "<svg xmlns=\"http://www.w3.org/2000/svg\"... </svg>" // 末尾省略 SVG 内容
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('app').innerHTML = svgContent;
The relevant Webpack configuration when using raw-loader is as follows:
module.exports = { module: { rules: [ { test: /\.svg$/, use: ['raw-loader'] } ] } };
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>
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>
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: ['svg-inline-loader'] } ] } };
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics


![Error loading plugin in Illustrator [Fixed]](https://img.php.cn/upload/article/000/465/014/170831522770626.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
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

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

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

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.

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.

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 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

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.
