[Tools] Target specific browsers with babel-preset-env and the babel pollyfill (browserslist)
Converting all of our modern JavaScript into ES5 compatible syntax is a great way to use modern features while targeting older browsers. What happens when the browsers natively support these language features? Then it no longer makes sense to transform that code or to include polyfills that will go unused. In this lesson, we’ll add the @babel/polyfill package and configure babel-preset-env
To reduce the polyfill size, we can targeting morden browser by using browserlist:
webpack.config.base.js
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, 'dist'),
filename: 'app.bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets: [['@babel/preset-env', {
targets: [
'last 2 versions',
'not dead',
'not < 2%'
],
useBuiltIns: 'entry'
}], '@babel/preset-react'],
plugins: [
'react-hot-loader/babel',
'@babel/plugin-proposal-class-properties'
]
}
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
exclude: /node_modules/
}
]
},
plugins: [new HtmlWebpackPlugin({
template: './src/index.html'
})]
}
You can see the supported browserlist by running:
npx browserlist "last 2 versions, not dead, not < 2%"
It will return a llst of supported browsers.
Together with bundler-analyser we can see the bundle size:
webpack.config.prod.js
const merge = require('webpack-merge')
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer')
const baseConfig = require('./webpack.config.base')
module.exports = merge(baseConfig, {
mode: 'production',
plugins: [new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
reportFilename: 'bundle_sizes.html'
})],
externals: {
react: 'React',
'react-dom': 'ReactDOM'
}
})
[Tools] Target specific browsers with babel-preset-env and the babel pollyfill (browserslist)的更多相关文章
- SSE优化指令集编译错误: inlining failed in call to always_inline 'xxx': target specific option mismatch xxx
在用QtCreator编译SSE优化指令的时候,出现了如下错误, inlining failed in call to always_inline '__m128i _mm_packus_epi32( ...
- Babel(1)认识Babel
阅读文档 Babel中文网 关于 Babel 你必须知道的 如何写好.babelrc?Babel的presets和plugins配置解析 不容错过的 Babel 7 知识汇总 一口(很长的)气了解 b ...
- Error: Couldn't find preset "env" relative to directory "/Users/user/ethereumjs-vm"
运行npm run build时遇见这个问题,解决办法是安装: npm install --save-dev babel-preset-env 就解决了
- webpack.config.js配置遇到Error: Cannot find module '@babel/core'&&Cannot find module '@babel/plugin-transform-react-jsx' 问题
下文是网上找到的方法,是因为版本冲突的原因,参照后安装7版本解决 cnpm install -D babel-loader@ babel-core babel-preset-env 一. 问题描述 在 ...
- [Node] Use babel-preset-env with Native Node Features and Also Use Babel Plugins
In this lesson we'll show how to setup a .babelrc file with presets and plugins. Then create npm scr ...
- Babel:下一代Javascript语法编译器
定义 Babel是一个Javascript的编译器,通过它你可以将一些新版本的ECMAScript语法转换成低版本的语法.以便能够在低版本的浏览器或者其它环境平稳运行. 截至目前笔者写这篇文章的时候, ...
- 深入学习rollup来进行打包
深入学习rollup来进行打包 阅读目录 一:什么是Rollup? 二:如何使用Rollup来处理并打包JS文件? 三:设置Babel来使旧浏览器也支持ES6的代码 四:添加一个debug包来记录日志 ...
- vue客户端渲染首屏优化之道
提取第三方库,缓存,减少打包体积 1. dll动态链接库, 使用DllPlugin DllReferencePlugin,将第三方库提取出来另外打包出来,然后动态引入html.可以提高打包速度和缓存第 ...
- 还学不会webpack?看这篇!
摘要: webpack入门教程. 原文:还学不会webpack?看这篇! 作者:MudOnTire Fundebug经授权转载,版权归原作者所有. Webpack已经流行好久了,但很多同学使用webp ...
随机推荐
- poj 3463 最短路+次短路
独立写查错不能,就是维护一个次短路的dist 题意:给定一个有向图,问从起点到终点,最短路+比最短路距离长1的路的个数. Sample Input25 81 2 31 3 21 4 52 3 12 5 ...
- bio、nio、aio及select、poll、epoll
BIO与NIO.AIO的区别http://blog.csdn.net/skiof007/article/details/52873421 select.poll.epoll之间的区别总结http:// ...
- 不同的activity使用bundle对象传值给广播接收器
解决了一下午的问题,广播机制传值,在一个activity中发送广播给广播接收器,使用的是同一个action 在另一个activity中如果也发送广播给同一个广播接收器,使用相同的action,会导致后 ...
- IntelliJ IDEA 学习(五)类注释和自定义方法注释
intellj idea的强大之处就不多说了,相信每个用过它的人都会体会到,但是我们经常会感觉他很复杂,尤其刚从eclipse转过来的童鞋,相信刚开始的那段经历都是不堪回首的 如何实现 ...
- python文本 字符与字符值转换
python文本 字符与字符值转换 场景: 将字符转换成ascii或者unicode编码 在转换过程中,注意使用ord和chr方法 >>> print(ord('a')) 97 ...
- 【java】将字符串的首字母大写
工具方法: public static void main(String[] args) { System.out.println(upperCaseFirst("barer")) ...
- 使用web.xml方式加载Spring时,获取Spring context的两种方式
使用web.xml方式加载Spring时,获取Spring context的两种方式: 1.servlet方式加载时: [web.xml] <servlet> <servlet-na ...
- Android之在Tab更新两个ListView,让一个listview有按下另个一个listview没有的效果
直接上代码,不说了 UpdateListViewItem.zip
- 高效的数据压缩编码方式 Protobuf
一. protocol buffers 是什么? Protocol buffers 是一种语言中立,平台无关,可扩展的序列化数据的格式,可用于通信协议,数据存储等. Protocol buffers ...
- mac下使用brew安装java等应用
可以使用brew安装很多应用,比如java,idea,iterms,sublime brew tap caskroom/versions 将会安装新的brew仓库源brew cask install ...