Skip to content

Commit

Permalink
Added hint comment for inline injector.
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed Jul 11, 2024
1 parent 3b1d83c commit 850ccdc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
7 changes: 6 additions & 1 deletion cmake/frontend/emscripten.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ function(nui_prepare_emscripten_target)
set(NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_UNPACKED_MODE off)
endif()

set(NUI_DEFER_INLINE_SCRIPTS_TAG "nodefer")
if (NUI_DEFER_INLINE_SCRIPTS)
set(NUI_DEFER_INLINE_SCRIPTS_TAG "defer")
endif()

set(INLINER_COMMAND "")
if (NOT NO_INLINE)
nui_enable_inline(TARGET ${NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_TARGET} RELATIVE_TO ${CMAKE_CURRENT_SOURCE_DIR})
if (NOT NO_INLINE_INJECT)
set(INLINER_COMMAND COMMAND ${NUI_INLINE_INJECTOR_TARGET_FILE} "${CMAKE_BINARY_DIR}/static/index.html" "${CMAKE_BINARY_DIR}/nui-inline/inline_imports.js" "${CMAKE_BINARY_DIR}/nui-inline/inline_imports.css")
set(INLINER_COMMAND COMMAND ${NUI_INLINE_INJECTOR_TARGET_FILE} "${CMAKE_BINARY_DIR}/static/index.html" "${CMAKE_BINARY_DIR}/nui-inline/inline_imports.js" "${CMAKE_BINARY_DIR}/nui-inline/inline_imports.css" ${NUI_DEFER_INLINE_SCRIPTS_TAG})
endif()
endif()

Expand Down
7 changes: 6 additions & 1 deletion cmake/inline_extractor.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,16 @@ function(nui_enable_inline)
set(nui_enable_inline_ARGS_UNPACKED_MODE off)
endif()

set(NUI_DEFER_INLINE_SCRIPTS_TAG "nodefer")
if (NUI_DEFER_INLINE_SCRIPTS)
set(NUI_DEFER_INLINE_SCRIPTS_TAG "defer")
endif()

if (NOT ${nui_enable_inline_ARGS_UNPACKED_MODE})
add_custom_command(
OUTPUT
"${CMAKE_BINARY_DIR}/index_inserts.html"
COMMAND ${NUI_INLINE_INJECTOR_TARGET_FILE} "${CMAKE_BINARY_DIR}/module_${nui_enable_inline_ARGS_TARGET}/bin/index.html" ${INLINE_IMPORTS_SCRIPTS} ${INLINE_IMPORTS_STYLES}
COMMAND ${NUI_INLINE_INJECTOR_TARGET_FILE} "${CMAKE_BINARY_DIR}/module_${nui_enable_inline_ARGS_TARGET}/bin/index.html" ${INLINE_IMPORTS_SCRIPTS} ${INLINE_IMPORTS_STYLES} ${NUI_DEFER_INLINE_SCRIPTS_TAG}
DEPENDS
${INLINE_IMPORTS_SCRIPTS}
${INLINE_IMPORTS_STYLES}
Expand Down
1 change: 1 addition & 0 deletions cmake/options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ set(NUI_NPM "npm" CACHE STRING "Path to npm (node package manager)")
set(NUI_NODE "node" CACHE STRING "Path to node")
option(NUI_BUILD_EXAMPLES "Build examples" off)
option(NUI_ENABLE_CLANG_TIDY "Enable clang-tidy" off)
option(NUI_DEFER_INLINE_SCRIPTS "Defer inline scripts" on)

option(NUI_ENABLE_TOOLING_CONFIGURE "Enable patching and configuring of acorn and emscription? (default on)" on)
34 changes: 28 additions & 6 deletions tools/inline_injector/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ std::string readFile(const std::filesystem::path& path)

int main(int argc, char** argv)
{
if (argc != 4)
if (argc != 5)
{
std::cout << "Expected 3 argument: <index.html> <import_scripts> <import_styles>, but got " << argc - 1 << "\n";
std::cout
<< "Expected 4 argument: <index.html> <import_scripts> <import_styles> <import_scripts_defer>, but got "
<< argc - 1 << "\n";
return 1;
}

const auto index = std::filesystem::path{argv[1]};
const auto importScripts = std::filesystem::path{argv[2]};
const auto importStyles = std::filesystem::path{argv[3]};
const auto importScriptsDefer = std::string{argv[4]} == "defer" ? true : false;

std::string indexHtml;
try
Expand All @@ -48,7 +51,9 @@ int main(int argc, char** argv)
const auto binIndex =
std::filesystem::relative(index.parent_path() / ".." / "bin" / "index.js", index.parent_path());

const std::string importScriptsHtml = "\t<script type=\"module\" defer>\n\t\timport \"" +
const std::string deferTag = importScriptsDefer ? " defer" : "";

const std::string importScriptsHtml = "\t<script type=\"module\" " + deferTag + ">\n\t\timport \"" +
relativeImportScriptsFile.generic_string() + "\";\n\t</script>\n";
const std::string importStylesHtml =
"\t<style>\n\t\t@import \"" + relativeImportStylesFile.generic_string() + "\";\n\t</style>\n";
Expand All @@ -63,13 +68,30 @@ int main(int argc, char** argv)
return 1;
}

const auto headBegin = indexHtml.find("<head>");
if (headBegin == std::string::npos)
{
std::cout << "Could not find <head> in " << index << "\n";
return 1;
}

auto insertPoint = headEnd;

const std::string insertionHint = "<!-- Nui Inline Insertion Slot -->";
const auto insertHintPos = indexHtml.find(insertionHint);
if (insertHintPos != std::string::npos)
{
// insert after the hint:
insertPoint = insertHintPos + insertionHint.size();
}

// insert importScriptsHtml before headEnd:
indexHtml.insert(headEnd, importScriptsHtml);
indexHtml.insert(insertPoint, importScriptsHtml);

// insert importStylesHtml before headEnd:
indexHtml.insert(headEnd, importStylesHtml);
indexHtml.insert(insertPoint, importStylesHtml);

// insert importBinIndexHtml before headEnd:
// insert importBinIndexHtml before headEnd (always):
if (indexHtml.find(binIndex.generic_string()) == std::string::npos)
indexHtml.insert(headEnd, importBinIndexHtml);

Expand Down

0 comments on commit 850ccdc

Please sign in to comment.