babel
解析ES6
- 下载包
npm i @babel/preset-core @babel/preset-env babel-loader -D```javascript // webpack.config.js ‘use strict’; const path = require(‘path’);
 
module.exports = { entry: { index: ‘./src/index.js’, search: ‘./src/search.js’, }, output: { path: path.join(__dirname, ‘dist’), filename: ‘[name].js’, }, module: { rules: [ { test: /.js$/, use: ‘babel-loader’, } ] }, mode: ‘production’ }
```json{"presets": ["@babel/preset-env",]}
解析ReactJSX
在上面的安装的基础上,继续下载包
css-loader 用于加载
.css文件,并转换成commonjs对象- style-loader 将样式通过
<style>标签插入head中 - 下载包
npm i style-loader css-loader -D```javascript // webpack.config.js ‘use strict’; const path = require(‘path’);
 
module.exports = { entry: { index: ‘./src/index.js’, search: ‘./src/search.js’, }, output: { path: path.join(__dirname, ‘dist’), filename: ‘[name].js’, }, module: { rules: [ { test: /.js$/, use: ‘babel-loader’, }, { test: /.css$/, use: [‘style-loader’, ‘css-loader’] }, ] }, mode: ‘production’ }
<a name="DJBBw"></a>## 解析less和sass- 解析less- 下载包:`npm i less less-loader -D`- 作用将:less转为css```javascript'use strict';const path = require('path');module.exports = {entry: {index: './src/index.js',search: './src/search.js',},output: {path: path.join(__dirname, 'dist'),filename: '[name].js',},module: {rules: [{test: /\.js$/,use: 'babel-loader',},{test: /\.css$/,use: ['style-loader', 'css-loader']},{test: /\.less$/,use: ['style-loader', 'css-loader', 'less-loader'],}]},mode: 'production'}
- 解析sass
- 下载包:
npm i sass sass-loader -D webpack.config.js配置同理
 - 下载包:
 
file-loader
- 作用:用于处理文件
 - 下载包:
npm i file-loader -D解析图片
```javascript // webpack.config.js ‘use strict’; const path = require(‘path’); 
module.exports = { entry: { index: ‘./src/index.js’, search: ‘./src/search.js’, }, output: { path: path.join(__dirname, ‘dist’), filename: ‘[name].js’, }, module: { rules: [ { test: /.js$/, use: ‘babel-loader’, }, { test: /.css$/, use: [‘style-loader’, ‘css-loader’] }, { test: /.less$/, use: [‘style-loader’, ‘css-loader’, ‘less-loader’], }, { test: /.(scss|sass)$/, use: [‘style-loader’, ‘css-loader’, ‘sass-loader’], }, { test: /.(png|jpg|gif|jpeg|svg)$/, use: [‘file-loader’] }, ] }, mode: ‘production’ }
<a name="PwsCq"></a>## 解析字体```javascript// webpack.config.js'use strict';const path = require('path');module.exports = {entry: {index: './src/index.js',search: './src/search.js',},output: {path: path.join(__dirname, 'dist'),filename: '[name].js',},module: {rules: [{test: /\.js$/,use: 'babel-loader',},{test: /\.css$/,use: ['style-loader', 'css-loader']},{test: /\.less$/,use: ['style-loader', 'css-loader', 'less-loader'],},{test: /\.(scss|sass)$/,use: ['style-loader', 'css-loader', 'sass-loader'],},{test: /\.(png|jpg|gif|jpeg|svg)$/,use: ['file-loader']},{test: /\.(woff|woff2|eot|ttf|otf)$/,use: ['file-loader']}]},mode: 'production'}
url-loader处理图片、字体
- 可设置较小资源自动转成base64
 - 据说其内部也用了file-loader,但其实删掉file-loader一样能打包 ```javascript ‘use strict’; const path = require(‘path’);
 
module.exports = { entry: { index: ‘./src/index.js’, search: ‘./src/search.js’, }, output: { path: path.join(__dirname, ‘dist’), filename: ‘[name].js’, }, module: { rules: [ { test: /.js$/, use: ‘babel-loader’, }, { test: /.css$/, use: [‘style-loader’, ‘css-loader’] }, { test: /.less$/, use: [‘style-loader’, ‘css-loader’, ‘less-loader’], }, { test: /.(scss|sass)$/, use: [‘style-loader’, ‘css-loader’, ‘sass-loader’], }, { test: /.(png|jpg|gif|jpeg|svg)$/, use: [ { loader: ‘url-loader’, options: { limit: 30000 } } ] }, { test: /.(woff|woff2|eot|ttf|otf)$/, use: [ { loader: ‘url-loader’, options: { limit: 70000 } } ] } ] }, mode: ‘production’ } ```
