Let's take a look at the code file structure first:Contents of the entry file (index1.js):import $ from 'jquery' import './css/index.css' import './less/index.less' $(function () { $('#app li:nth-child(odd)').css('color', 'red') $('#app li:nth-child(even)').css('color', 'green') }) import './assets/fonts/iconfont.css'; const ul = document.querySelector("ul"); const theI = document.createElement("li"); theI.className='iconfont icon-qq'; ul.appendChild(theI); Contents of the webpack.config.js configuration file:const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { //Entry file address entry: './src/index1.js', output: { path: path.resolve(__dirname, 'dist'), //Export file name filename: 'bundle.js', }, plugins: [ new HtmlWebpackPlugin({ template: './public/interlaced color.html' })], module: { rules: [{ //Match files ending with .css, i is case insensitivetest: [/\.css$/i], //Execute from right to left, the order cannot be changed. Style-loader inserts CSS into DOM, and css-loader processes @import and url(), just like js parses import/require(). use: ["style-loader", "css-loader"], }, { test: /\.less$/i, use: [ // compiles Less to CSS 'style-loader', 'css-loader', 'less-loader', ], }, { // webpack5 does not recognize these files by default, so just output them as static resources test: /\.(eot|svg|ttf|woff|woff2)$/, type: 'asset/resource', generator: { filename: 'font/[name].[hash:6][ext]' } }], }, }; We package and then run the packaged html file: It is found that the css style is added in the form of style tags generated by js After running the package, we will find that less is converted to a css file, but the css file is added with a style tag through js. Next, we will split the css out and introduce it with a link tag step:1. Install mini-css-extract-pluginnpm i mini-css-extract-plugin -D //npm installation yarn add mini-css-extract-plugin -D //yarn installation 2. Introduce and configure in webpack.config.js fileconst path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); //Introduce the installed mini-css-extract-plugin const MiniCssExtractPlugin = require("mini-css-extract-plugin"); module.exports = { //Entry file address entry: './src/index1.js', output: { path: path.resolve(__dirname, 'dist'), //Export file name filename: 'bundle.js', }, plugins: [new MiniCssExtractPlugin(), new HtmlWebpackPlugin({ template: './public/interlaced color.html' })], module: { rules: [{ //Match files ending with .css, i is case insensitivetest: [/\.css$/i], //Execute from right to left, the order cannot be changed. Style-loader inserts CSS into DOM, and css-loader processes @import and url(), just like js parses import/require(). use: [MiniCssExtractPlugin.loader, "css-loader" ], }, { test: /\.less$/i, use: [ // compiles Less to CSS MiniCssExtractPlugin.loader, 'css-loader', 'less-loader', ], }, { // webpack5 does not recognize these files by default, so just output them as static resources test: /\.(eot|svg|ttf|woff|woff2)$/, type: 'asset/resource', generator: { filename: 'font/[name].[hash:6][ext]' } }], }, }; Notice:
3. Compress the split CSS files
const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); //Introduce the installed mini-css-extract-plugin const MiniCssExtractPlugin = require("mini-css-extract-plugin"); //CSS used to compress the split const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); module.exports = { //Entry file address entry: './src/index1.js', output: { path: path.resolve(__dirname, 'dist'), //Export file name filename: 'bundle.js', }, plugins: [new MiniCssExtractPlugin(),new OptimizeCSSAssetsPlugin({}), new HtmlWebpackPlugin({ template: './public/interlaced color.html' })], module: { rules: [{ //Match files ending with .css, i is case insensitivetest: [/\.css$/i], //Execute from right to left, the order cannot be changed. Style-loader inserts CSS into DOM, and css-loader processes @import and url(), just like js parses import/require(). use: [MiniCssExtractPlugin.loader, "css-loader" ], }, { test: /\.less$/i, use: [ // compiles Less to CSS MiniCssExtractPlugin.loader, 'css-loader', 'less-loader', ], }, { // webpack5 does not recognize these files by default, so just output them as static resources test: /\.(eot|svg|ttf|woff|woff2)$/, type: 'asset/resource', generator: { filename: 'font/[name].[hash:6][ext]' } }], } }; 4. PackagingI found an extra main.css file and opened the webpage to view it: The main.css file is imported as a link and compressed. This is the end of this article about webpack splitting and compressing CSS and importing it with link. For more relevant webpack splitting and compressing CSS content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed tutorial on running multiple Springboot with Docker
>>: HTML table markup tutorial (15): table title
Today I learned to install MySQL, and some proble...
Table of contents Overview 1. How to animate a DO...
1. Always close HTML tags In the source code of p...
This article originated from my complaints about ...
Today a client wants to run an advertisement, and ...
Think about it: Why should css be placed in the h...
Table of contents Login business process Login fu...
What is my.ini? my.ini is the configuration file ...
The following is an introduction to using SQL que...
Today we are going to make origami airplanes (the...
Table of contents Preface: Kind tips: variable 1....
This article example shares the specific code of ...
1. Introduction By enabling the slow query log, M...
Beginners can learn HTML by understanding some HT...
Preface: Basically, whether it is for our own use...