Skip to content

Commit 19e6636

Browse files
author
Nako Sung
committed
updated module resolve
1 parent 63663e3 commit 19e6636

File tree

6 files changed

+40
-44
lines changed

6 files changed

+40
-44
lines changed

Examples/Content/Scripts/2048/game/application.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
module.exports = function (widget, container, data) {
55
require('animframe_polyfill')
6-
var Actuator = require('actuator')(container, data)
7-
var LocalStorageManager = require('local_storage_manager')
8-
var KeyboardInputManager = require('keyboard_input_manager')(widget, container)
9-
var GameManager = require('game_manager')
6+
var Actuator = require('./actuator')(container, data)
7+
var LocalStorageManager = require('./local_storage_manager')
8+
var KeyboardInputManager = require('./keyboard_input_manager')(widget, container)
9+
var GameManager = require('./game_manager')
1010

1111
var game = new GameManager(data.Size, KeyboardInputManager, Actuator, LocalStorageManager)
1212

Examples/Content/Scripts/2048/game/game_manager.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var Tile = require('tile')
2-
var Grid = require('grid')
1+
var Tile = require('./tile')
2+
var Grid = require('./grid')
33

44
function GameManager(size, InputManager, Actuator, StorageManager) {
55
this.size = size; // Size of the grid

Examples/Content/Scripts/2048/game/grid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Tile = require('game/tile')
1+
var Tile = require('./game/tile')
22

33
function Grid(size, previousState) {
44
this.size = size;

Examples/Content/Scripts/2048/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
game()
4646
}
4747
try {
48-
game = require('game/application')(widget, page, data)
48+
game = require('./game/application')(widget, page, data)
4949
}
5050
catch (e) {
5151
console.error("EXCEPTION",String(e),e.stack)

Plugins/UnrealJS/Content/Scripts/polyfill/timers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(function (target) {
2-
var makeWindowTimer = require('windowTimers')
2+
var makeWindowTimer = require('./windowTimers')
33
if (Root == undefined || Root.OnTick == undefined) return
44

55
var timerLoop = makeWindowTimer(target);

Plugins/UnrealJS/Source/V8/Private/JavascriptContext_Private.cpp

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,8 @@ class FJavascriptContextImplementation : public FJavascriptContext
11711171

11721172
auto inner = [&](const FString& script_path)
11731173
{
1174-
auto it = Self->Modules.Find(script_path);
1174+
auto full_path = IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*script_path);
1175+
auto it = Self->Modules.Find(full_path);
11751176
if (it)
11761177
{
11771178
info.GetReturnValue().Set(Local<Value>::New(isolate, *it));
@@ -1182,8 +1183,7 @@ class FJavascriptContextImplementation : public FJavascriptContext
11821183
FString Text;
11831184
if (FFileHelper::LoadFileToString(Text, *script_path))
11841185
{
1185-
Text = FString::Printf(TEXT("(function (__dirname) {\nvar module = { exports : {}, filename : __dirname }, exports = module.exports;\n%s\n;return module.exports;}('%s'));"), *Text, *script_path);
1186-
auto full_path = IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*script_path);
1186+
Text = FString::Printf(TEXT("(function (__dirname) {\nvar module = { exports : {}, filename : __dirname }, exports = module.exports;\n%s\n;return module.exports;}('%s'));"), *Text, *script_path);
11871187
#if PLATFORM_WINDOWS
11881188
full_path = full_path.Replace(TEXT("/"), TEXT("\\"));
11891189
#endif
@@ -1192,7 +1192,7 @@ class FJavascriptContextImplementation : public FJavascriptContext
11921192
{
11931193
UE_LOG(Javascript, Log, TEXT("Invalid script for require"));
11941194
}
1195-
Self->Modules.Add(script_path, UniquePersistent<Value>(isolate, exports));
1195+
Self->Modules.Add(full_path, UniquePersistent<Value>(isolate, exports));
11961196
info.GetReturnValue().Set(exports);
11971197
found = true;
11981198
return true;
@@ -1265,47 +1265,43 @@ class FJavascriptContextImplementation : public FJavascriptContext
12651265

12661266
auto inner2 = [&](FString base_path)
12671267
{
1268-
for (;;)
1269-
{
1270-
if (!FPaths::DirectoryExists(base_path)) return false;
1271-
1272-
auto script_path = base_path / required_module;
1273-
if (!script_path.EndsWith(TEXT(".js")))
1274-
{
1275-
if (script_path.EndsWith(TEXT(".json")))
1276-
{
1277-
if (inner_json(script_path)) return true;
1278-
}
1279-
else
1280-
{
1281-
if (inner(script_path + TEXT(".js"))) return true;
1282-
if (inner_json(script_path + TEXT(".json"))) return true;
1283-
if (inner(script_path / TEXT("index.js"))) return true;
1268+
if (!FPaths::DirectoryExists(base_path)) return false;
12841269

1285-
if (inner_package_json(script_path)) return true;
1286-
}
1287-
}
1288-
else
1289-
{
1290-
if (inner(script_path)) return true;
1291-
}
1292-
1293-
base_path = base_path / TEXT("node_modules");
1270+
auto script_path = base_path / required_module;
1271+
if (script_path.EndsWith(TEXT(".js")))
1272+
{
1273+
if (inner(script_path)) return true;
12941274
}
1275+
else
1276+
{
1277+
if (inner(script_path + TEXT(".js"))) return true;
1278+
}
1279+
if (script_path.EndsWith(TEXT(".json")))
1280+
{
1281+
if (inner_json(script_path)) return true;
1282+
}
1283+
else
1284+
{
1285+
if (inner_json(script_path + TEXT(".json"))) return true;
1286+
}
1287+
1288+
if (inner(script_path / TEXT("index.js"))) return true;
1289+
if (inner_package_json(script_path)) return true;
1290+
1291+
return false;
12951292
};
12961293

12971294
auto current_script_path = FPaths::GetPath(StringFromV8(StackTrace::CurrentStackTrace(isolate, 1, StackTrace::kScriptName)->GetFrame(0)->GetScriptName()));
12981295
#if PLATFORM_WINDOWS
12991296
current_script_path = current_script_path.Replace(TEXT("\\"), TEXT("/"));
13001297
#endif
1301-
if (!inner2(current_script_path))
1298+
if (!(required_module[0] == '.' && inner2(current_script_path)))
13021299
{
1303-
if (!inner2("."))
1300+
for (const auto& path : Self->Paths)
13041301
{
1305-
for (const auto& path : Self->Paths)
1306-
{
1307-
if (inner2(path)) break;
1308-
}
1302+
if (inner2(path)) break;
1303+
1304+
if (inner2(path / TEXT("node_modules"))) break;
13091305
}
13101306
}
13111307

0 commit comments

Comments
 (0)