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

ant*_*tor 5 safari webpack babeljs

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

Const declarations are not supported in strict mode.
Run Code Online (Sandbox Code Playgroud)

当我看到失败的那条线时,我看到了:

const Crypto = __webpack_require__(624)
Run Code Online (Sandbox Code Playgroud)

这不是我的应用程序中的东西,所以它必须由Webpack或其他依赖项注入.

不宜巴贝尔替换constvar

Babel Dependencies

"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"
Run Code Online (Sandbox Code Playgroud)

Babel Loader配置

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

注意我的应用可以在Chrome中运行.

Mik*_*ike 7

您已在babel-loader设置中排除"node_modules",因此它不会处理您的外部依赖项.您所依赖的此软件包可能未针对浏览器内使用进行测试.

而且,顺便说一下,除非你使用"transform-es2015-block-scoping"插件,否则babel无论如何都不会取代你的consts.

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

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

将consts转换为vars的插件称为"transform-es2015-block-scoping",它包含在"es2015"预设中.

  • 根据 http://babeljs.io/docs/plugins/preset-es2015/,transform-es2015-block-scoping 插件包含在 es2015 预设中。 (2认同)