Skip to content

Commit

Permalink
Merge pull request tidev#5641 from pec1985/timob-16004
Browse files Browse the repository at this point in the history
[TiMOB-16004] iOS: Expose current file path and directory path to commonjs module scope
  • Loading branch information
vishalduggal committed May 9, 2014
2 parents af19de8 + 294dd0e commit 9001c6e
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions iphone/Classes/KrollBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
extern NSString * const TI_APPLICATION_GUID;

NSString * TitaniumModuleRequireFormat = @"(function(exports){"
"var __OXP=exports;var module={'exports':exports};%@;\n"
"var __OXP=exports;var module={'exports':exports};var __dirname=\"%@\";var __filename=\"%@\";\n%@;\n"
"if(module.exports !== __OXP){return module.exports;}"
"return exports;})({})";

Expand Down Expand Up @@ -723,7 +723,25 @@ - (id)krollObjectForProxy:(id)proxy

-(id)loadCommonJSModule:(NSString*)code withSourceURL:(NSURL *)sourceURL
{
NSString *js = [[NSString alloc] initWithFormat:TitaniumModuleRequireFormat,code];
// This takes care of resolving paths like `../../foo.js`
sourceURL = [NSURL fileURLWithPath:[[sourceURL path] stringByStandardizingPath]];

// Get the relative path to the Resources directory
NSString *relativePath = [sourceURL path];
relativePath = [relativePath stringByReplacingOccurrencesOfString:[[[NSBundle mainBundle] resourceURL] path] withString:@""];
relativePath = [[relativePath substringFromIndex:1] stringByDeletingLastPathComponent];

NSString *dirname = [relativePath length] == 0 ? @"." : relativePath;
/*
* This is for parity with android, if the file is located in the Resources, then __dirname returns "."
* otherwise the __dirname returns the folder names separated by "/"
* for example:
* "/Resources/constants.js" __dirname = "."
* "/Resources/views/login/window.js" __dirname = "views/login"
*/

NSString *filename = [sourceURL lastPathComponent];
NSString *js = [[NSString alloc] initWithFormat:TitaniumModuleRequireFormat, dirname, filename,code];

/* This most likely should be integrated with normal code flow, but to
* minimize impact until a in-depth reconsideration of KrollContext can be
Expand Down

0 comments on commit 9001c6e

Please sign in to comment.