@@ -21,15 +21,18 @@ std::string readFile(const std::filesystem::path& path)
21
21
22
22
int main (int argc, char ** argv)
23
23
{
24
- if (argc != 4 )
24
+ if (argc != 5 )
25
25
{
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 " ;
27
29
return 1 ;
28
30
}
29
31
30
32
const auto index = std::filesystem::path{argv[1 ]};
31
33
const auto importScripts = std::filesystem::path{argv[2 ]};
32
34
const auto importStyles = std::filesystem::path{argv[3 ]};
35
+ const auto importScriptsDefer = std::string{argv[4 ]} == " defer" ? true : false ;
33
36
34
37
std::string indexHtml;
35
38
try
@@ -48,7 +51,9 @@ int main(int argc, char** argv)
48
51
const auto binIndex =
49
52
std::filesystem::relative (index.parent_path () / " .." / " bin" / " index.js" , index.parent_path ());
50
53
51
- const std::string importScriptsHtml = " \t <script type=\" module\" defer>\n\t\t import \" " +
54
+ const std::string deferTag = importScriptsDefer ? " defer" : " " ;
55
+
56
+ const std::string importScriptsHtml = " \t <script type=\" module\" " + deferTag + " >\n\t\t import \" " +
52
57
relativeImportScriptsFile.generic_string () + " \" ;\n\t </script>\n " ;
53
58
const std::string importStylesHtml =
54
59
" \t <style>\n\t\t @import \" " + relativeImportStylesFile.generic_string () + " \" ;\n\t </style>\n " ;
@@ -63,13 +68,30 @@ int main(int argc, char** argv)
63
68
return 1 ;
64
69
}
65
70
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
+
66
88
// insert importScriptsHtml before headEnd:
67
- indexHtml.insert (headEnd , importScriptsHtml);
89
+ indexHtml.insert (insertPoint , importScriptsHtml);
68
90
69
91
// insert importStylesHtml before headEnd:
70
- indexHtml.insert (headEnd , importStylesHtml);
92
+ indexHtml.insert (insertPoint , importStylesHtml);
71
93
72
- // insert importBinIndexHtml before headEnd:
94
+ // insert importBinIndexHtml before headEnd (always) :
73
95
if (indexHtml.find (binIndex.generic_string ()) == std::string::npos)
74
96
indexHtml.insert (headEnd, importBinIndexHtml);
75
97
0 commit comments