14.webpack4处理js兼容性问题及js压缩——babel

1.执行步骤

1)创建相关文件

 2)下载js兼容相关依赖

babel@6.23.0、babel-loader@8.0.6、babel/preset-env@7.8.4、@babel/polyfill@7.8.3、@core-js@3.6.4

2.代码段

1)webpack.config.js 使用babel-loader、@babel/preset-env做基本js兼容性处理

const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.export = {

    entry: './src/js/index.js',
    output: {
        filename: 'js/built.js',
        path: resolve(__dirname,'build')
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                options: {
                    //预设:基本js兼容性处理
                    presets: ['@babel-preset-env']
                }
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html'
        })
    ],
    mode: 'development'
}

使用基本兼容性处理,只能转换基本语法,如:Promise语法就不能转换

2)webpack.config.js 使用babel-loader、@babel/preset-env、@babel/polyfill做全部js兼容性处理

const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.export = {

    entry: './src/js/index.js',
    output: {
        filename: 'js/built.js',
        path: resolve(__dirname,'build')
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                options: {
                    //预设:基本js兼容性处理
                    presets: ['@babel-preset-env']
                }
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html'
        })
    ],
    mode: 'development'
}

再在入口文件index.js中增加:import '@babel/polyfill 语句就行了

3)webpack.config.js 使用babel-loader、@babel/preset-env、core-js依赖做部分js兼容性问题

const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: './src/js/index.js',
    output: {
        filename: 'js/built.js',
        path: resolve(__dirname,'build')
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                options: {
                    presets: [
                        [
                            '@babel/preset-env',
                            // 按需加载
                            useBuiltIns: 'usage',
                            // 指定core-js版本
                            corejs: {
                                version: 3
                            },
                            targets: {
                                chrome: '60',
                                firefox: '60',
                                ie: '9',
                                safari: '10',
                                edge: '17'
                            }
                        ]
                    ]
                }
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html'
        })
    ],
    mode: 'development'
}

4)index.js

// 直接引用就可以了,处理全部的兼容性
// import "@babel/polyfill"

const add = (x, y) => x + y;
console.log(add(2, 5));


const promise = new Promise((resolve) => {
  setTimeout(() => {
    console.log("定时器执行完了~")
    resolve()
  }, 1000)
})

console.log(promise)

5)index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h2>hello webpack</h2>
</body>
</html>

总结:

①可以下载并使用babel、babel-loader、@babel/preset-env做基本js兼容处理

②可以下载并使用babel、babel-loader、@babel/preset-env、@babel/polyfill做全部js兼容处理

③可以下载并使用babel、babel-loader、@babel/preset-env、core-js做部分兼容性处理

js压缩只许将mode改为production就行

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值