relative includes
hi - i noticed that "require" doesn't handle relative includes, like Node etc.
This isn't great for portable code of course.
Here's a link to an alternative implementation that I've used in the paste -- trying it out in Ejecta right now : https://gist.github.com/weepy/8318556
If require were to change, my vote would go to require.js since it supports CommonJS, etc.
The code I provided does everything needed. It's basically identical to Node's implementation. Require.js does a whole bunch more and isn't really suited for Ejecta, as it's more a web development tool.
Ejecta's require is a small wrapper around the native loadModuleWithId.
I guess we would have to remember the current's scripts path somewhere when calling these functions, in order to construct the relative paths. This would have to be a "per file global var" which I don't think is possible!? Also if we only do this for require(), it would fall apart when mixing it with ejecta.include().
I don't think the native JSC functions provide a way to extract the calling scripts path somehow. I'll look into it a bit more.
I managed to get the implementation in here working ok : https://gist.github.com/weepy/8318556
This uses XHR to get the text and eval it, so that I can add a local version of require to the script as a variable
var text = "require.register('" + path + "',
function(module, exports, require, __filename, __dirname) {\n\n"
+ request.responseText + "\n\n
}); //@ sourceURL=" + path + '.js';
Currently I'm just overwriting ejecta's require with this implementation.
Wouldn't this fall apart when modules are not required directly with the first eval()? E.g.:
var someFunctionCalledFromSomeOtherFile = function() {
var m = require('some-module');
}
The require would then have the wrong base path, I believe.
I'm sorry - I don't quite follow ...
I use this file in all my web development and it works well. I also have a node script which will spider through and build a single file.
Oh - I think I understand what you're saying.
Yes it only works if you do the first require from a single file, e.g. require("./app") which then require's everything else.
An alternative way round this is to make a path starting with "/" reference the base root, i.e. the require leaves them untouched.
On Thu, Jan 9, 2014 at 2:04 PM, Dominic Szablewski <[email protected]
wrote:
Wouldn't this fall apart when modules are not required directly with the first eval()? E.g.:
var someFunctionCalledFromSomeOtherFile = function() { var m = require('some-module');}
The require would then have the wrong base path, I believe.
— Reply to this email directly or view it on GitHubhttps://github.com/phoboslab/Ejecta/issues/279#issuecomment-31933610 .