javascript - 如何使用 Babel 为 Internet Explorer 11 构建 React 应用程序?

标签 javascript jquery reactjs webpack babeljs

// webpack.config.js

const webpack = require('webpack');
const precss = require('precss');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const PROD = process.env.NODE_ENV === 'production';

const jsPresets = [
  ['env', {
    targets: PROD ? {
      'browsers' : [
      'last 4 versions',
      'safari >= 7',
      'Explorer 11',
      ]
    } : {
      chrome: 1,
    },
  }],
  'es2015',
  'stage-1',
];

const baseConfig = {
  entry: [
    'babel-polyfill',
    'antd/dist/antd.css',
    './node_modules/m-react-splitters/lib/splitters.css',
    './node_modules/cli-truncate/index.js',
    'react-s-alert/dist/s-alert-default.css',
    'react-s-alert/dist/s-alert-css-effects/flip.css',
    'react-s-alert/dist/s-alert-css-effects/bouncyflip.css',
    'react-quill/dist/quill.snow.css',
  ],
  output: {
    path: './wp-content/plugins/clausehound/js',
    filename: 'clausehound.js',
  },
  module: {
    loaders: [{
      test: /\.json$/,
      loader: 'json-loader',
    }, {
      test: /\.css$/,
      loader: ExtractTextPlugin.extract('style', 'css?-url!postcss'),
    }, {
      test: /\.js$/,
      exclude: /(node_modules|bower_components)/,
      include: /cli-truncate\/index.js/,
      loader: 'babel-loader',
      query: {
        presets: jsPresets,
        plugins: [
          ['import', { libraryName: 'antd' }],
          ['transform-es2015-arrow-functions'],
        ],
      },
    }, {
      test: /\.jsx/,
      exclude: /(node_modules|bower_components)/,
      loader: 'babel-loader',
      query: {
        presets: jsPresets.concat(['react']),
      },
    }],
    rules: [
      { test: /[\/]jquery\.js$/, use: 'expose-loader?$!expose?jQuery' }
    ],
  },

  postcss: () => [precss],

  plugins: [
    new ExtractTextPlugin('../css/clausehound.css', { allChunks: true }),
    new webpack.OldWatchingPlugin(),
  ],
};

// Modify config if production build or not
if (PROD) {
  module.exports = Object.assign({}, baseConfig, {
    plugins: baseConfig.plugins.concat([
      new webpack.optimize.DedupePlugin(),
      new webpack.optimize.UglifyJsPlugin({ sourceMap: true }),
      new webpack.DefinePlugin({
        process: {
          env: {
            NODE_ENV: JSON.stringify('production'),
          },
        },
      }),
      new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery'
      }),
      [
      'transform-es2015-arrow-functions',
      'transform-class-properties',
      'syntax-class-properties',
      ],
    ]),
  });
} else {
  module.exports = baseConfig;
}

IE11 破坏了箭头函数语法。大多数(如果不是全部)节点模块都具有箭头函数,我不想将其全部包含在捆绑的 js 文件中。我运行了 babel-loader,只是为了测试,我包含了来自“cli-truncate”的文件,该文件向 baseConfigentry 属性抛出语法错误,但是应用程序仍然会在该包的 index.js 中的 () => 处抛出错误。

代码从 cli-truncate 失败的确切行是这样的: module.exports = (输入、列、选项) => {..}

我不认为这是这个包特有的,但我猜我需要以某种方式修改 webpack 配置,我不确定。有什么想法如何解决这个问题吗?

错误:

IE 11 errors

文件:

bundled file

最佳答案

为了确保 Babel 支持 IE11,您还需要将其添加到当前的 env 目标列表中。例如:

{
  "presets": [
    ["env", {
      "targets": {
        "browsers": [
          "Chrome >= 59",
          "FireFox >= 44",
          "Safari >= 7",
          "Explorer 11",
          "last 4 Edge versions"
        ]
      },
      "useBuiltIns": true
    }],
    "react",
    "stage-1"
  ],
  "ignore": [
    "node_modules"
  ],
  "plugins": [
    "transform-es2015-arrow-functions",
    "transform-class-properties",
    "syntax-class-properties"
  ]
}

另请参阅:Babel Env documentation

关于javascript - 如何使用 Babel 为 Internet Explorer 11 构建 React 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47443553/

相关文章:

javascript - 添加到集合时指定额外属性

javascript - 创建 jQuery 回调时指定参数

javascript - CSS - 设置旋转元素的顶部/左侧属性

javascript - jQuery 代码不会连接到搜索表单

javascript - onclick加载js数据?

reactjs - React 测试库测试仍然出现 "React state update on an unmounted component"错误

reactjs - React Hooks - 处理异步 api 调用

javascript - JavaScript/jQuery 如何在多个 ajax 响应上调用回调函数

javascript - Mootools Form.Validate 不触发事件

javascript - 动态设置 React 组件的 Props