tonylukasavage / triple

REPL for Titanium
MIT License
96 stars 21 forks source link

require() local JS files at runtime #42

Closed tonylukasavage closed 10 years ago

tonylukasavage commented 10 years ago
$ 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 commented 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

tonylukasavage commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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

mattapperson commented 10 years ago

This is for iOS simulator only correct?

tonylukasavage commented 10 years ago

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