webpack - babel 篇
栏目: JavaScript · 发布时间: 7年前
内容简介:基于我的上篇文章,Babel 是一个让我们能够使用 ES 新特性的 JS 编译工具,我们可以在本文以当前最新版本的babel - 7.10 为例, 做
基于我的上篇文章, webpack4.0 入门篇 - 构建前端开发的基本环境
,补充对 babel
进行的一次探究。上篇文章讲叙到的 webpack babel
时几乎一笔带过,所以这篇文章将进行补充说明.
Babel 是一个让我们能够使用 ES 新特性的 JS 编译工具,我们可以在 webpack
中配置 Babel
,以便使用 ES6、ES7 标准来编写 JS 代码。
本文以当前最新版本的babel - 7.10 为例, 做 babel
的配置. 相关版本号如下
{
"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"
}
}
复制代码
babel-loader 和 @babel/core
建立基本的 webpack
配置文件
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
复制代码
-
yarn : 和
npm几乎一样,本文使用yarn安装... - babel-loader: 转义 js 文件代码的 loader
- @babel/core:babel 核心库
根目录下添加 webpack.config.js
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
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
添加:
module.exports = {
//...
devtool: true
}
复制代码
@babel-preset-env
打包后我们可以发现箭头函数并未转化为 ES5
语法!
查阅babel plugins 文档,如果要转义箭头函数,需要使用到 @babel/plugin-transform-arrow-functions
这个插件
同理转义 class
需要使用 @babel/plugin-transform-classes
yarn add @babel/plugin-transform-arrow-functions @babel/plugin-transform-classes -D 复制代码
根目录下建立 .babelrc
文件:
{
"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/polyfill
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/runtime 和 @babel/plugin-transform-runtime
-
babel-polyfill会污染全局作用域, 如引入Array.prototype.includes修改了 Array 的原型,除此外还有 String... -
babel-polyfill引入新的对象:Promise、WeakMap等
这也不是我们希望出现的。
-
@babel/runtime的作用:babel-runtime polyfill
-
@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'
@babel/plugin-proposal-decorators
添加装饰器模式的支持
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 }]
]
}
复制代码
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
我是90后,我是创业家
腾讯互联网与社会研究院 / 华章图书 / 2015-1-1 / 48.00元
第1本揭秘17个90后精彩创业故事 他们是:脸萌创始人郭列、北大硕士卖米粉的张天一、微博《我只过1%的生活》短时间转发35万多次的伟大的安妮、备受争议的90后总裁余佳文、节操姐CEO陈桦……17位90后的创业家为你分享他们的创业故事!从这些90后孩子的经历中,还可以看到互联网带来的巨大好处,这又是这一代人的幸运。这些创业者有一个共同特点,即他们在做自己事业的时候,会经常遇到来自家庭和社会的阻......一起来看看 《我是90后,我是创业家》 这本书的介绍吧!