Safari/Babel/Webpack 严格模式不支持 Const 声明

Safari/Babel/Webpack Const declarations are not supported in strict mode

Safari 无法使用此行加载我的 React 应用程序:

Const declarations are not supported in strict mode.

当我查看失败的行时,我看到:

const Crypto = __webpack_require__(624)

这不是我的应用程序中的内容,因此它必须由 Webpack 或其他依赖项注入。

Babel 不应该用 var 替换 const 吗?

Babel 依赖项

"babel": "~6.1.0",
"babel-core": "~6.2.0",
"babel-loader": "~6.2.0",
"babel-plugin-transform-runtime": "~6.1.0",
"babel-polyfill": "~6.2.0",
"babel-preset-es2015": "~6.1.0",
"babel-preset-react": "~6.1.0",
"babel-preset-stage-0": "~6.1.0",
"babel-runtime": "~6.2.0"

Babel 加载器配置

{
  test: /\.js|\.jsx$/,
  exclude: /node_modules/,
  loader: 'babel-loader',
  query: {
    cacheDirectory: true,
    plugins: ['transform-runtime'],
    presets: ['es2015', 'react', 'stage-0']
  }
}

注意 我的应用程序在 Chrome 中运行。

您在 babel-loader 设置中排除了 "node_modules",因此它不会处理您的外部依赖项。您所依赖的这个包可能没有经过浏览器内使用测试。

而且,顺便说一句,除非你使用 "transform-es2015-block-scoping" 插件,否则 babel 不会替换你的常量。

http://babeljs.io/docs/plugins/transform-es2015-block-scoping/

它不包含在 "es2015" 预设中。你只有 "check-es2015-constants" 插件,它只检查和验证 const 声明。

将 const 转换为 var 的插件称为 "transform-es2015-block-scoping",它包含在 "es2015" 预设中。