-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from NuiCpp/inline_js
Inline js
- Loading branch information
Showing
24 changed files
with
1,684 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
function(nui_preprocess_inline_js) | ||
set(one_value_args TARGET SOURCE OUTPUT DIRECTORY INLINE_CACHE DEPENDS IS_FIRST) | ||
set(multi_value_args EXTRA_CXX_FLAGS) | ||
cmake_parse_arguments(CPP "" "${one_value_args}" "${multi_value_args}" ${ARGN}) | ||
|
||
string(TOUPPER ${CMAKE_BUILD_TYPE} build_type) | ||
string(REPLACE " " ";" c_flags "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${build_type}}") | ||
|
||
add_custom_command( | ||
OUTPUT ${CPP_OUTPUT} | ||
COMMAND ${CMAKE_COMMAND} -E make_directory ${CPP_DIRECTORY} | ||
COMMAND ${CMAKE_CXX_COMPILER} | ||
#"-D$<JOIN:$<TARGET_PROPERTY:${CPP_TARGET},COMPILE_DEFINITIONS>,;-D>" | ||
"-I$<JOIN:$<TARGET_PROPERTY:${CPP_TARGET},INCLUDE_DIRECTORIES>,;-I>" | ||
${c_flags} | ||
$<TARGET_PROPERTY:${CPP_TARGET},COMPILE_OPTIONS> | ||
${CPP_EXTRA_CXX_FLAGS} | ||
-E ${CPP_SOURCE} -o ${CPP_OUTPUT} | ||
# COMMAND "${NUI_INLINE_EXTRACTOR_TARGET_FILE}" ${CPP_INLINE_CACHE} ${CPP_OUTPUT} ${CPP_IS_FIRST} | ||
COMMAND_EXPAND_LISTS VERBATIM | ||
IMPLICIT_DEPENDS C ${CPP_SOURCE} | ||
DEPENDS ${CPP_SOURCE} ${CPP_DEPENDS}) | ||
endfunction() | ||
|
||
function(nui_enable_inline) | ||
set(one_value_args TARGET UNPACKED_MODE) | ||
set(multi_value_args) | ||
cmake_parse_arguments(nui_enable_inline_ARGS "" "${one_value_args}" "${multi_value_args}" ${ARGN}) | ||
|
||
get_target_property(INLINE_JS_SOURCES ${nui_enable_inline_ARGS_TARGET} SOURCES) | ||
|
||
set(INLINE_DIRECTORY_SUBDIR "nui-inline") | ||
set(INLINE_DIRECTORY "${CMAKE_BINARY_DIR}/${INLINE_DIRECTORY_SUBDIR}") | ||
set(INLINE_CACHE "${INLINE_DIRECTORY}/inline.cache") | ||
set(INLINE_IMPORTS_SCRIPTS "${INLINE_DIRECTORY}/inline_imports.js") | ||
set(INLINE_IMPORTS_STYLES "${INLINE_DIRECTORY}/inline_imports.css") | ||
|
||
# for each source file preprocess it: | ||
set(IS_FIRST TRUE) | ||
foreach(source_file ${INLINE_JS_SOURCES}) | ||
get_filename_component(source_file_name "${source_file}" NAME) | ||
set(preprocessed_source_file "${INLINE_DIRECTORY}/${source_file_name}.i") | ||
nui_preprocess_inline_js( | ||
TARGET ${nui_enable_inline_ARGS_TARGET} | ||
DIRECTORY "${INLINE_DIRECTORY}" | ||
INLINE_CACHE "${INLINE_CACHE}" | ||
SOURCE "${source_file}" | ||
OUTPUT "${preprocessed_source_file}" | ||
EXTRA_CXX_FLAGS -P -CC -DNUI_INLINE -DNUI_MODULE_SOURCE_DIR="${CMAKE_SOURCE_DIR}" -DNUI_MODULE_CURRENT_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}" | ||
IS_FIRST ${IS_FIRST} | ||
) | ||
set(IS_FIRST FALSE) | ||
message(STATUS "Preprocessing ${source_file} to ${preprocessed_source_file}") | ||
list(APPEND preprocessed_sources "${preprocessed_source_file}") | ||
endforeach() | ||
|
||
add_custom_command( | ||
OUTPUT | ||
${INLINE_IMPORTS_SCRIPTS} | ||
${INLINE_IMPORTS_STYLES} | ||
COMMAND ${CMAKE_COMMAND} -E make_directory ${INLINE_DIRECTORY} | ||
COMMAND "${NUI_INLINE_EXTRACTOR_TARGET_FILE}" ${INLINE_CACHE} ${CMAKE_BINARY_DIR} ${INLINE_DIRECTORY_SUBDIR} ${preprocessed_sources} | ||
COMMAND_EXPAND_LISTS VERBATIM | ||
DEPENDS ${preprocessed_sources} | ||
) | ||
|
||
add_custom_target( | ||
nui-inline-${nui_enable_inline_ARGS_TARGET} | ||
ALL | ||
DEPENDS ${INLINE_IMPORTS_SCRIPTS} | ||
DEPENDS ${INLINE_IMPORTS_STYLES} | ||
) | ||
add_dependencies(${nui_enable_inline_ARGS_TARGET} nui-inline-${nui_enable_inline_ARGS_TARGET}) | ||
|
||
if (NOT nui_enable_inline_ARGS_UNPACKED_MODE) | ||
set(nui_enable_inline_ARGS_UNPACKED_MODE off) | ||
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} | ||
DEPENDS | ||
${INLINE_IMPORTS_SCRIPTS} | ||
${INLINE_IMPORTS_STYLES} | ||
"${CMAKE_BINARY_DIR}/module_${nui_enable_inline_ARGS_TARGET}/bin/index.html" | ||
) | ||
|
||
add_custom_target( | ||
nui-inline-inject-${nui_enable_inline_ARGS_TARGET} | ||
ALL | ||
DEPENDS "${CMAKE_BINARY_DIR}/index_inserts.html" | ||
) | ||
endif() | ||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
cmake_minimum_required(VERSION 3.16) | ||
|
||
project(inline-injector VERSION 0.1.0) | ||
|
||
add_executable(inline-injector main.cpp) | ||
target_compile_features(inline-injector PRIVATE cxx_std_20) | ||
|
||
set_target_properties(inline-injector | ||
PROPERTIES | ||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tools_bin" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <string> | ||
#include <filesystem> | ||
|
||
std::string readFile(const std::filesystem::path& path) | ||
{ | ||
std::ifstream file{path, std::ios_base::binary}; | ||
if (!file) | ||
{ | ||
throw std::runtime_error{"Could not open " + path.string()}; | ||
} | ||
|
||
file.seekg(0, std::ios::end); | ||
std::string content; | ||
content.resize(file.tellg()); | ||
file.seekg(0, std::ios::beg); | ||
file.read(&content[0], content.size()); | ||
return content; | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
if (argc != 4) | ||
{ | ||
std::cout << "Expected 3 argument: <index.html> <import_scripts> <import_styles>, 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]}; | ||
|
||
std::string indexHtml; | ||
try | ||
{ | ||
indexHtml = readFile(index); | ||
} | ||
catch (const std::exception& e) | ||
{ | ||
std::cout << "Error reading file: " << e.what() << "\n"; | ||
return 1; | ||
} | ||
|
||
// make relative path of import scripts to index file: | ||
const auto relativeImportScriptsFile = std::filesystem::relative(importScripts, index.parent_path()); | ||
const auto relativeImportStylesFile = std::filesystem::relative(importStyles, index.parent_path()); | ||
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 \"" + | ||
relativeImportScriptsFile.generic_string() + "\";\n</script>\n"; | ||
const std::string importStylesHtml = | ||
"\t<style>\n\t\t@import \"" + relativeImportStylesFile.generic_string() + "\";\n</style>\n"; | ||
const std::string importBinIndexHtml = | ||
"\t<script type=\"module\" defer>\n\t\timport \"" + binIndex.generic_string() + "\";\n</script>\n"; | ||
|
||
// find end of header </head> from behind in indexHtml: | ||
const auto headEnd = indexHtml.rfind("</head>"); | ||
if (headEnd == std::string::npos) | ||
{ | ||
std::cout << "Could not find </head> in " << index << "\n"; | ||
return 1; | ||
} | ||
|
||
// insert importScriptsHtml before headEnd: | ||
indexHtml.insert(headEnd, importScriptsHtml); | ||
|
||
// insert importStylesHtml before headEnd: | ||
indexHtml.insert(headEnd, importStylesHtml); | ||
|
||
// insert importBinIndexHtml before headEnd: | ||
if (indexHtml.find(binIndex.generic_string()) == std::string::npos) | ||
indexHtml.insert(headEnd, importBinIndexHtml); | ||
|
||
// write indexHtml back to index file: | ||
std::ofstream file{index, std::ios_base::binary}; | ||
if (!file) | ||
{ | ||
std::cout << "Could not open " << index << " for writing\n"; | ||
return 1; | ||
} | ||
|
||
file.write(indexHtml.data(), indexHtml.size()); | ||
return 0; | ||
} |
Oops, something went wrong.