webpack中的babel-loader


本文作者: jsweibo

本文链接: https://jsweibo.github.io/2019/07/10/webpack%E4%B8%AD%E7%9A%84babel-loader/

摘要

本文主要讲述了:

  1. 作用
  2. 安装
  3. 配置

正文

作用

webpack中使用babel

安装

1
2
3
4
#!/usr/bin/env bash

npm install --save-dev babel-loader @babel/core @babel/preset-env
npm install core-js regenerator-runtime

配置

babel-loader的配置可以写在webpack.config.js里,也可以写在babel.config.js里。

示例:

learn_webpack/webpack.config.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module.exports = {
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: [
{
loader: 'babel-loader',
},
],
},
],
},
};

learn_webpack/babel.config.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
ie: 9,
},
useBuiltIns: 'usage',
corejs: 3,
},
],
],
plugins: [],
};

示例:

learn_webpack/webpack.config.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module.exports = {
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: [
{
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
targets: {
ie: 9,
},
useBuiltIns: 'usage',
corejs: 3,
},
],
],
plugins: [],
},
},
],
},
],
},
};

参考资料

本文作者: jsweibo

本文链接: https://jsweibo.github.io/2019/07/10/webpack%E4%B8%AD%E7%9A%84babel-loader/


本文对你有帮助?请支持我


支付宝
微信