5

我目前正在使用Jawr压缩和捆绑我的 css、javascript 和图像文件。

Jawr目前正在转换我的 css 文件中的所有 url() 链接,无论它们是否是图像。例如:

@font-face {
    font-family: 'NothingYouCouldSay';
    src: url('../fonts/NothingYouCouldSay.eot') format('eot');
    src: local("☺"), url('../fonts/NothingYouCouldSay.woff') format('woff'), url("../fonts/NothingYouCouldSay.otf") format("opentype"), url('../fonts/NothingYouCouldSay.ttf') format('truetype'), url('../fonts/NothingYouCouldSay.svg') format('svg');
    font-weight: normal;
    font-style: normal;
}

Jawr 正在转换所有 url() 值,但是当网络服务器运行时找不到资源,因为我已将图像 servlet 配置为仅侦听 *.png 和 *.jpg。

@font-face {
    font-family: 'NothingYouCouldSay';
    src: url('../../../cb1130234589/resources/fonts/NothingYouCouldSay.eot') format('eot');
    src: local("☺"), url('../../../cb1130234589/resources/fonts/NothingYouCouldSay.woff') format('woff'), url("../../../cb1130234589/resources/fonts/NothingYouCouldSay.otf") format("opentype"), url('../../../cb1130234589/resources/fonts/NothingYouCouldSay.ttf') format('truetype'), url('../../../cb1130234589/resources/fonts/NothingYouCouldSay.svg') format('svg');
    font-weight: normal;
    font-style: normal;
}

如果我将 *.woff 添加到图像 servlet 映射中,则 servlet 会抱怨文件的 mime 类型不被理解。

有什么方法可以让 Jawr 不处理这些特定的特定 URL?

4

1 回答 1

2

因此,在尝试了一些不同的想法之后,我最终编写了自己的自定义后处理器来处理这个问题。我尽可能多地重用现有的 Jawr 代码,这意味着如果 Jawr 更改它的底层代码,它可能会非常脆弱。

无论如何,这是我写的:

package com.bullethq.jawr.postprocessor;

import net.jawr.web.resource.FileNameUtils;
import net.jawr.web.resource.bundle.factory.util.PathNormalizer;
import net.jawr.web.resource.bundle.postprocess.BundleProcessingStatus;
import net.jawr.web.resource.bundle.postprocess.impl.CSSURLPathRewriterPostProcessor;
import net.jawr.web.resource.bundle.postprocess.impl.PostProcessorCssImageUrlRewriter;

import java.io.IOException;

public class CustomCssUrlPathRewriterPostProcessor extends CSSURLPathRewriterPostProcessor {

    public static final String CUSTOM_URL_PATH_REWRITER = "customcsspathrewriter";

    public CustomCssUrlPathRewriterPostProcessor() {
        super(CUSTOM_URL_PATH_REWRITER);
    }

    // ========================================================================
    // ========================================================================
    // ========================================================================
    @Override
    protected PostProcessorCssImageUrlRewriter createImageUrlRewriter(BundleProcessingStatus status) {
        return new CustomPostProcessorCssImageUrlRewriter(status);
    }

    // ========================================================================
    // ========================================================================
    // ========================================================================
    public class CustomPostProcessorCssImageUrlRewriter extends PostProcessorCssImageUrlRewriter {

        public CustomPostProcessorCssImageUrlRewriter(BundleProcessingStatus status) {
            super(status);
        }

        // ========================================================================
        // ========================================================================
        // ========================================================================
        @Override
        protected String getUrlPath(String match, String originalPath, String newCssPath) throws IOException {
            String url = match.substring(match.indexOf('(') + 1, match.lastIndexOf(')')).trim();

            // Remove any quotes if necessary.
            String quoteStr = "";
            if (url.startsWith("'") || url.startsWith("\"")) {
                quoteStr = String.valueOf(url.charAt(0));
                url = url.substring(1, url.length() - 1);
            }

            // We now check if the url ends in a known image file extension
            // If not, the url is ignored.
            if (FileNameUtils.hasImageExtension(url)) {
                return super.getUrlPath(match, originalPath, newCssPath);
            } else {
                // We need to rewrite the path, as any relative URLs will
                // not resolve correctly if Jawr has changed the CSS path.
                url = PathNormalizer.concatWebPath(originalPath, url);
                url = PathNormalizer.getRelativeWebPath(PathNormalizer.getParentPath(newCssPath), url);
                return "url(" + quoteStr + url + quoteStr + ")";
            }
        }
    }
}

然后,您需要在 jawr.properties 中配置 Jawr 以使用此自定义后处理器:

jawr.custom.postprocessors.customcsspathrewriter.class=com.bullethq.jawr.postprocessor.CustomCssUrlPathRewriterPostProcessor
jawr.css.bundle.factory.filepostprocessors=customcsspathrewriter
于 2012-04-21T19:09:41.637 回答