Skip to content

Commit 6cfb6e3

Browse files
authored
Merge pull request #115 from NuiCpp/feat/inline_injector_hint
Added option to add hint comment for inline injector position
2 parents 97c000d + 850ccdc commit 6cfb6e3

File tree

5 files changed

+75
-10
lines changed

5 files changed

+75
-10
lines changed

cmake/frontend/emscripten.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ function(nui_prepare_emscripten_target)
1818
set(NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_UNPACKED_MODE off)
1919
endif()
2020

21+
set(NUI_DEFER_INLINE_SCRIPTS_TAG "nodefer")
22+
if (NUI_DEFER_INLINE_SCRIPTS)
23+
set(NUI_DEFER_INLINE_SCRIPTS_TAG "defer")
24+
endif()
25+
2126
set(INLINER_COMMAND "")
2227
if (NOT NO_INLINE)
2328
nui_enable_inline(TARGET ${NUI_PREPARE_EMSCRIPTEN_TARGET_ARGS_TARGET} RELATIVE_TO ${CMAKE_CURRENT_SOURCE_DIR})
2429
if (NOT NO_INLINE_INJECT)
25-
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")
30+
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})
2631
endif()
2732
endif()
2833

cmake/inline_extractor.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,16 @@ function(nui_enable_inline)
9999
set(nui_enable_inline_ARGS_UNPACKED_MODE off)
100100
endif()
101101

102+
set(NUI_DEFER_INLINE_SCRIPTS_TAG "nodefer")
103+
if (NUI_DEFER_INLINE_SCRIPTS)
104+
set(NUI_DEFER_INLINE_SCRIPTS_TAG "defer")
105+
endif()
106+
102107
if (NOT ${nui_enable_inline_ARGS_UNPACKED_MODE})
103108
add_custom_command(
104109
OUTPUT
105110
"${CMAKE_BINARY_DIR}/index_inserts.html"
106-
COMMAND ${NUI_INLINE_INJECTOR_TARGET_FILE} "${CMAKE_BINARY_DIR}/module_${nui_enable_inline_ARGS_TARGET}/bin/index.html" ${INLINE_IMPORTS_SCRIPTS} ${INLINE_IMPORTS_STYLES}
111+
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}
107112
DEPENDS
108113
${INLINE_IMPORTS_SCRIPTS}
109114
${INLINE_IMPORTS_STYLES}

cmake/options.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ set(NUI_NPM "npm" CACHE STRING "Path to npm (node package manager)")
55
set(NUI_NODE "node" CACHE STRING "Path to node")
66
option(NUI_BUILD_EXAMPLES "Build examples" off)
77
option(NUI_ENABLE_CLANG_TIDY "Enable clang-tidy" off)
8+
option(NUI_DEFER_INLINE_SCRIPTS "Defer inline scripts" on)
89

910
option(NUI_ENABLE_TOOLING_CONFIGURE "Enable patching and configuring of acorn and emscription? (default on)" on)

nui/include/nui/frontend/event_system/observed_value.hpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,38 @@ namespace Nui
11141114
return *this;
11151115
}
11161116
};
1117+
template <typename... Parameters>
1118+
class Observed<std::list<Parameters...>> : public ObservedContainer<std::list<Parameters...>>
1119+
{
1120+
public:
1121+
using ObservedContainer<std::list<Parameters...>>::ObservedContainer;
1122+
using ObservedContainer<std::list<Parameters...>>::operator=;
1123+
using ObservedContainer<std::list<Parameters...>>::operator->;
1124+
static constexpr auto isRandomAccess = false;
1125+
1126+
public:
1127+
Observed()
1128+
: ObservedContainer<std::list<Parameters...>>{RangeEventContext{0, true}}
1129+
{}
1130+
template <typename T = std::list<Parameters...>>
1131+
explicit Observed(T&& t)
1132+
: ObservedContainer<std::list<Parameters...>>{
1133+
std::forward<T>(t),
1134+
RangeEventContext{static_cast<long>(t.size()), true}}
1135+
{}
1136+
1137+
Observed<std::list<Parameters...>>& operator=(std::list<Parameters...> const& contained)
1138+
{
1139+
ObservedContainer<std::list<Parameters...>>::operator=(contained);
1140+
return *this;
1141+
}
1142+
Observed<std::list<Parameters...>>& operator=(std::list<Parameters...>&& contained)
1143+
{
1144+
ObservedContainer<std::list<Parameters...>>::operator=(std::move(contained));
1145+
return *this;
1146+
}
1147+
};
1148+
11171149
template <>
11181150
class Observed<void> : public ObservedBase
11191151
{
@@ -1165,8 +1197,8 @@ namespace Nui
11651197
return observedValue;
11661198
}
11671199
template <typename T>
1168-
inline auto operator--(ModifiableObserved<T>& observedValue, int)
1169-
-> Detail::PickFirst_t<T, decltype(std::declval<T>()--)>
1200+
inline auto
1201+
operator--(ModifiableObserved<T>& observedValue, int) -> Detail::PickFirst_t<T, decltype(std::declval<T>()--)>
11701202
{
11711203
auto tmp = observedValue.value();
11721204
--observedValue.value();

tools/inline_injector/main.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ std::string readFile(const std::filesystem::path& path)
2121

2222
int main(int argc, char** argv)
2323
{
24-
if (argc != 4)
24+
if (argc != 5)
2525
{
26-
std::cout << "Expected 3 argument: <index.html> <import_scripts> <import_styles>, but got " << argc - 1 << "\n";
26+
std::cout
27+
<< "Expected 4 argument: <index.html> <import_scripts> <import_styles> <import_scripts_defer>, but got "
28+
<< argc - 1 << "\n";
2729
return 1;
2830
}
2931

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

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

51-
const std::string importScriptsHtml = "\t<script type=\"module\" defer>\n\t\timport \"" +
54+
const std::string deferTag = importScriptsDefer ? " defer" : "";
55+
56+
const std::string importScriptsHtml = "\t<script type=\"module\" " + deferTag + ">\n\t\timport \"" +
5257
relativeImportScriptsFile.generic_string() + "\";\n\t</script>\n";
5358
const std::string importStylesHtml =
5459
"\t<style>\n\t\t@import \"" + relativeImportStylesFile.generic_string() + "\";\n\t</style>\n";
@@ -63,13 +68,30 @@ int main(int argc, char** argv)
6368
return 1;
6469
}
6570

71+
const auto headBegin = indexHtml.find("<head>");
72+
if (headBegin == std::string::npos)
73+
{
74+
std::cout << "Could not find <head> in " << index << "\n";
75+
return 1;
76+
}
77+
78+
auto insertPoint = headEnd;
79+
80+
const std::string insertionHint = "<!-- Nui Inline Insertion Slot -->";
81+
const auto insertHintPos = indexHtml.find(insertionHint);
82+
if (insertHintPos != std::string::npos)
83+
{
84+
// insert after the hint:
85+
insertPoint = insertHintPos + insertionHint.size();
86+
}
87+
6688
// insert importScriptsHtml before headEnd:
67-
indexHtml.insert(headEnd, importScriptsHtml);
89+
indexHtml.insert(insertPoint, importScriptsHtml);
6890

6991
// insert importStylesHtml before headEnd:
70-
indexHtml.insert(headEnd, importStylesHtml);
92+
indexHtml.insert(insertPoint, importStylesHtml);
7193

72-
// insert importBinIndexHtml before headEnd:
94+
// insert importBinIndexHtml before headEnd (always):
7395
if (indexHtml.find(binIndex.generic_string()) == std::string::npos)
7496
indexHtml.insert(headEnd, importBinIndexHtml);
7597

0 commit comments

Comments
 (0)