[TIMOB-20472] Allow require to find modules in node_modules, etc
GitHub Issue | n/a |
---|---|
Type | New Feature |
Priority | High |
Status | Closed |
Resolution | Duplicate |
Resolution Date | 2016-07-25T13:18:51.000+0000 |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | Tooling |
Labels | n/a |
Reporter | Brenton House |
Assignee | Christopher Williams |
Created | 2016-02-26T03:20:25.000+0000 |
Updated | 2018-11-16T15:41:44.000+0000 |
Description
{quote}As a developer,
I want to be able to use NPM to install modules and libraries,
So that I can install packages such as bluebird, lodash, or custom libraries from NPM in my application.{quote}
Unless I am missing something here, at compile time, you know all the libraries that could possibly be referenced. For all static includes, i.e. require("lodash") or require("../mylib"), you could easily map and change the requires to the correct path before packaging the app.
Dynamic requires, i.e. require(myvar + "mylib") would require a bit more effort such as creating a function that could lookup the desired library at runtime and use that with the relative folder to find it in a list of libraries in the app (assuming the list was generated at compile time.)
You could also look in the package.json files at compile time to see if there is a main referenced. Support for index.js could also be added at compile time.
I've tried a few of the methods such as ti-commonjs which look for things at runtime, but the performance hit from those was far too great.
Related to TIMOB-16078
Also see https://github.com/tonylukasavage/ti-commonjs
Implemented in Adamantium now: https://github.com/mobilehero/adamantium/blob/next/1.7.35/adamantium/features/support-for-nodejs-style-require.md I am not sure many of my new "features" I should work at creating PR's for. If it doesn't fit in with the Appc way of doing things, I can just keep it in my fork. If it really is something that has a possibility of being integrated into Alloy, then I will do the legwork of creating a PR and tests. Thanks!
[~cbarber] this might be an interesting user story to consider for the refactored build process?
[~fokkezb] Isn't this a runtime thing? You can put a
node_modules
directory in your project today and it will get bundled. The thing isrequire()
will not resolve it. That's more of an SDK/runtime issue than a tooling issue. We need to fixrequire()
on all platforms to properly resolve modules and support thenode_modules
directory. BTW, am I the only one that doesn't like having "node" stuff in Titanium?[~cbarber] lol! node love you even if the feeling isn't reciprocal! The change I made resolves most
require()
calls at compile time and the remainder at runtime. Yes, ideally, the change would go into the SDK but I am able to get a working solution by altering just Alloy. My reasoning for the change is I often want to use nodejs packages in Titanium without having to modify them to get them to work. Also, it is nice to be able to use the same coding styles across Titanium/Alloy and Node.js. I really blame it all on you guys anyway, as I probably wouldn't be nearly as deep into Node.js if it wasn't for Appccelerator![~cbarber] This indeed is a workaround for TIMOB-16078. I've pushed to get that done for a long time and I agree that would be highly preferable over solving it in Alloy. I actually think our Windows implementation does follow the NodeJS specs btw. I get requests to support NodeJS require every week. A great example is how we have to modify our Alloy built-in moment.js every time we update it in order for it to be able to find its localisations. It's libraries like these that people die to use in Alloy. This should be seen entirely separate from the question if we should provide shims for NodeJS APIs. It's first-most about being able to use libraries distributed via NPM. Of course once that works, people can choose to load in libraries that shim
process
,os
,path
etc themselves. We might not actually need to provide that ourselves.Things like filesystem, network, etc do not have a standard. Titanium invented an API. Years later, Node.js invented an API. Node.js won. Node.js is the standard. So is the goal to turn Titanium into Node.js for mobile?
CommonJS and the NodeJS compliant
require()
is the standard that this ticket is about, with the goal to allow developers to drop in JS libraries without the need to (as we do with Moment) modify them. The Node.js API are a different story. Literally as in ticket, but with this one resolved we might not even need to since the community could create those (or use the ones [~brentonhouse] and [~tlukasavage] made)Duplicate of TIMOB-16078, which will be in 6.0.0
Closing ticket as duplicate with reference to the above comments.