This time I will bring you the separate packaging of CSS styles in webpack. What are the precautions for packaging CSS styles separately in webpack? The following is a practical case, let's take a look.
Zero, introduction
The following are some basic knowledge summarized in personal projects. Record them here to deepen your impression and let others know. Everyone can understand webpack more quickly and conveniently, and use it. Due to limited capabilities, if there are errors or problems, please help point them out.
Webpack treats all resources as a module. CSS, Image, and JS font files are all resources and can be packaged into a bundle.js file.
The basic use of webpack is very simple, but it would be a lot to explain all aspects, so here we mainly explain how to use webpack to package css separately.
As for packaging, how to add a hash value, how to replace the reference path in HTML, and how to upload to CND can be achieved using gulp. [Write an article later if you are interested]
1. Extract-text-webpack-plugin usage
Package css separately, in webpack You need to use a plug-in, extract-text-webpack-plugin
1. Install extract-text-webpack-plugin
// use npm npm install extract-text-webpack-plugin --save-dev // or use yarn yarn add extract-text-webpack-plugin
2. Configuration
Write the configuration of the plug-in in the loader (what loader to use), and set the name of the extracted CSS file in the plugins of webpack.
var Ex = require('extract-text-webpack-plugin'); // ...省略 module: { loaders: [{ test: /\.less/, loader: Ex.extract('style-loader', 'css-loader','less-loader') // 单独打包出CSS,这里配置注意下 }] }, plugins: [ new Ex("【name】.css") ]
For a little more detail, you can refer to this "Usage and Installation of extract-text-webpack-plugin"
Here are two actual usage examples to facilitate everyone's understanding
2. Single-page application, package the CSS in JS separately
To package a file, you only need to reference the css file in the regular entry js file. To package into multiple CSS files, you can set multiple CSS entries and let webpack use loader to package them. It is the same as splitting and packaging js files separately. Here are two examples.
// webpack 1.x 配置 【早期使用的配置,那时候是1.x】 /* webpack.config.js */ var precss = require('precss'); var cssnext = require('cssnext'); var autoprefixer = require('autoprefixer'); var cssnano = require('cssnano'); var Ex = require('extract-text-webpack-plugin'); module.exports = { entry: './index.js', output: { filename: 'index.js' }, module: { loaders: [{ test: /\.less/, loader: Ex.extract('style-loader', 'css-loader','less-loader') // 单独打包出CSS,这里配置注意下 }] }, plugins: [ new Ex("【name】.css") ] }
3. How webpack packages multiple CSS files
2. Add the corresponding configuration to the configuration file
Provided directly below A completed multi-entry CSS packaging configuration
// webpack 3.x 的配置 var path = require('path') var glob = require('globby') var webpack = require('webpack') var ExtractTextPlugin = require('extract-text-webpack-plugin') // CSS入口配置 var CSS_PATH = { css: { pattern: ['./src/**/[^_]*.less', '!./src/old/**/*.less'], src: path.join(__dirname, 'src'), dst: path.resolve(__dirname, 'static/build/webpack'), } } // 遍历除所有需要打包的CSS文件路径 function getCSSEntries(config) { var fileList = glob.sync(config.pattern) return fileList.reduce(function (previous, current) { var filePath = path.parse(path.relative(config.src, current)) var withoutSuffix = path.join(filePath.dir, filePath.name) previous[withoutSuffix] = path.resolve(__dirname, current) return previous }, {}) } module.exports = [ { devtool: 'cheap-module-eval-source-map', context: path.resolve(__dirname), entry: getCSSEntries(CSS_PATH.css), output: { path: CSS_PATH.css.dst, filename: '[name].css' }, module: { rules: [ { test: /\.less$/, use: ExtractTextPlugin.extract({ use: ['css-loader', 'postcss-loader', 'less-loader'] }) } ] }, resolve: { extensions: ['.less'] }, plugins: [ new ExtractTextPlugin('[name].css'), ] }, // 如果还需要打包js,则可以在这里增加一个单独打包js的处理【根据自己需求来】 // { // entry:{}, // output:{}, // ... 省略 // } ]
Maybe some students are still using webpack1.x, so here is a simple configuration of webpack1.x
// webpack 1.x 版本 // ...其他配置和webpack3.x一样 module: { loaders: [ { test: /\.less$/, loader: ExtractTextPlugin.extract("style-loader","css-loader","postcss-loader","less-loader") }, ] } plugins: [ new ExtractTextPlugin('[name].css'), ] // ...其他配置和webpack3.x一样
I believe you have read the case in this article After mastering the method, please pay attention to other related articles on the php Chinese website for more exciting content!
Recommended reading:
Vue php handles cross-domain issues
apply Math.max() function usage
The above is the detailed content of Package css styles separately in webpack. For more information, please follow other related articles on the PHP Chinese website!

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

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

SublimeText3 Linux new version
SublimeText3 Linux latest version
