Automate Eclipse “Yui Compressor…”

前端 未结 4 1227
既然无缘
既然无缘 2021-01-14 00:17

Eclipse PDT has this handy built-in Yui Compressor in the context menu for files. But when building a webapp that uses multiple such files,

4条回答
  •  轮回少年
    2021-01-14 00:48

    You can select and "build" js/css resources by configuring and adding an External Tool with a batch (for Windows users).

    This makes it possible to minify by just pressing one button. (It is not part of a run configuration.)

    Preconditions: - Java has to be installed. - You must download yuicompressor. (Currently the official download page is here: https://github.com/yui/yuicompressor/releases )

    Step1: Add "yuicompressor" and "minify.bat" (given below) to your project.

    Step2: Configure an External Tool named "Minify" (Go to menu Run -> External Tools -> External Tools Configurations).

    Step3: Select the resources (js/css) or the directory that contains them, and click "Minify". The js/css files will be deployed to the sibling "min" directory.

    (minify.bat)

    @echo off
    
    set target=%1
    set yui=%2\WebContent\build\yuicompressor\yuicompressor-2.4.8.jar
    
    :: %1 selected resource
    :: %2 current project
    
    :: dir
    if exist %target%\ (
      cd /d %target%
      mkdir min 2>nul
    
      for %%f in (*.js *.css) do (
        echo Minifying "%%~ff"
        java -jar %yui% %%f -o min\%%f
      ) 
    
      goto end
    ) 
    
    :: .js/.css
    set pathNoExt=0
    for /f %%i in ('dir /b %target%') do set pathNoExt=%%~ni
    
    if not %pathNoExt%==0 (
      cd /d %~dp1
      mkdir min 2>nul
    
      for /f %%f in ('dir /b %pathNoExt%.js %pathNoExt%.css') do (
        echo Minifying "%%~ff"
        java -jar %yui% %%f -o min\%%f
      )
    )
    
    :end
    echo Minified
    

提交回复
热议问题