[TIMOB-23490] iOS: Hybrid CommonJS and Native modules don't export commonjs
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Critical |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2017-05-08T20:15:25.000+0000 |
Affected Version/s | n/a |
Fix Version/s | Release 6.1.0 |
Components | Tooling |
Labels | merge-6.1.0, mobilesdk, module, user_experience, validate |
Reporter | Andrea Vitale |
Assignee | Christopher Williams |
Created | 2015-06-16T22:03:31.000+0000 |
Updated | 2017-06-08T17:50:17.000+0000 |
Description
When I create a pure Javascript Module, any exported method is accessible.
In detail for the module I've:
Created a new Mobile Module Project, from Appcelerator Studio, with id (for example) it.module.test
Inside _assets_ folder, I've created a js file called _it.module.test.js_
Into _it.module.test.js_ I have:
module.exports = {
something: "hi"
}
Now I package this module directly from Appcelerator Studio and install this module inside a Test Application.
Into this application I do:
var module = require("it.module.test");
Ti.API.info(module.something);
And the result is *null*.
The problem is that the native module classes are being called instead of the CommonJS ones as per the docs.
__UPDATE__
After several tests, it seems that the problem is the Titanium SDK 4.0.0.
When I create the module project inside Appcelerator Studio and selecting a previous version of Titanium SDK (3.4.1 for example) all works perfectly with the same code.
Attachments
File | Date | Size |
---|---|---|
module with 3.4.1GA.png | 2015-07-01T09:17:04.000+0000 | 22307 |
module with 4.0.0GA.png | 2015-07-01T09:20:57.000+0000 | 27205 |
package_log.txt | 2017-05-05T17:37:49.000+0000 | 31291 |
[~Andrea.Vitale], the correct CommonJS syntax is to use:
or:
It might be easier to use [Titaniumifier](https://github.com/smclab/titaniumifier) to package CommonJS modules if you really want to, but you can also just drop the library in your Alloy project's lib folder (
app/lib
) and require it by name (without the.js
)Hi Fokke, thanks for your response. The problem using:
still persists.
@fokkezb I want to package my module [as per the docs at the bottom of this guide](http://docs.appcelerator.com/platform/latest/#!/guide/Titanium_Module_Concepts) because I've to distribute it inside a company so the code can't be legible. I tryied Titaniumifier but the result is not the same.
@Andrea I am facing the same problem trying to isolate some code in commonJS modules, I just cannot get it to work. I followed this fine tutorial but that did not work either http://code.tutsplus.com/tutorials/creating-titanium-mobile-modules-using-commonjs--mobile-16833 Did you find a solution?
@Claus Stilborg, I've wrote an update into issue description: when you create a module using Appcelerator Studio, select a Titanium SDK version prior to 4.0.0 (3.5.1 for example).
Thx, I will try that.
Well that did not help. I am going back to copying files around the lib folders. It horrifies me to think about how many hours I have spent trying to get this to work. I rarely give up, but this one is off the charts frustrating...
Any update on this? It is a year old issue and it is still unfixed..
[~cng]- Issue is reproducible with 5.4.0 and 6.0 SDK's it.module.test.js
index.js
I confirm that this issue is still alive also with Ti SDK 6.0.0.GA. How is possible that there is no plan to solve it?
Any update on this? This is really blocking for me. Thanks.
I pushed a PR to fix iOS here: https://github.com/appcelerator/titanium_mobile/pull/9007 While doing this I also looked at Android. While i was able to push a fix for encrypting the main JS file into the native hybrid module, I don't see any support within the generated module for hooking the commonjs encrypted contents to the runtime. I suspect this may *never* have worked on the Android side. I'll have to take a deeper look if we want a fix there as well.
If you compile with 3.5.1.GA, it works well also on Android on a 5.5.1.GA app.
I've cloned off a ticket to address Android's issue separately. This PR https://github.com/appcelerator/titanium_mobile/pull/9007 address the issue for iOS, which was that we were not sending *relative* file paths to titanium_prep for encryption, but instead *absolute* file paths in the CLI rewrite (way, w ay back in 4.x days). titanium_prep is brain-dead and assumes relative paths, so it effectively was outputting "empty" encrypted js into the module. Additionally, my require rewrite in 6.0.0 was slightly off in how to combine the two together, and required a 1-1 or 2- line change.
For QE: - Create a new native module project from the cli named with id
com.axway.hybrid.mod
for iOS - in the module project'sassets
dir add acom.axway.hybrid.mod.js
file with these contents:- build that module from the cli using the SDK from this PR
appc run -p ios --build-only
- create a new titanium mobile project from cli for iOS - drop the built module into the mobile project, add it to tiapp.xml - Modify the app.js of the mobile project to be:-
appc run -p ios
- an alert should pop up with the message 'hi'. If it has no message and just the *alert* title, then that is the bug.Tested with this environment: Node Version: 6.10.1 NPM Version: 3.10.10 Mac OS: 10.12.4 Appc CLI: 6.2.0 Appc CLI NPM: 4.2.9 Titanium SDK version: 6.2.0 (locally built on PR/9007) Appcelerator Studio, build: 4.8.1.201612050850 Xcode 8.3.2 FR did not pass. I tried with both sample codes (the original one in the Description, and the one in the comment specially written for QE). I tried these on the CLI and in Studio and saw this error. While building the module, the following error was seen:
When I reverted back to SDK 6.0.4.GA, I was able to reproduce the original bug.
The error described in my last comment was not related to this ticket (but needed to be fixed and merged before carrying out this test. After the fix on the unrelated ticket, I reran the test on this ticket and FR passed for 6.2.0. For 6.1.0, the PR was merged so I tested it on the installed SDK where the fix appears, 6.1.0.v20170509114242 Node Version: 6.10.1 NPM Version: 3.10.10 Mac OS: 10.12.4 Appc CLI: 6.2.0 Appc CLI NPM: 4.2.9 Titanium SDK version: 6.1.0.v20170509114242, and locally installed 6.2.0 Appcelerator Studio, build: 4.8.1.201612050850 Xcode 8.3.2 Android 6.0.1 device I used Chris's demo code above, and followed the appropriate test steps. I installed the app and saw an alert pop up with a message "hi".
Hi, I tested this using 6.1.0.GA and it works only for a single commonjs file. Suppose that you have a module called
it.commonjs.test.module
and in yourassets
folder you have two files called: -it.commonjs.test.module.js
-another.module.js
In the second one you have:In the first one you have:
You can successfully build the module but when you try to run an app that requires the
it.commonjs.test.module
module you get an error like:If you try to build the module with 3.5.1.GA sdk everything is working fine. On Android is working also with 6.1.0.GA.
[~Andrea.Vitale] I've cloned off TIMOB-24796 to handle this new bug report.