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

require() local JS files at runtime #42

Closed
tonylukasavage opened this issue Jul 26, 2014 · 6 comments
Closed

require() local JS files at runtime #42

tonylukasavage opened this issue Jul 26, 2014 · 6 comments
Assignees
Milestone

Comments

@tonylukasavage
Copy link
Owner

$ triple 
> var something = require('something');
undefined

We should use acorn to parse the AST for each line and determine if it contains a require. We should allow it to run as is on the app, and if it fails with a "missing module" error, we should use the repl's require.resolve to determine the potential local path of the file. If we find it, it should be added to the project (preferably using .include as in #41) and then the line should be attempted to be executed again.

@mattapperson
Copy link
Contributor

What's the use case for needing to ast a required file? I don't think we need to go that far but I could be wrong

@tonylukasavage tonylukasavage added this to the 0.2.0 milestone Jul 26, 2014
@tonylukasavage tonylukasavage self-assigned this Jul 28, 2014
@tonylukasavage
Copy link
Owner Author

Nah, you're right, AST would be overkill. I only really brought it up because acorn is already included and is fast. A regex, which I already added, would work. In any case, I'll likely tackle this, but if anyone else is planning to take a stab at it, please discuss it here first. I have short and long term plans for handling require() in triple, not the least of which is supporting ti.current and ti.next (node.js-style) style requires, likely contingent on TiSDK used with triple. I don't want any work going to waste, so just want to make sure any contriobution has the maximum chance of survival.

@tonylukasavage
Copy link
Owner Author

Initial implementation is in master. It treats the current working directory as though it was the root for the titanium app. So if you were in the triple folder you could require lodash like this:

$ triple
> var _ = require('node_modules/lodash/lodash')
undefined
> _.map([1,2,3],function(i){return i*3});
[ 3, 6, 9 ]

It will also work with relative and absolute paths. This all needs a lot of testing to flush out the bugs and edge cases.

@tonylukasavage
Copy link
Owner Author

Tests show require() working as expected for single file commonjs modules. Any future issues will be logged as new issues.

@mattapperson
Copy link
Contributor

This is for iOS simulator only correct?

@tonylukasavage
Copy link
Owner Author

Yep, everything is only for ios sim at the moment. https://github.com/tonylukasavage/triple#support

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

2 participants