【问题标题】:How to exclude core-js using useBuiltIns: "usage"如何使用 useBuiltIns 排除 core-js:“用法”
【发布时间】:2019-12-13 03:29:07
【问题描述】:

使用 babel 7.5.5、core-js 3.1.4 和 webpack 4.38.0,如何将 core-js 排除在转译之外?

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

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

TypeError: $ 不是函数

这给我留下了另外两个选择。

  1. 改用包含,包括我的 src 目录和需要一个一个转译的每个依赖项
  2. 使用useBuiltIns: entry 而不是使用,因为在这种情况下exclude: /node_modules/\(core-js)/ 有效,而import core.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

【问题讨论】:

标签: javascript webpack babeljs babel-loader core-js


【解决方案1】:

您需要从 Babel 转译中排除 core-jswebpack/buildin

您可以使用以下排除正则表达式:

exclude : [
  /\bcore-js\b/,
  /\bwebpack\/buildin\b/
]

这里还有一个完整的babel-loader 配置和一些有用的cmets:

{
  module : {
    rules : [{
      test : /\.js$/,
      // Some module should not be transpiled by Babel
      // See https://github.com/zloirock/core-js/issues/743#issuecomment-572074215
      exclude : [
        /\bcore-js\b/,
        /\bwebpack\/buildin\b/
      ],
      loader : "babel-loader",
      options : {
        babelrc : false,
        // Fixes "TypeError: __webpack_require__(...) is not a function"
        // https://github.com/webpack/webpack/issues/9379#issuecomment-509628205
        // https://babeljs.io/docs/en/options#sourcetype
        sourceType : "unambiguous",
        presets : [
          ["@babel/preset-env", {
            // Webpack supports ES Modules out of the box and therefore doesn’t require
            // import/export to be transpiled resulting in smaller builds, and better tree
            // shaking. See https://webpack.js.org/guides/tree-shaking/#conclusion
            modules : false,
            // Adds specific imports for polyfills when they are used in each file.
            // Take advantage of the fact that a bundler will load the polyfill only once.
            useBuiltIns : "usage",
            corejs : {
              version : "3",
              proposals : true
            }
          }]
        ]
      }
    }
  }
}

https://github.com/zloirock/core-js/issues/743#issuecomment-572074215


编辑:如果你尝试使用@babel/plugin-transform-runtime

plugins : [
  // Require the Babel runtime as a separate module to avoid the duplication
  // https://webpack.js.org/loaders/babel-loader/#babel-is-injecting-helpers-into-each-file-and-bloating-my-code
  ["@babel/plugin-transform-runtime", {
    // Requires @babel/runtime-corejs3
    // https://babeljs.io/blog/2019/03/19/7.4.0#migration-from-core-js-2
    corejs : { version: 3, proposals: true }
  }],
}

您可能会遇到类似的错误:

Uncaught TypeError: _typeof2 is not a function
    at _typeof (typeof.js:8)
    at eval (sockjs.js:123)
    at Object.eval (sockjs.js:131)
    at eval (sockjs.js:6565)
    at Object../node_modules/sockjs-client/dist/sockjs.js (main.js:13790)
    at __webpack_require__ (main.js:70)
    at eval (webpack://PUBLIC_ENGINE/(:8000/webpack)-dev-server/client/clients/SockJSClient.js?:110:14)
    at Object../node_modules/webpack-dev-server/client/clients/SockJSClient.js (main.js:13874)
    at __webpack_require__ (main.js:70)
    at eval (webpack://PUBLIC_ENGINE/(:8000/webpack)-dev-server/client/socket.js?:56:41)

这可以通过从转译中排除@babel/runtime-corejs3 来解决:

exclude : [
  /\bcore-js\b/,
  /\bwebpack\/buildin\b/,
  /@babel\/runtime-corejs3/
]

【讨论】:

  • 这个答案似乎很有希望。一旦我确认它也可以与 jquery 一起使用,我会接受它
  • 我能够确认您的解决方案适用于多种配置。谢谢!
  • 是的!好的!感谢您的反馈@TomM
  • @YvesM。我无法让它工作。这是我开始的一个新问题..您能否提供您的专业知识? stackoverflow.com/questions/61578788/…
猜你喜欢
  • 2020-09-17
  • 2020-05-06
  • 1970-01-01
  • 2019-08-18
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 2023-02-16
相关资源
最近更新 更多