Skip to content

Commit 8ecac86

Browse files
committed
Add a Web export option to name the HTML file index.html
If this option is set to true, all the exported files will be named as the export path value is set (e.g. project name), except for the HTML file that will be named index.html
1 parent be587f3 commit 8ecac86

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

editor/export/editor_export.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,14 @@ void EditorExport::load_config() {
378378
preset->set_include_filter(config->get_value(section, "include_filter"));
379379
preset->set_exclude_filter(config->get_value(section, "exclude_filter"));
380380
preset->set_export_path(config->get_value(section, "export_path", ""));
381+
382+
#ifndef DISABLE_DEPRECATED
383+
// Compatibility with Web export paths before 4.7: .html extension is no longer required.
384+
if (platform == "Web" && preset->get_export_path().get_extension() == "html") {
385+
preset->set_export_path(preset->get_export_path().get_basename());
386+
}
387+
#endif
388+
381389
preset->set_script_export_mode(config->get_value(section, "script_export_mode", EditorExportPreset::MODE_SCRIPT_BINARY_TOKENS_COMPRESSED));
382390
preset->set_patches(config->get_value(section, "patches", Vector<String>()));
383391

platform/web/doc_classes/EditorExportPlatformWeb.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
Additional HTML tags to include inside the [code]&lt;head&gt;[/code], such as [code]&lt;meta&gt;[/code] tags.
4242
[b]Note:[/b] You do not need to add a [code]&lt;title&gt;[/code] tag, as it is automatically included based on the project's name.
4343
</member>
44+
<member name="html/name_html_file_as_index" type="bool" setter="" getter="">
45+
If [code]true[/code], the exported HTML file will be named [code]index.html[/code] instead of using the export base name. This is useful when deploying to web servers that expect [code]index.html[/code] as the default entry point.
46+
</member>
4447
<member name="progressive_web_app/background_color" type="Color" setter="" getter="">
4548
The background color used behind the web application.
4649
</member>

platform/web/export/export_plugin.cpp

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

481483
List<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;

platform/web/export/export_plugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class EditorExportPlatformWeb : public EditorExportPlatform {
113113
Error _write_or_error(const uint8_t *p_content, int p_len, String p_path);
114114

115115
Error _export_project(const Ref<EditorExportPreset> &p_preset, int p_debug_flags);
116-
Error _launch_browser(const String &p_bind_host, uint16_t p_bind_port, bool p_use_tls);
116+
Error _launch_browser(const Ref<EditorExportPreset> &p_preset, const String &p_bind_host, uint16_t p_bind_port, bool p_use_tls);
117117
Error _start_server(const String &p_bind_host, uint16_t p_bind_port, bool p_use_tls);
118118
Error _stop_server();
119119

0 commit comments

Comments
 (0)