排除标准库的 babel polyfills

Tam*_*mil 5 typescript webpack babeljs babel-polyfill

当使用 babel 和 webpack 将我们的 TS 项目捆绑到 es5 中时,我们看到了标准字符串方法的填充,例如

String.replace, String.match,String.split

$ Added following core-js polyfill:
  es.array.join { "ie":"11" }

$ Added following core-js polyfills:
  es.string.replace { "edge":"17", "firefox":"67", "ie":"11", "ios":"12", "safari":"12.1", "samsung":"9.2" }
  es.string.split { "edge":"17", "ie":"11" }
Run Code Online (Sandbox Code Playgroud)

根据 MDN 和其他可信兼容性表的兼容性图表,所有这些平台都完全支持这些方法。但为什么 babel 会为它们添加 polyfills 对我们来说仍然是一个问题。

有没有办法通过 babel 配置禁用这些标准方法的 polyfill?下面是我们的 babel 配置,如果我们遗漏了什么,请提示我们。

const presets = [
  [
    '@babel/env', // Using babel preset-env
    {
      debug: true, // Disabling debug output in console
      targets: {
        browsers: ['> 1%', 'not dead', 'not ie < 8'], // Spit code for all browsers with usage > 1% and not dead.
      },
      useBuiltIns: 'usage', // Use polyfills for runtime feature support
      corejs: 3, // Use core-js 3 Polyfills.
    },
  ],
];

const plugins = [
  '@babel/plugin-transform-runtime', // Use Babel helpers to prevent code bloating with helpers
];

module.exports = { presets, plugins };
Run Code Online (Sandbox Code Playgroud)