测试代码
//index.js
const arr = [new Promise(() => {}), new Promise(() => {})];
arr.map(item => {
console.log(item);
});
npm i babel-loader @babel/core @babel/preset-env -D
配置webpack.config.js
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"]
}
}
}
npm install --save @babel/polyfill
//index.js 顶部
import "@babel/polyfill";
但是在添加运行后,会发现生成的js文件非常大,但是你只可能只需要转化几行ES6代码。
options: {
presets: [
[
"@babel/preset-env",
{
targets: {
edge: "17",
firefox: "60",
chrome: "67",
safari: "11.1",
},
corejs: 2, //新版本需要指定核⼼库版本
useBuiltIns: "usage", //按需注⼊
},
],
];
}
// .bebelrc
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"edge": "17",
"firefox": "60",
"chrome": "67",
"safari": "11.1"
},
"corejs": 2, //新版本需要指定核⼼库版本
"useBuiltIns": "usage" //按需注⼊
}
],
"@babel/preset-react"
]
}
// webpack.config.js
{
test: /\.js$/,
exclude: /node_modules/, // 排除
use: {
loader: "babel-loader",
},
},
配置React打包环境
安装
npm install react react-dom --save
//index.js
import React, { Component } from "react";
import ReactDom from "react-dom";
class App extends Component {
render() {
return hello world;
}
}
ReactDom.render( , document.getElementById("app"));
安装babel与react转换的插件:
npm install --save-dev @babel/preset-react
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"edge": "17",
"firefox": "60",
"chrome": "67",
"safari": "11.1"
},
"corejs": 2, //新版本需要指定核⼼库版本
"useBuiltIns": "usage" //按需注⼊
}
],
"@babel/preset-react"
]
}
1.如果是库的作者的话,提供模块的时候代码怎么打包的?
2.构建速度会越来越慢,怎么优化
本文主要配置webpack内的Babel处理ES6操作及实例展示,及webpack配置React打包环境。