tidev / liveview

Titanium LiveTi (live preview of Titanium applications on device)
Other
7 stars 3 forks source link

No ES Module export on controller #226

Open jonasfunk opened 9 months ago

jonasfunk commented 9 months ago

I'm trying to export a function from a controller.ts, but using ES Module style doesn't work:

// Doesn't work
export function foo(){
    alert('foo');
}

// Works
$.bar = function(){
     alert('bar');
}

// Works
exports.foobar = function(){
     alert('foobar');
}

Works with lib files.

Operating System
  Name                        = macOS
  Version                     = 14.0
  Architecture                = 64bit
  # CPUs                      = 8
  Memory                      = 16.0GB

Node.js
  Node.js Version             = 16.20.2
  npm Version                 = 8.19.4

Titanium CLI
  CLI Version                 = 6.1.1
  node-appc Version           = 1.1.6

Titanium SDKs
  12.2.1.GA
janvennemann commented 9 months ago

Thanks for reporting this issue. I was able to reproduce this and already found the issue. A fix will be included the next alpha release.

janvennemann commented 9 months ago

@jonasfunk This should be fixed in the latest 2.0.0-alpha.10.

jonasfunk commented 9 months ago

Updated to the latest version. I have a simple ts alloy app, that opens a window. This worked with 2.0.0-alpha.9, but with 2.0.0-alpha.10 I get this error:

[ERROR] /@fs/usr/local/lib/node_modules/alloy/Alloy/template/lib/alloy.js:284
[ERROR] (null)
[ERROR]                                                                                     ^
[ERROR] TypeError: Object is not a constructor (evaluating 'new (require(__vite__injectQuery("/alloy/controllers/" + name, "import")))(args)')
[ERROR]     at  (/@fs/usr/local/lib/node_modules/alloy/Alloy/template/lib/alloy.js:284:84)
[ERROR]     at  (/app:27:40)
[ERROR]     at load (/ti.kernel.js:634:24)
[ERROR]     at liveViewRequire (/_liveview.bootstrap.js:3292:20)
[ERROR]     at  (/ti.main.js:12919:10)
[ERROR]     at doLoad (/ti.main.js:12885:15)
[ERROR]   
[ERROR]     Foundation 0x0000000180dca15c _NSDescriptionWithLocaleFunc + 76
[ERROR]     CoreFoundation 0x00000001803ed380 __CFStringAppendFormatCore + 10680
[ERROR]     CoreFoundation 0x00000001803ee0e4 _CFStringCreateWithFormatAndArgumentsReturningMetadata + 180
[ERROR]     CoreFoundation 0x00000001803ee024 _CFStringCreateWithFormatAndArgumentsAux2 + 40
[ERROR]     TitaniumKit 0x0000000104d422a4 TiLogMessage + 52
[ERROR]     TitaniumKit 0x0000000104d719ac -[TiExceptionHandler reportScriptError:] + 60
[ERROR]     JavaScriptCore 0x0000000109b06120 -[JSContext(Internal) valueFromNotifyException:] + 20
[ERROR]     JavaScriptCore 0x0000000109b0c5d4 -[JSValue callWithArguments:] + 456
[ERROR]     TitaniumKit 0x0000000104d41c9c -[KrollTimerTarget timerFired:] + 44
[ERROR]     Foundation 0x0000000180dd7f30 __NSFireTimer + 56
[ERROR]     CoreFoundation 0x00000001803c6d9c __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 28
[ERROR]     CoreFoundation 0x00000001803c6a58 __CFRunLoopDoTimer + 948
[ERROR]     CoreFoundation 0x00000001803c60f4 __CFRunLoopDoTimers + 284
[ERROR]     CoreFoundation 0x00000001803c0840 __CFRunLoopRun + 1796
[ERROR]     CoreFoundation 0x00000001803bfd28 CFRunLoopRunSpecific + 572
[ERROR]     GraphicsServices 0x000000018986ebc0 GSEventRunModal + 160
[ERROR]     UIKitCore 0x0000000119d9ffdc -[UIApplication _run] + 868
[ERROR]     UIKitCore 0x0000000119da3c54 UIApplicationMain + 124
[ERROR]     tsliveview-alpha 0x0000000104780ec0 main + 848
[ERROR]     dyld 0x0000000104cad558 start_sim + 20
janvennemann commented 9 months ago

@jonasfunk I'm currently working on a fix for this and i'm testing some examples. Do you have all your exports as ES module exports? Or can it happen that you have a CJS module.exports or exports mixed within? Or some files use ES export and others CJS module.exports? Because right now it would be pretty complex to support that. In the current liveview and Alloy build we use Babel to be able to transform such mixed cases. But Vite assumes valid ES modules and uses esbuild, so things break with combinations like that.

So it can either be have everything as ES exports, or everything as CJS exports. This could be configurable per project.

jonasfunk commented 9 months ago

@janvennemann I've attached the project I'm testing with. Noticed this is displayed when building: The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details. Don't know if that has any impact on anything, but would just mention it :) tsliveview-alpha.zip

janvennemann commented 9 months ago

Noticed this is displayed when building: The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details. Don't know if that has any impact on anything, but would just mention it :)

Yeah that comes from the recent upgrade to Vite 5 but it doens't impact anything build related. We need to change the way we load Vite in Liveview for it to go away, which will likely happen in one of the next updates ;)

jonasfunk commented 9 months ago

@janvennemann Noticed that you uploaded a new release. Is esm support fixed with that? 🙂

janvennemann commented 9 months ago

@janvennemann Noticed that you uploaded a new release. Is esm support fixed with that? 🙂

Unfortunately no, not yet. That release contained some fixes for stuff that i broke while trying to make ES Module exports work. I have an idea how to solve it, but i need a way to control the desired export Synatx per project i think. I'll keep you updated until i have this sorted out.

jonasfunk commented 7 months ago

Any progress on this issue (not pushy, just curious 🙂) ?

hansemannn commented 4 months ago

@janvennemann Can you please schedule this for May? Thank you!

jonasfunk commented 2 months ago

@janvennemann Can you please schedule this for May? Thank you!

Is this one in progress 🙂?