[Tools] Target specific browsers with babel-preset-env and the babel pollyfill (browserslist)
The and with tools Target babel ENV specific
2023-09-14 09:00:49 时间
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' } })
相关文章
- Mysql 1290 - The MySQL server is running with the --secure-file-priv option
- ORA-01193: file string is not the same file seen at start of recovery ORACLE 报错 故障修复 远程处理
- ORA-31067: XML nodes must be updated with valid nodes and of the same type ORACLE 报错 故障修复 远程处理
- ORA-38407: The ADT associated with the attribute set already exists. ORACLE 报错 故障修复 远程处理
- ORA-38787: Creating the first guaranteed restore point requires mount mode when flashback database is off. ORACLE 报错 故障修复 远程处理
- ORA-39322: Cannot use transportable tablespace with TIMESTAMP WITH TIME ZONE columns and different time zone version. ORACLE 报错 故障修复 远程处理
- ORA-39935: Updatable materialized view log string.string in tablespace string and materialized view string.string in tablespace string are not fully contained in the transportable set. ORACLE 报错 故障修复 远程处理
- ORA-40026: reference class name not found in the weights table ORACLE 报错 故障修复 远程处理
- ORA-48490: The field number exceeds the maximum number [string] ORACLE 报错 故障修复 远程处理
- ORA-13906: The tablespace is not of the right type. ORACLE 报错 故障修复 远程处理
- Start Exploring the World of Linux with RM(rmlinux)
- Getting Started with GPT and Linux: Unleashing the Power of the OS(gptlinux)
- Unlock the Full Potential of Your System with Etc and Linux(Etclinux)
- Discover the Versatility of Tilda Linux: The Perfect Operating System for Tech Enthusiasts.(tildalinux)
- Exploring the Power of Linux and Its Integration with FB Technology(linuxfb)
- Learn How to Set IP using Linux and C: The Ultimate Guide(linuxc设置ip)
- Unleash Your Inner Gamer with Linux and L4D2: Discover the Ultimate Gaming Experience!(linuxl4d2)
- Exploring the Power of MySQL and C for Advanced Database Management(mysqlcpp)
- Exploring the Benefits of MySQL Crossdatabase querying(mysql跨库联查)
- 尝试攻克決勝難關The A to Z of Redis(the a什么redis)