简体   繁体   English

使用cmake打包成zip之前,请最小化CSS和Javascripts文件

[英]Minify CSS and Javascripts files before packing into zip using cmake

I am using CMake to build and pack a C++ Web Application. 我正在使用CMake构建和打包C ++ Web应用程序。 The application needs additional CSS and Javascript files. 该应用程序需要其他CSS和Javascript文件。 To ease the installation process on different machines I prepare a ZIP file and add the required files using rules similar to the following ones. 为了简化在不同计算机上的安装过程,我准备了一个ZIP文件,并使用类似于以下规则的规则添加所需文件。

# add javascript/CSS
install(DIRECTORY "${PROJECT_SOURCE_DIR}/css" DESTINATION "${THE_HTDOCS_DIR}"
    DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)

# add images/icons
install(DIRECTORY "${PROJECT_SOURCE_DIR}/ico" DESTINATION "${THE_HTDOCS_DIR}"
    DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)

What is the best way to integrate a compressor or minify tool into the CMake/CPack release process? 将压缩器或压缩工具集成到CMake / CPack发布过程中的最佳方法是什么? For example it would be nice to call the YUICompressor to compress the CSS/JS files. 例如,最好调用YUICompressor来压缩CSS / JS文件。 I haven't found any hints to solve this during my web search. 在我的网络搜索过程中,我没有找到解决此问题的任何提示。 So any hint is welcome. 因此,任何提示都是值得欢迎的。

Using ant or other build systems is not an option for me - I am aware of ant support for YUICompressor. 对我而言,不能使用ant或其他构建系统-我知道对YUICompressor的蚂蚁支持。

I needed this for myself and used CMake add_custom_command() and add_custom_target() to call the minifier. 我自己需要这个,并使用CMake add_custom_command()和add_custom_target()来调用压缩程序。 It looks for the yui-copmressor binary and compresses if CMAKE_BUILD_TYPE is not "Debug" so you have it a little easier while developing. 它查找yui-copmressor二进制文件,如果CMAKE_BUILD_TYPE不是“ Debug”,则进行压缩,因此在开发时会更轻松一些。

set(js_in_files
    foo.js
    bar.js
    baz.js
)

find_program(YUI_EXECUTABLE yui-compressor)
if(YUI_EXECUTABLE AND (NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug"))
    message(STATUS "JS files will be minified before install.")
    foreach(jsfile ${js_in_files})
        set(jsmin "${CMAKE_CURRENT_BINARY_DIR}/${jsfile}.min")
        add_custom_command(OUTPUT ${jsmin}
            COMMAND ${YUI_EXECUTABLE}
            ARGS "${CMAKE_CURRENT_SOURCE_DIR}/${jsfile}" -o "${jsmin}"
        )
        install(FILES ${jsmin}
            DESTINATION "${WEB_INSTALL_PATH}/cgi-bin/scripts/"
            RENAME ${jsfile}
        )
        set(js_out_files ${js_out_files} ${jsmin})
    endforeach(jsfile)
else()
    message(STATUS "JS files will be installed unmodified.")
    foreach(jsfile ${js_in_files})
        install(FILES ${jsfile}
            DESTINATION "${WEB_INSTALL_PATH}/cgi-bin/scripts/"
        )
        set(js_out_files ${js_out_files} ${jsfile})
    endforeach(jsfile)
endif()

add_custom_target(installjs ALL DEPENDS ${js_out_files})

Be sure you adapt the DESTINATION of the install commands. 确保调整安装命令的目的地。 ;-) ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM