为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 useBuiltIns: "usage" 排除 core-js

如何使用 useBuiltIns: "usage" 排除 core-js

料青山看我应如是 2021-09-04 15:04:21
使用 babel 7.5.5、core-js 3.1.4 和 webpack 4.38.0,我如何从转译中排除 core-js?我不想node_modules完全排除,因为我有需要转译的库如果我使用exclude: /node_modules\/(core-js)/,一个 core-js 模块会抛出类型错误:$ 不是函数这让我有另外两个选择。改用包含,包含我的 src 目录和需要一一转换的每个依赖项使用useBuiltIns: entry而不是使用,因为在这种情况下exclude: /node_modules/\(core-js)/有效,并且importcore.js 位于 main.js 的顶部这两个选项对我来说似乎都不是很好的解决方案,因为usage自 7.4以来“不再是实验性的”。有什么办法可以使用用法让它工作吗?它是 babel-loader 还是 babel 中的错误?还是我的配置有问题?这是我的 Webpack 配置:const path = require('path');const webpack = require('webpack');module.exports = {    mode: 'production',    entry: {        main: ['./src/main'],    },    output: {        path: path.resolve(__dirname, './build/'),        publicPath: '/build/'    },    module: {        rules: [            {                test: /\.js$/,                exclude: /node_modules\/(core-js)/,                use: {                    loader: 'babel-loader'                },            },            {                test: require.resolve('jquery'),                use: [                    {                        loader: 'expose-loader',                        options: 'jQuery'                    },                    {                        loader: 'expose-loader',                        options: '$'                    }                ]            }        ]    },    plugins: [        new webpack.ProvidePlugin({            $: 'jquery',            jQuery: 'jquery',            'window.jQuery': 'jquery'        })    ],};这是我的 babel 配置:module.exports = function (api) {    api.cache(true);    return {        presets: [            [                '@babel/preset-env',                {                    corejs: {                        version: 3,                    },                    useBuiltIns: 'usage',                }            ]        ],    };};您可以使用以下存储库重现错误:https : //github.com/tomm1996/usebuiltins-exclude-test
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 431 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信