@@ -29,6 +29,12 @@ bool IsLikelyOptionalModule(const std::string& moduleName) {
29
29
return false ;
30
30
}
31
31
32
+ // Helper function to check if a file path is an ES module (.mjs) but not a source map (.mjs.map)
33
+ bool IsESModule (const std::string& path) {
34
+ return path.size () >= 4 && path.compare (path.size () - 4 , 4 , " .mjs" ) == 0 &&
35
+ !(path.size () >= 8 && path.compare (path.size () - 8 , 8 , " .mjs.map" ) == 0 );
36
+ }
37
+
32
38
// Helper function to resolve main entry from package.json with proper extension handling
33
39
std::string ResolveMainEntryFromPackageJson (const std::string& baseDir) {
34
40
// Get the main value from package.json
@@ -139,7 +145,7 @@ bool IsLikelyOptionalModule(const std::string& moduleName) {
139
145
}
140
146
141
147
// Check if this is an ES module (.mjs) and handle it directly
142
- if (path. size () >= 4 && path. compare (path. size () - 4 , 4 , " .mjs " ) == 0 ) {
148
+ if (IsESModule ( path) ) {
143
149
// For ES modules, use LoadESModule directly instead of require()
144
150
TryCatch tc (isolate);
145
151
Local<Value> moduleNamespace;
@@ -508,7 +514,7 @@ bool IsLikelyOptionalModule(const std::string& moduleName) {
508
514
}
509
515
510
516
// Check if this is an ES module
511
- bool isESM = modulePath. size () >= 4 && modulePath. compare (modulePath. size () - 4 , 4 , " .mjs " ) == 0 ;
517
+ bool isESM = IsESModule ( modulePath) ;
512
518
std::shared_ptr<Caches> cache = Caches::Get (isolate);
513
519
514
520
if (isESM) {
@@ -662,7 +668,7 @@ throw NativeScriptException(isolate,
662
668
}
663
669
664
670
Local<Value> ModuleInternal::LoadScript (Isolate* isolate, const std::string& path) {
665
- if (path. size () >= 4 && path. compare (path. size () - 4 , 4 , " .mjs " ) == 0 ) {
671
+ if (IsESModule ( path) ) {
666
672
// Treat all .mjs files as standard ES modules.
667
673
return ModuleInternal::LoadESModule (isolate, path);
668
674
}
@@ -1070,7 +1076,8 @@ ScriptOrigin origin(isolate, urlString, 0, 0, false, -1, Local<Value>(), false,
1070
1076
std::shared_ptr<Caches> cache = Caches::Get (isolate);
1071
1077
bool isWorkerContext = cache && cache->isWorker ;
1072
1078
1073
- if (path.size () >= 4 && path.compare (path.size () - 4 , 4 , " .mjs" ) == 0 ) {
1079
+ // Check if this is an .mjs file but NOT a .mjs.map file
1080
+ if (IsESModule (path)) {
1074
1081
// Read raw text without wrapping.
1075
1082
std::string sourceText = tns::ReadText (path);
1076
1083
0 commit comments