简体   繁体   中英

Minify CSS and Javascripts files before packing into zip using cmake

I am using CMake to build and pack a C++ Web Application. The application needs additional CSS and Javascript files. 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.

# 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? For example it would be nice to call the YUICompressor to compress the CSS/JS files. 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.

I needed this for myself and used CMake add_custom_command() and add_custom_target() to call the minifier. 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.

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. ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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