Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No ES Module export on controller #226

Open
jonasfunk opened this issue Nov 16, 2023 · 11 comments
Open

No ES Module export on controller #226

jonasfunk opened this issue Nov 16, 2023 · 11 comments

Comments

@jonasfunk
Copy link

jonasfunk commented Nov 16, 2023

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
Copy link
Contributor

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
Copy link
Contributor

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

@jonasfunk
Copy link
Author

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
Copy link
Contributor

janvennemann commented Nov 26, 2023

@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
Copy link
Author

@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
Copy link
Contributor

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
Copy link
Author

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

@janvennemann
Copy link
Contributor

@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
Copy link
Author

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

@hansemannn
Copy link
Collaborator

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

@jonasfunk
Copy link
Author

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

Is this one in progress 🙂?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants