@@ -773,39 +773,6 @@ Maybe<URL> FinalizeResolution(Environment* env,
773773 return Just (resolved);
774774}
775775
776- Maybe<URL> PackageMainResolve (Environment* env,
777- const URL& pjson_url,
778- const PackageConfig& pcfg,
779- const URL& base) {
780- if (pcfg.exists == Exists::Yes) {
781- if (pcfg.has_main == HasMain::Yes) {
782- URL resolved (pcfg.main , pjson_url);
783- const std::string& path = resolved.ToFilePath ();
784- if (CheckDescriptorAtPath (path) == FILE) {
785- return Just (resolved);
786- }
787- }
788- if (env->options ()->es_module_specifier_resolution == " node" ) {
789- if (pcfg.has_main == HasMain::Yes) {
790- return FinalizeResolution (env, URL (pcfg.main , pjson_url), base);
791- } else {
792- return FinalizeResolution (env, URL (" index" , pjson_url), base);
793- }
794- }
795- if (pcfg.type != PackageType::Module) {
796- Maybe<URL> resolved = LegacyMainResolve (pjson_url, pcfg);
797- if (!resolved.IsNothing ()) {
798- return resolved;
799- }
800- }
801- }
802- std::string msg = " Cannot find main entry point for " +
803- URL (" ." , pjson_url).ToFilePath () + " imported from " +
804- base.ToFilePath ();
805- node::THROW_ERR_MODULE_NOT_FOUND (env, msg.c_str ());
806- return Nothing<URL>();
807- }
808-
809776void ThrowExportsNotFound (Environment* env,
810777 const std::string& subpath,
811778 const URL& pjson_url,
@@ -887,6 +854,99 @@ Maybe<URL> ResolveExportsTarget(Environment* env,
887854 return Just (subpath_resolved);
888855}
889856
857+ Maybe<URL> PackageMainResolve (Environment* env,
858+ const URL& pjson_url,
859+ const PackageConfig& pcfg,
860+ const URL& base) {
861+ if (pcfg.exists == Exists::Yes) {
862+ Isolate* isolate = env->isolate ();
863+ Local<Context> context = env->context ();
864+ if (!pcfg.exports .IsEmpty ()) {
865+ Local<Value> exports = pcfg.exports .Get (isolate);
866+ if (exports->IsString () || exports->IsObject () || exports->IsArray ()) {
867+ Local<Value> target;
868+ if (!exports->IsObject ()) {
869+ target = exports;
870+ } else {
871+ Local<Object> exports_obj = exports.As <Object>();
872+ Local<String> dot_string = String::NewFromUtf8 (env->isolate (), " ." ,
873+ v8::NewStringType::kNormal ).ToLocalChecked ();
874+ target =
875+ exports_obj->Get (env->context (), dot_string).ToLocalChecked ();
876+ }
877+ if (target->IsString ()) {
878+ Utf8Value target_utf8 (isolate, target.As <v8::String>());
879+ std::string target (*target_utf8, target_utf8.length ());
880+ Maybe<URL> resolved = ResolveExportsTarget (env, target, " " , " ." ,
881+ pjson_url, base);
882+ if (resolved.IsNothing ()) {
883+ ThrowExportsInvalid (env, " ." , target, pjson_url, base);
884+ return Nothing<URL>();
885+ }
886+ return FinalizeResolution (env, resolved.FromJust (), base);
887+ } else if (target->IsArray ()) {
888+ Local<Array> target_arr = target.As <Array>();
889+ const uint32_t length = target_arr->Length ();
890+ if (length == 0 ) {
891+ ThrowExportsInvalid (env, " ." , target, pjson_url, base);
892+ return Nothing<URL>();
893+ }
894+ for (uint32_t i = 0 ; i < length; i++) {
895+ auto target_item = target_arr->Get (context, i).ToLocalChecked ();
896+ if (target_item->IsString ()) {
897+ Utf8Value target_utf8 (isolate, target_item.As <v8::String>());
898+ std::string target_str (*target_utf8, target_utf8.length ());
899+ Maybe<URL> resolved = ResolveExportsTarget (env, target_str, " " ,
900+ " ." , pjson_url, base, false );
901+ if (resolved.IsNothing ()) continue ;
902+ return FinalizeResolution (env, resolved.FromJust (), base);
903+ }
904+ }
905+ auto invalid = target_arr->Get (context, length - 1 ).ToLocalChecked ();
906+ if (!invalid->IsString ()) {
907+ ThrowExportsInvalid (env, " ." , invalid, pjson_url, base);
908+ return Nothing<URL>();
909+ }
910+ Utf8Value invalid_utf8 (isolate, invalid.As <v8::String>());
911+ std::string invalid_str (*invalid_utf8, invalid_utf8.length ());
912+ Maybe<URL> resolved = ResolveExportsTarget (env, invalid_str, " " ,
913+ " ." , pjson_url, base);
914+ CHECK (resolved.IsNothing ());
915+ return Nothing<URL>();
916+ } else {
917+ ThrowExportsInvalid (env, " ." , target, pjson_url, base);
918+ return Nothing<URL>();
919+ }
920+ }
921+ }
922+ if (pcfg.has_main == HasMain::Yes) {
923+ URL resolved (pcfg.main , pjson_url);
924+ const std::string& path = resolved.ToFilePath ();
925+ if (CheckDescriptorAtPath (path) == FILE) {
926+ return Just (resolved);
927+ }
928+ }
929+ if (env->options ()->es_module_specifier_resolution == " node" ) {
930+ if (pcfg.has_main == HasMain::Yes) {
931+ return FinalizeResolution (env, URL (pcfg.main , pjson_url), base);
932+ } else {
933+ return FinalizeResolution (env, URL (" index" , pjson_url), base);
934+ }
935+ }
936+ if (pcfg.type != PackageType::Module) {
937+ Maybe<URL> resolved = LegacyMainResolve (pjson_url, pcfg);
938+ if (!resolved.IsNothing ()) {
939+ return resolved;
940+ }
941+ }
942+ }
943+ std::string msg = " Cannot find main entry point for " +
944+ URL (" ." , pjson_url).ToFilePath () + " imported from " +
945+ base.ToFilePath ();
946+ node::THROW_ERR_MODULE_NOT_FOUND (env, msg.c_str ());
947+ return Nothing<URL>();
948+ }
949+
890950Maybe<URL> PackageExportsResolve (Environment* env,
891951 const URL& pjson_url,
892952 const std::string& pkg_subpath,
0 commit comments