Weex 在 Android 上报错 SyntaxError: Use of const in strict mode

更新日期: 2017-06-13 阅读次数: 14967 分类: weex

编译之后,真机界面上显示

weex android render error: -2013

Android Studio 的 Android Monitor 提示错误信息

06-13 15:18:38.555 11988-12005/com.alibaba.weex E/jsengine:  ReportException :undefined:393: SyntaxError: Use of const in strict mode.
06-13 15:18:38.565 11988-12005/com.alibaba.weex E/jsengine:  ReportException : SyntaxError: Use of const in strict mode.
                                                                at Jt ((weex):4:2004)
                                                                at Object.Dt [as createInstance] ((weex):3:31729)
                                                                at Object.R [as createInstance] ((weex):1:7385)
                                                                at global.(anonymous function) ((weex):7:1148)
06-13 15:18:38.565 11988-12005/com.alibaba.weex E/weex: reportJSException >>>> instanceId:1, exception function:createInstance, exception:SyntaxError: Use of const in 

index.js 393 行附近的代码

"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony export (immutable) */ __webpack_exports__["host"] = host;
/* harmony export (immutable) */ __webpack_exports__["https"] = https;
/* harmony export (immutable) */ __webpack_exports__["timeAgo"] = timeAgo;
/* harmony export (immutable) */ __webpack_exports__["unescape"] = unescape;
function host(url) {
  if (!url) return '';
  const host = url.replace(/^https?:\/\//, '').replace(/\/.*$/, '');
  const parts = host.split('.').slice(-3);
  if (parts[0] === 'www') parts.shift();
  return parts.join('.');
}

但是,从官方示例的 weex hacknews build 之后的代码 index.weex.js 来看

function host(url) {
  if (!url) return '';
  var host = url.replace(/^https?:\/\//, '').replace(/\/.*$/, '');
  var parts = host.split('.').slice(-3);
  if (parts[0] === 'www') parts.shift();
  return parts.join('.');
}

代码被转换成了 es5 的代码,而我的代码依然是 es6 的语法。

我的配置文件跟 weex hacknews 的完全一致,为何他的代码被转换成了 es5, 而我的没有呢?

反复对比发现,我的项目中少了这么一个配置

.babelrc

{ "presets": ["es2015"] }

加上之后,就 OK 啦。

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式