Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-19155] LiveView: Breaks when app.js contains named self execution function

GitHub Issuen/a
TypeBug
PriorityNone
StatusClosed
ResolutionFixed
Resolution Date2020-05-19T15:18:43.000+0000
Affected Version/sRelease 4.1.0
Fix Version/sn/a
ComponentsLiveView
Labelsn/a
ReporterFokke Zandbergen
AssigneeUnknown
Created2015-07-03T06:44:09.000+0000
Updated2020-05-19T15:18:43.000+0000

Description

When the (compiled) app.js contains a named self executing function (e.g. to protect global scope) LiveView will break.

Example

(function hello() {
  Ti.UI.createWindow({
    backgroundColor: 'green'
  }).open();
})();

Error

[INFO]  [LiveView] Error Evaluating app @ Line: 1
[ERROR] SyntaxError: Unexpected identifier 'hello'
[ERROR] File: app
[ERROR] Line: 1
[ERROR] SourceId: <null>
[ERROR] Backtrace:
[ERROR]  undefined

Cause

I tracked this back to LiveView's fileserver.js where it bind all global variables in app.js to Global._globalCtx: https://github.com/appcelerator/liveview/blob/master/lib/fserver.js#L298-L312 Before this part everything is OK, but after the code will end up as:
   nction hello() {
  Ti.UI.createWindow({
    backgroundColor: 'green'
  }).open();
})();

Solution

As the comment in the LiveView code says there IS a better way to do this. An example could be [how I did this](https://github.com/FokkeZB/ti-proxy/commit/147aa34eccb383cf33ef9df11677d3be766b4abb) for [ti-proxy](https://github.com/FokkeZB/ti-proxy), the client-side transformer that [appc-liveviewer-app](https://github.com/appcelerator/appc-liveviewer-app/) uses. We should consider using ti-proxy for LiveViewer as well, it already has more features then LiveViewer does, like serving new/changed assets, cleaning up events etc.

Comments

  1. Ewan Harris 2020-05-19

    I can no longer reproduce this on SDK 9.0.1.GA, we removed the munging of code we use to do via a regex in favour of operating on the AST via babel so I imagine that this got fixed with that

JSON Source