This time I will show you how to extract css styles in webpack, and what are the precautions for extracting css styles in webpack. The following is a practical case, let's take a look.
npm install extract-text-webpack-plugin --save-dev # for webpack 2 npm install --save-dev extract-text-webpack-plugin # for webpack 1 npm install --save-dev extract-text-webpack-plugin@1.0.1
First enter the root directory of the project, and then execute the above command to install the plug-in. After the plug-in installation is completed, the next thing we have to do is to introduce the plug-in in webpack.config.js
const ExtractTextPlugin = require("extract-text-webpack-plugin"); module.exports = { module: { rules: [ { test: /\.css$/, use: ExtractTextPlugin.extract({ fallback: "style-loader", use: "css-loader" }) } ] }, plugins: [ new ExtractTextPlugin("styles.css"), ] }
const ExtractTextPlugin = require('extract-text-webpack-plugin'); // Create multiple instances const extractCSS = new ExtractTextPlugin('stylesheets/[name]-one.css'); const extractLESS = new ExtractTextPlugin('stylesheets/[name]-two.css'); module.exports = { module: { rules: [ { test: /\.css$/, use: extractCSS.extract([ 'css-loader', 'postcss-loader' ]) }, { test: /\.less$/i, use: extractLESS.extract([ 'css-loader', 'less-loader' ]) }, ] }, plugins: [ extractCSS, extractLESS ] };
The plug-in has three parameters with the following meanings
use: refers to what kind of loader is needed to compile the file. Since the source file is .css, css-loader is selected here
fallback: What loader is used to extract the css file after compilation
publicfile: used to overwrite the project path and generate the file path of the css file
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to customize the content of ElTableColumn
Return to the homepage after developing a small program and sharing the page
The above is the detailed content of How to extract css styles in webpack. For more information, please follow other related articles on the PHP Chinese website!