Webpack parsing image, file-loader; and the difference between url-loader parsing

tags: webpack

Note: Before installing, make sure the webpack project works properly!

1. Install file-loader dependency
cnpm i file-loader -D
2. Create an img folder in the src directory and import a picture (the image file name is called logo.jpg)
3. Configure, add a new configuration under webpack.config.js:
module.exports = {
  // ...
  module:{
    rules:[
      // ...
      {
        test: /.(png|jpg|gif|jpeg)$/,
        use:'file-loader'
      }
    ]
  }
}

4. Use, import images in search.js
import ReactDom from 'react-dom';
import './search.css';
import './search.less';
Import logo from './img/logo.jpg'; // introduction

class Search extends React.Component {
  render() {
    return <div className="search-txt">
      Search Text
             <img src={ logo } /> // use
    </div>;
  }
};

ReactDom.render(
  <Search/>,
  document.getElementById('root')
);
5.file-loader and url-loader parse the difference between images

In fact, the two functions are similar. The url-loader is actually packaged on the basis of the file-loader, and the base64 conversion is automatically performed for the picture and font files of the limit size.

5.1 Installation url-loader
cnpm i url-loader -D
5.2 Under webpack.config.js, modify the file-loader configuration to url-loader
module.exports = {
  // ...
  module:{
    rules:[
      // ...
      {
        test: /.(png|jpg|gif|jpeg)$/,
        use:[
        	{
				loader: 'url-loader',
	        	options:{
	        		 Limit: 10240 // The unit of limit is byte. Only when it is less than 10k, it is allowed to automatically transfer to base64.
	        	}
			}
        ]
      }
    ]
  }
}

Intelligent Recommendation

15. Webpack configuration vue-loader, parsing vue file

Article Directory Install the loader related to the vue file webpack about Vue related configuration Define the main.js startup file and load the App.vue file Install the loader related to the vue fil...

webpack learning file-loader, url-loader, css-loader

Installation webpack Webpack command execution npx command that the implementation of the project in webpack node_modules commands executed directly webpack otherwise, you will need to install a globa...

The difference and use of url-loader and file-loader

Webpack is written in JS and runs in a node environment, so by default, only the dependencies between JS will be processed when webpack is packaged! ! ! If you don’t believe it, you can create a...

The difference between loader and plugin in webpack

Original address:Detailed webpack loader and plugin   First, the common configuration of webpack Second, the packaging principle of webpack Identify entry files By layer-by-layer identification o...

The difference between the loader and plugin of webpack

Interview questions: Do you know the principle of webpack? What configuration did we use webpack in your project? What is the difference between loader and plugin? What are used in the project? 【Loade...

More Recommendation

WebPack uses file-loader and url-loader, the post-packaged picture cannot be viewed and the web image path is incorrect.

contents  File structure Solution File structure I set two DIVs on the page to display the background image, the picture is introduced by the LESS file inside SRC / CSS. Configure the following c...

Webpack packaging image files-url-loader

I. Introduction Second, install the configuration picture related loader and prepare the picture Install url-loader and file-loader: Check the package.json file after installation Related documents: h...

webpack study notes -2-file-loader and url-loader

1 Introduction If we want to introduce the picture page (including the url img src and the background). When we webpack-based development, the introduction of the picture will encounter some problems....

Picture introduction of webpack-enhanced file-loader: url-loader

Foreword: This article introduces url-loader (enhanced file-loader); The role of url-loader: selectively encode some small pictures into base64 format and write them into the page according to the dem...

Does webpack use url-loader or file-loader for images?

You can see that the url-loader is used for image compilation, and some people use file-laoder, so what is the difference between the two? Which one should I use? url-loader: Function: The function of...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top