electrino icon indicating copy to clipboard operation
electrino copied to clipboard

Support for importing js files and modules via require

Open ninjaprawn opened this issue 8 years ago • 16 comments
trafficstars

Modules are a key aspect in creating an application. In Node.js, modules can be installed and imported.

This PR adds support for importing js files, as well as modules that could come from Node packages.

What this PR does:

  • Modifies the require implementation
  • Adds a method to create a JSContext which is a duplicate of the default one
    • This is done as Apple has not implemented the -copyWithZone: method
  • Renamed the test-app folder to app
  • Changed the app group to a folder reference, so that files can be easily copied without having to manually add them to a Copy Files phase.

If you have any questions, feel free to ask :)

ninjaprawn avatar May 07 '17 10:05 ninjaprawn

I've created this PR: https://github.com/ninjaprawn/electrino/pull/1/files

That adds better support. Nevertheless the whole thing should follow this documentation: https://nodejs.org/api/modules.html#modules_all_together

Some things that we will need to implement:

  • requires to relative paths should start with ./ or ../
  • Inject variables like __dirname and __filename
  • the module object has more things (such as the loaded attribute), and I think this is the object that should be stored in the require cache (not the exports one, although obviously module.exports is the thing that needs to be returned when requiring the module).
  • a module should only be loaded once. So first we should check if it exists in the require cache.
  • Right before evaluating a module it should be put in the require cache to avoid infinite loops if there are require cycles. For example: module A requires module B and this one requires A. The second time we require A, we check if it's in the cache and we don't load it again.

gimenete avatar May 09 '17 11:05 gimenete

I think that once we've established where the project is heading, we will add those features in a reliable way.

ninjaprawn avatar May 10 '17 09:05 ninjaprawn

Hi @ninjaprawn @gimenete @kittsuphatt !

Thanks very much for this. (I failed at Github and just didn't notice this pull request until now.)

I'll take a look and merge ASAP.

pojala avatar May 22 '17 19:05 pojala

@ninjaprawn would be awesome if you can fix the merge conflicts. I can merge this after testing it out

amilajack avatar Jun 09 '17 18:06 amilajack

Oh man -- when I wrote "ASAP", I didn't mean to imply "20 days later". Sorry for that. I've just been swamped.

I can check out and fix the merge conflicts on this one tomorrow, I think.

pojala avatar Jun 10 '17 09:06 pojala

I won't be able to do much for the next couple of days (I'm flying back from WWDC to Australia, which is a long time). If nothing is done until then, I'll be more than happy to fix the conflicts

ninjaprawn avatar Jun 10 '17 16:06 ninjaprawn

What ever happened to this project?

genesy avatar Nov 12 '18 16:11 genesy

@ninjaprawn is this PR ready to review?

amilajack avatar Jan 04 '19 02:01 amilajack

@ninjaprawn please help with the merge conflicts!

paramaggarwal avatar Jul 03 '19 02:07 paramaggarwal

sorry this took so long! should be good to go now

ninjaprawn avatar Jul 12 '19 00:07 ninjaprawn

@pojala let's move ahead with this PR! 😄

paramaggarwal avatar Mar 12 '20 11:03 paramaggarwal

this proj is still active?

genesy avatar Mar 12 '20 11:03 genesy

@gimenete Should this be closed since your PR was merged?

amilajack avatar Mar 12 '20 15:03 amilajack

@amilajack my PR improved things, but was not implementing everything. I'm unsure about the state of the current implementation.

The implementation should follow this spec https://nodejs.org/api/modules.html#modules_all_together

gimenete avatar Mar 12 '20 15:03 gimenete

@ninjaprawn Would love if you could the merge conflicts one last time. I'll definitely merge this right after you fix them.

amilajack avatar Mar 12 '20 16:03 amilajack

@ninjaprawn could you help us out with the merge conflicts - we are looking to merge this in.

paramaggarwal avatar Mar 20 '20 02:03 paramaggarwal