【问题标题】:How to build React app with Babel for Internet Explorer 11?如何使用 Babel 为 Internet Explorer 11 构建 React 应用程序?
【发布时间】:2017-11-22 20:40:04
【问题描述】:
// 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 配置,我不确定。任何想法如何解决这个问题?

错误:

文件:

【问题讨论】:

  • 我还在学习自己做出反应。但是,每当我在控制台中看到该错误时,在我看来就像缺少 Jquery 库一样?不过可能还很遥远。
  • 这是对 PROD env 还是 dev 的破坏?从屏幕截图中我看到了语法错误,但我没有看到语法错误是什么。我认为这与 webpack 配置更相关,也许你也应该将它标记为 webpack(如果可能的话)
  • @Jorden1337:该错误会阻止其余的 js 执行,因此在这种情况下,第一个错误就是所有需要解决的问题。
  • @Kunukn:Prod 坏了。
  • 您似乎有一些文件在需要 jQuery 的地方加载/渲染,而 jQuery 本身尚未加载。

标签: javascript jquery reactjs webpack babeljs


【解决方案1】:

为确保您通过 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

【讨论】:

  • 我已根据您的更改更新了jsPresetsmodule.exports,它们看起来正确吗?捆绑的文件还是一样的。
  • 您的预设/插件的顺序似乎有效。但是,请注意,如果您已经在使用 env,则不需要包含 es-2015 预设。请参阅答案中链接的文档——页面上的第一个子标题。
猜你喜欢
  • 2019-10-18
  • 2020-03-28
  • 2017-11-05
  • 2021-12-16
  • 2019-12-17
  • 1970-01-01
  • 1970-01-01
  • 2019-07-17
  • 2013-12-09
相关资源
最近更新 更多