Skip to content

Commit 37cde9b

Browse files
committed
fix: allow dynamic import chunks to resolve properly under diverse bundling cases
1 parent 25a4e90 commit 37cde9b

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

NativeScript/runtime/ModuleInternalCallbacks.mm

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,15 @@ static bool IsNodeBuiltinModule(const std::string& moduleName) {
8787
// would need the referrer's directory to resolve it.
8888
bool specIsRelative = !spec.empty() && spec[0] == '.';
8989
if (referrerPath.empty() && specIsRelative) {
90-
// Unable to resolve a relative path without knowing the base directory.
91-
return v8::MaybeLocal<v8::Module>();
90+
// For dynamic imports, assume the base directory is the application root
91+
// This handles cases where runtime.mjs calls import("./chunk.mjs")
92+
// but the referrer module isn't properly registered
93+
if (IsScriptLoadingLogEnabled()) {
94+
NSLog(@"[resolver] No referrer found for relative import '%s', assuming app root",
95+
spec.c_str());
96+
}
97+
referrerPath =
98+
RuntimeConfig.ApplicationPath + "/runtime.mjs"; // Default to runtime.mjs as referrer
9299
}
93100

94101
// 3) Compute its directory
@@ -110,7 +117,13 @@ static bool IsNodeBuiltinModule(const std::string& moduleName) {
110117
if (!spec.empty() && spec[0] == '.') {
111118
// Relative import (./ or ../)
112119
std::string cleanSpec = spec.rfind("./", 0) == 0 ? spec.substr(2) : spec;
113-
candidateBases.push_back(baseDir + cleanSpec);
120+
std::string candidate = baseDir + cleanSpec;
121+
candidateBases.push_back(candidate);
122+
123+
if (IsScriptLoadingLogEnabled()) {
124+
NSLog(@"[resolver] Relative import: '%s' + '%s' -> '%s'", baseDir.c_str(), cleanSpec.c_str(),
125+
candidate.c_str());
126+
}
114127
} else if (spec.rfind("file://", 0) == 0) {
115128
// Absolute file URL, e.g. file:///app/path/to/chunk.mjs
116129
std::string tail = spec.substr(7); // strip file://
@@ -149,7 +162,7 @@ static bool IsNodeBuiltinModule(const std::string& moduleName) {
149162
std::string base = RuntimeConfig.ApplicationPath + "/" + spec;
150163
candidateBases.push_back(base);
151164

152-
// Additional heuristic: Webpack encodes path separators as underscores in
165+
// Additional heuristic: bundlers often encode path separators as underscores in
153166
// chunk IDs (e.g. "src_app_components_foo_bar_ts.mjs"). Try converting
154167
// those underscores back to slashes and look for that file as well.
155168
std::string withSlashes = spec;
@@ -295,7 +308,7 @@ static bool IsNodeBuiltinModule(const std::string& moduleName) {
295308
absPath = builtinPath;
296309
}
297310
} else if (IsLikelyOptionalModule(spec)) {
298-
// Create a placeholder file on disk that webpack can resolve to
311+
// Create a placeholder file on disk that bundler can resolve to
299312
std::string appPath = RuntimeConfig.ApplicationPath;
300313
std::string placeholderPath = appPath + "/" + spec + ".mjs";
301314

@@ -494,7 +507,7 @@ static bool IsNodeBuiltinModule(const std::string& moduleName) {
494507
}
495508
}
496509

497-
// Special handling for webpack chunks: check if this is a webpack chunk and install it
510+
// Special handling for bundler chunks: check if this is a bundler chunk and install it
498511
v8::Local<v8::Value> namespaceObj = module->GetModuleNamespace();
499512
if (namespaceObj->IsObject()) {
500513
v8::Local<v8::Object> nsObj = namespaceObj.As<v8::Object>();

0 commit comments

Comments
 (0)