Webpack loads css files and its configuration method

Webpack loads css files and its configuration method

webpack loads css files and its configuration

  • After we have written several CSS files, if we want to reference them in HTML, our initial method is to import the CSS files through the link tag. However, if we have a lot of CSS files, importing them one by one is not recommended. Now that we have learned webpack, we hope to import all dependent files by packaging the bundle.js file.
  • What we need to know is that if the css file is not imported in the entry function main.js file, then the packaged bundle.js file will definitely not have a css file, so we need to make the main.js file depend on the css file.
  • Use code: require('css file address')
  • After the dependencies are completed, we package it again and run npm run build and we will find an error. The error is: You may need an appropriate loader to handle this file type. This means you may need a suitable loader to process this file.
  • So we opened the Chinese website of webpack and found the two loaders we need to install: css-loader and style-loader. And we also need to configure these loaders in the webpack.config.js file
  • So we use npm to download these two dependencies. The code is as follows: npm install css-loader style-loader --save-dev
  • In the webpack.config.js file, there is a module property, which is an object. In this object, there is a rules property, which is an array. Each item in the array is an object required to process different file loaders. The code is as follows:
const path = require('path');
    module.exports = {
        // In the configuration file, manually specify the entry file and export file mode:'development', // This attribute needs to be added in the webpack4.x version entry:'./src/main.js', // Entry file output:{ // Export file path:path.resolve(__dirname,'dist'), // Specify where the packaged files should be output (note: the path must be an absolute address)
            filename: 'bundle.js' //Specify the file name of the output file},
        module:{
            rules:[
            {test:/\.css$/ , use:['style-loader' , 'css-loader']}
            ]
        }
    }

Test indicates what type of file we want to process, and each item in use is the loader required to process that type of file.

Note: The css-loader is used to let the main.js file load the css file, while the style-loader is used to add the module's exports as styles to the DOM. Some people may have questions here: according to this function, the file should be loaded first and then added to the DOM as a style, so the order in the array should not be like this. I am here to tell you clearly that your idea is not wrong, but webpack is very peculiar in that it loads the loader from the last element of the array, from right to left.

After setting the dependency, downloading the loader and configuring it, we will find that the styles in the CSS file appear after running it.

Summarize

The above is the webpack loading css file and its configuration method introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

<<:  How to use Element in React project

>>:  Pagination Examples and Good Practices

Recommend

Graphic tutorial on configuring log server in Linux

Preface This article mainly introduces the releva...

JavaScript to achieve product query function

This article example shares the specific code of ...

Example of how to install nginx to a specified directory

Due to company requirements, two nginx servers in...

Web page image optimization tools and usage tips sharing

As a basic element of a web page, images are one ...

CSS flexible layout FLEX, media query and mobile click event implementation

flex layout Definition: The element of Flex layou...

A permanent solution to MYSQL's inability to recognize Chinese

In most cases, MySQL does not support Chinese whe...

How to deploy LNMP architecture in docker

Environmental requirements: IP hostname 192.168.1...

How to view the type of mounted file system in Linux

Preface As you know, Linux supports many file sys...

Summary of fragmented knowledge of Docker management

Table of contents 1. Overview 2. Application Exam...

mysql create database, add users, user authorization practical method

1. Create a MySQL database 1. Create database syn...

From CSS 3D to spatial coordinate axis with source code

One time we talked about the dice rolling game. A...

Implementing a simple Gobang game with native JavaScript

This article shares the specific code for impleme...

How to solve the problem that Docker container has no vim command

Find the problem Today, when I tried to modify th...