猿问

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

使用 babel 7.5.5、core-js 3.1.4 和 webpack 4.38.0,我如何从转译中排除 core-js?

我不想node_modules完全排除,因为我有需要转译的库

如果我使用exclude: /node_modules\/(core-js)/,一个 core-js 模块会抛出

类型错误:$ 不是函数

这让我有另外两个选择。

  1. 改用包含,包含我的 src 目录和需要一一转换的每个依赖项

  2. 使用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


料青山看我应如是
浏览 432回答 1
1回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答