基于个人上篇文章,webpack4.0 入门篇 - 构建前端开发的基本环境,补充对babel
进行的一次探究。上篇文章讲叙到的 webpack babel
时几乎一笔带过,因此这篇文章将进行补充说明.前端
Babel 是一个让咱们可以使用 ES 新特性的 JS 编译工具,咱们能够在
webpack
中配置Babel
,以便使用 ES六、ES7 标准来编写 JS 代码。node
本文以当前最新版本的 babel - 7.10 为例, 作 babel
的配置. 相关版本号以下webpack
{
"devDependencies": {
"@babel/core": "^7.1.6",
"@babel/plugin-proposal-decorators": "^7.1.6",
"@babel/plugin-transform-runtime": "^7.1.0",
"@babel/preset-env": "^7.1.6",
"@babel/runtime": "^7.1.5",
"babel-loader": "^8.0.4",
"webpack": "^4.26.1",
"webpack-cli": "^3.1.2"
}
}
复制代码
创建基本的 webpack
配置文件git
mkdir webpack-babel => cd webpack-babel => yarn init -y // 初始化
npm i yarn -g // 安装了yarn能够忽略
yarn add webpack webpack-cli -D
// package.json 中添加:
"scripts": {
"start": "webpack --mode development",
"build": "webpack --mode production"
}
yarn add babel-loader @babel/core -D
复制代码
npm
几乎同样,本文使用 yarn
安装...根目录下添加 webpack.config.js
es6
const path = require('path')
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].[hash:8].js'
},
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules|bower_components)/,
use: { loader: 'babel-loader' } // options 在 .babelrc 定义
}
]
}
}
复制代码
src/index.js
github
const func = () => {
console.log('hello webpack')
}
func()
class User {
constructor() {
console.log('new User')
}
}
const user = new User()
复制代码
执行 yarn build
后就能够打包成功,打包后的代码是压缩后的。而 yarn start
后的代码是未压缩的。为了使代码可读性高一点,咱们能够在webpack.config.js
添加:web
module.exports = {
//...
devtool: true
}
复制代码
打包后咱们能够发现箭头函数并未转化为 ES5
语法!npm
查阅 babel plugins 文档,若是要转义箭头函数,须要使用到 @babel/plugin-transform-arrow-functions
这个插件 同理转义 class
须要使用 @babel/plugin-transform-classes
json
yarn add @babel/plugin-transform-arrow-functions @babel/plugin-transform-classes -D
复制代码
根目录下创建 .babelrc
文件:promise
{
"plugins": [
"@babel/plugin-transform-arrow-functions",
"@babel/plugin-transform-classes"
]
}
复制代码
yarn build
以后能够看出 箭头函数和类都被转义了。
可是假如你再使用 async await
之类的 es6
语法,你还得一个个添加,这是不实际的。
@babel-preset-env 就整合了这些语法转义插件:
Using plugins:
transform-template-literals {}
transform-literals {}
transform-function-name {}
transform-arrow-functions {}
transform-block-scoped-functions {}
transform-classes {}
transform-object-super {}
//...
复制代码
使用以下:
yarn add @babel-preset-env -D
复制代码
.babelrc
{
"presets": ["@babel/preset-env"]
}
复制代码
Babel 默认只转换新的 JavaScript 句法(syntax),而不转换新的 API ,好比 Iterator、Generator、Set、Maps、Proxy、Reflect、Symbol、Promise 等全局对象,以及一些定义在全局对象上的方法(好比 Object.assign)都不会转码。
这样就致使了一些新的 API 老版浏览器不兼容。如上述所说,对于新的 API,你可能须要引入 @babel-polyfill
来进行兼容
yarn add @babel-polyfill -D
复制代码
修改 weboack.config.js
module.exports = {
entry: ['@babel-polyfill', './src/index.js']
}
复制代码
yarn build
发现文件体积大了不少,由于上面的代码表示将 @babel-polyfill
的代码也打包进去了。
固然这不是咱们但愿的,如何按需编译呢? 咱们能够这么作:
index.js
import '@babel/polyfill' // 引入
const func = () => {
console.log('hello webpack')
}
func()
class User {
constructor() {
console.log('new User')
}
}
const user = new User()
new Promise(resolve => console.log('promise'))
Array.from('foo')
复制代码
还原 webpack.config.js
module.exports = {
entry: './src/index.js'
}
复制代码
修改 .babelrc
{
"presets": [["@babel/preset-env", { "useBuiltIns": "usage" }]]
}
复制代码
yarn build
后发现咱们的代码体积就变得很小了!
babel-polyfill
会污染全局做用域, 如引入 Array.prototype.includes
修改了 Array 的原型,除此外还有 String...babel-polyfill
引入新的对象: Promise
、WeakMap
等这也不是咱们但愿出现的。
@babel/runtime
的做用:
babel-runtime
中,这样作能减少项目文件的大小。polyfill
:不会污染全局做用域,可是不支持实例方法如 Array.includes@transform-runtime
的做用:
babel-runtime
更像是分散的 polyfill
模块,须要在各自的模块里单独引入,借助 transform-runtime 插件来自动化处理这一切,也就是说你不要在文件开头 import 相关的 polyfill
,你只需使用,transform-runtime
会帮你引入。yarn add @babel/runtime-corejs2
yarn add @babel/plugin-transform-runtime -D
复制代码
修改 .babelrc
{
"presets": ["@babel/preset-env"],
"plugins": [["@babel/plugin-transform-runtime", { "corejs": 2 }]]
}
复制代码
index.js
移除 import '@babel/polyfill'
添加装饰器模式的支持
yarn add @babel/plugin-proposal-decorators -D
复制代码
index.js
function annotation(target) {
target.annotated = true
}
@annotation
class User {
constructor() {
console.log('new User')
}
}
//...
复制代码
.babelrc
{
"presets": ["@babel/preset-env"],
"plugins": [
["@babel/plugin-proposal-decorators", { "decoratorsBeforeExport": true }],
["@babel/plugin-transform-runtime", { "corejs": 2 }]
]
}
复制代码