@@ -232,6 +232,7 @@ Error EditorExportPlatformWeb::_build_pwa(const Ref<EditorExportPreset> &p_prese
232232 // Service worker
233233 const String dir = p_path.get_base_dir ();
234234 const String name = p_path.get_file ().get_basename ();
235+ const String html_filename = ((bool )p_preset->get (" html/name_html_file_as_index" )) ? " index.html" : (name + " .html" );
235236 bool extensions = (bool )p_preset->get (" variant/extensions_support" );
236237 bool ensure_crossorigin_isolation_headers = (bool )p_preset->get (" progressive_web_app/ensure_cross_origin_isolation_headers" );
237238 HashMap<String, String> replaces;
@@ -242,7 +243,7 @@ Error EditorExportPlatformWeb::_build_pwa(const Ref<EditorExportPreset> &p_prese
242243
243244 // Files cached during worker install.
244245 Array cache_files = {
245- name + " .html " ,
246+ html_filename ,
246247 name + " .js" ,
247248 name + " .offline.html"
248249 };
@@ -306,7 +307,7 @@ Error EditorExportPlatformWeb::_build_pwa(const Ref<EditorExportPreset> &p_prese
306307
307308 Dictionary manifest;
308309 manifest[" name" ] = proj_name;
309- manifest[" start_url" ] = " ./" + name + " .html " ;
310+ manifest[" start_url" ] = " ./" + html_filename ;
310311 manifest[" display" ] = String::utf8 (modes[display]);
311312 manifest[" orientation" ] = String::utf8 (orientations[orientation]);
312313 manifest[" background_color" ] = " #" + p_preset->get (" progressive_web_app/background_color" ).operator Color ().to_html (false );
@@ -379,6 +380,7 @@ void EditorExportPlatformWeb::get_export_options(List<ExportOption> *r_options)
379380 r_options->push_back (ExportOption (PropertyInfo (Variant::INT, " html/canvas_resize_policy" , PROPERTY_HINT_ENUM, " None,Project,Adaptive" ), 2 ));
380381 r_options->push_back (ExportOption (PropertyInfo (Variant::BOOL, " html/focus_canvas_on_start" ), true ));
381382 r_options->push_back (ExportOption (PropertyInfo (Variant::BOOL, " html/experimental_virtual_keyboard" ), false ));
383+ r_options->push_back (ExportOption (PropertyInfo (Variant::BOOL, " html/name_html_file_as_index" ), false ));
382384 r_options->push_back (ExportOption (PropertyInfo (Variant::BOOL, " progressive_web_app/enabled" ), false ));
383385 r_options->push_back (ExportOption (PropertyInfo (Variant::BOOL, " progressive_web_app/ensure_cross_origin_isolation_headers" ), true ));
384386 r_options->push_back (ExportOption (PropertyInfo (Variant::STRING, " progressive_web_app/offline_page" , PROPERTY_HINT_FILE, " *.html" ), " " ));
@@ -480,7 +482,6 @@ bool EditorExportPlatformWeb::has_valid_project_configuration(const Ref<EditorEx
480482
481483List<String> EditorExportPlatformWeb::get_binary_extensions (const Ref<EditorExportPreset> &p_preset) const {
482484 List<String> list;
483- list.push_back (" html" );
484485 return list;
485486}
486487
@@ -567,15 +568,33 @@ Error EditorExportPlatformWeb::export_project(const Ref<EditorExportPreset> &p_p
567568 f->get_buffer (html.ptrw (), html.size ());
568569 f.unref (); // close file.
569570
571+ // Remove the template-extracted HTML now that it's been read into memory.
572+ // It will be rewritten with processed content to the final output path.
573+ if (custom_html.is_empty ()) {
574+ DirAccess::remove_file_or_error (html_path);
575+ }
576+
570577 // Generate HTML file with replaced strings.
571578 _fix_html (html, p_preset, base_name, p_debug, p_flags, shared_objects, file_sizes);
572- Error err = _write_or_error (html.ptr (), html.size (), p_path);
579+
580+ const bool name_as_index = (bool )p_preset->get (" html/name_html_file_as_index" );
581+ const String html_output_path = name_as_index ? base_dir.path_join (" index.html" ) : base_path + " .html" ;
582+
583+ Error err = _write_or_error (html.ptr (), html.size (), html_output_path);
573584 if (err != OK) {
574585 // Message is supplied by the subroutine method.
575586 return err;
576587 }
577588 html.resize (0 );
578589
590+ // Remove leftover index.html from a previous export that had the option enabled.
591+ if (!name_as_index && base_name != " index" ) {
592+ const String index_html_path = base_dir.path_join (" index.html" );
593+ if (FileAccess::exists (index_html_path)) {
594+ DirAccess::remove_file_or_error (index_html_path);
595+ }
596+ }
597+
579598 // Export splash (why?)
580599 Ref<Image> splash = _get_project_splash (p_preset);
581600 const String splash_png_path = base_path + " .png" ;
@@ -793,7 +812,7 @@ Error EditorExportPlatformWeb::run(const Ref<EditorExportPreset> &p_preset, int
793812 if (err != OK) {
794813 return err;
795814 }
796- return _launch_browser (bind_host, bind_port, use_tls);
815+ return _launch_browser (p_preset, bind_host, bind_port, use_tls);
797816 } break ;
798817
799818 // Start HTTP Server.
@@ -819,7 +838,7 @@ Error EditorExportPlatformWeb::run(const Ref<EditorExportPreset> &p_preset, int
819838 if (err != OK) {
820839 return err;
821840 }
822- return _launch_browser (bind_host, bind_port, use_tls);
841+ return _launch_browser (p_preset, bind_host, bind_port, use_tls);
823842 } break ;
824843
825844 // Re-export Project.
@@ -854,7 +873,7 @@ Error EditorExportPlatformWeb::_export_project(const Ref<EditorExportPreset> &p_
854873 }
855874
856875 const String basepath = dest.path_join (" tmp_js_export" );
857- Error err = export_project (p_preset, true , basepath + " .html " , p_debug_flags);
876+ Error err = export_project (p_preset, true , basepath, p_debug_flags);
858877 if (err != OK) {
859878 // Export generates several files, clean them up on failure.
860879 DirAccess::remove_file_or_error (basepath + " .html" );
@@ -873,8 +892,10 @@ Error EditorExportPlatformWeb::_export_project(const Ref<EditorExportPreset> &p_
873892 return err;
874893}
875894
876- Error EditorExportPlatformWeb::_launch_browser (const String &p_bind_host, const uint16_t p_bind_port, const bool p_use_tls) {
877- OS::get_singleton ()->shell_open (String ((p_use_tls ? " https://" : " http://" ) + p_bind_host + " :" + itos (p_bind_port) + " /tmp_js_export.html" ));
895+ Error EditorExportPlatformWeb::_launch_browser (const Ref<EditorExportPreset> &p_preset, const String &p_bind_host, const uint16_t p_bind_port, const bool p_use_tls) {
896+ const bool name_as_index = (bool )p_preset->get (" html/name_html_file_as_index" );
897+ const String html_file = name_as_index ? " index.html" : " tmp_js_export.html" ;
898+ OS::get_singleton ()->shell_open (String ((p_use_tls ? " https://" : " http://" ) + p_bind_host + " :" + itos (p_bind_port) + " /" + html_file));
878899 // FIXME: Find out how to clean up export files after running the successfully
879900 // exported game. Might not be trivial.
880901 return OK;
0 commit comments