WebUI.cmake 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Builds the React web UI via npm at CMake build time, into ${CMAKE_BINARY_DIR}/webui/dist.
  2. find_program(NPM_EXECUTABLE npm)
  3. if(NOT NPM_EXECUTABLE)
  4. message(WARNING "npm not found — skipping web UI build (set BUILD_WEBUI=OFF to silence)")
  5. return()
  6. endif()
  7. set(WEBUI_SRC "${CMAKE_SOURCE_DIR}/webui")
  8. set(WEBUI_DIST "${CMAKE_BINARY_DIR}/webui/dist")
  9. # Install dependencies once (when node_modules is absent).
  10. add_custom_command(
  11. OUTPUT "${WEBUI_SRC}/node_modules/.package-lock.json"
  12. COMMAND ${NPM_EXECUTABLE} ci --no-audit --no-fund
  13. WORKING_DIRECTORY "${WEBUI_SRC}"
  14. COMMENT "webui: npm ci"
  15. VERBATIM)
  16. # Everything the bundle is built from. Without these as DEPENDS the output
  17. # already exists after the first build, so editing a page or component would
  18. # never rebuild the UI and the server would keep serving a stale bundle.
  19. file(GLOB_RECURSE WEBUI_SOURCES CONFIGURE_DEPENDS
  20. "${WEBUI_SRC}/src/*"
  21. "${WEBUI_SRC}/index.html"
  22. "${WEBUI_SRC}/package.json"
  23. "${WEBUI_SRC}/package-lock.json"
  24. "${WEBUI_SRC}/vite.config.ts"
  25. "${WEBUI_SRC}/tsconfig*.json")
  26. # Build the UI into the CMake build tree (outDir override).
  27. add_custom_command(
  28. OUTPUT "${WEBUI_DIST}/index.html"
  29. COMMAND ${NPM_EXECUTABLE} run build -- --outDir "${WEBUI_DIST}" --emptyOutDir
  30. WORKING_DIRECTORY "${WEBUI_SRC}"
  31. DEPENDS "${WEBUI_SRC}/node_modules/.package-lock.json" ${WEBUI_SOURCES}
  32. COMMENT "webui: npm run build -> ${WEBUI_DIST}"
  33. VERBATIM)
  34. add_custom_target(webui ALL DEPENDS "${WEBUI_DIST}/index.html")