reactjs - 使用 babel polyfill 后,bundle.js 仍然包含箭头函数和默认参数

标签 reactjs webpack babeljs babel-polyfill

我从堆栈溢出中添加了 Babel 或其他人给出的许多设置,但 ES6 的新功能(例如箭头函数和默认参数)仍然在我的bundle.js中

bundle.js 的内容如下:

    var nn = function(e={}) {
        const {baseClasses: t, newClasses: n, Component: r} = e;
        if (!n)
            return t;
        const a = At()({}, t);
        return Object.keys(n).forEach(e=>{
            n[e] && (a[e] = `${t[e]} ${n[e]}`)
        }
        ),
        a
    };

因此,当我在 IE11 中打开页面时,发生错误 missing ')',该错误指向 function(e={})

这是我的 webpack.config.js:

const entries = require("./config/pages").reduce(function(map, page) {
  map[page] = `./src/${page}/index.js`;
  return map;
}, {});

module.exports = {
  mode: 'development',
  entry: ["@babel/polyfill",...entries],

  output: {
    path: path.resolve(__dirname,'dist'),
    publicPath: '/',
    filename: 'myapp/static/js/[name]/bundle.js'
  },
  devtool: 'source-map',
  module: require("./config/loaders"),
  devServer:{
    open: true,
    publicPath: '/',
    historyApiFallback: true,
    disableHostCheck: true
  },
  plugins: [
    new MiniCssExtractPlugin({
        filename: "[name].css",
        chunkFilename: "[id].css"
    }),
    new webpack.ProvidePlugin({
        fetch: "isomorphic-fetch",
    })
  ]
};

和模块的./config/loaders

module.exports = {
  rules: [
    {
      test: /\.(js|jsx)$/,
      // exclude: /node_modules/,
      loader: 'babel-loader',
    },
    {
      test: /\.(css|less)$/,
      use: ["style-loader", "css-loader", "less-loader"]
    },
    {
      test: /\.(png|jpg|gif)$/,
      use: [
        {
          loader: 'url-loader',
          options: {
            limit: 500, //small than 500 use url, otherwise use base64
            outputPath:'inquiry/static/img/'
          }
        }
      ]
    },
    {
      test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
      use: [{
        loader: 'file-loader',
        options: {
          outputPath: 'myapp/static/fonts/'
        }
      }]

    }
  ]
};

.babelrc:

{
  "presets": [
    // ["@babel/env",
    //   {
    //     "targets": {
    //       "browsers": "ie 11"
    //     },      
    //     "useBuiltIns": "usage",
    //     "corejs": "3.0.1",
    //   }
    // ],
    ["@babel/preset-env",
      {
        "useBuiltIns": "entry",
        "corejs": "3.0.1",
      }],
    "@babel/react"
  ],
  "plugins": [
    ["@babel/transform-runtime"],
    ["@babel/plugin-transform-parameters"],
    ["@babel/plugin-transform-arrow-functions"],
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy": true
      }
    ],
    [
      "@babel/plugin-proposal-class-properties",
      {
        "loose": true
      }
    ],
  ]
}

另外,我已经在我的index.js中导入了“@babel/polyfill”

PS:我注意到包含 ES6 功能的代码来自 node_modules 中的 Meterial UI lib,因此我删除了 babel loader 规则中的 exclude:/node_modules/, 但它仍然不起作用。

最佳答案

我使用的是 webpack 5,并且在 webpack.config.js 中将目标设置为 es5 解决了我的问题。

module.exports = {
    target: 'es5',
    ...
}

关于reactjs - 使用 babel polyfill 后,bundle.js 仍然包含箭头函数和默认参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55823678/

相关文章:

html - React SVG Manipulation [Electron](如果连接了 DB,则更改颜色)

ionic-framework - 如何在 Ionic v2 中扩展默认 webpack 配置

reactjs - 当名称不以大写字母开头时导入不起作用

webpack - 碰到我不理解的babel构建错误

javascript - DraftJS 未捕获类型错误 : Cannot read property 'blocks' of undefined

reactjs - 如何以编程方式关闭 ionic 选择

typescript - 为什么使用 webpack 重新编译这么多 [不可缓存] 文件?

javascript - 尽管预设了 ES2015,Webpack 仍输出 ES6 代码

javascript - 为什么 instanceof 对 babel-node 下的 Error 子类的实例不起作用?

node.js - 通过 SASS 支持使用 Typescript、SASS 和 CSS 模块搜索 React 组件库的 Webpack 配置