Closed tonylukasavage closed 10 years ago
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
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.
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.
Tests show require()
working as expected for single file commonjs modules. Any future issues will be logged as new issues.
This is for iOS simulator only correct?
Yep, everything is only for ios sim at the moment. https://github.com/tonylukasavage/triple#support
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'srequire.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.