首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何停止Jawr处理CSS文件的某些部分?

如何停止Jawr处理CSS文件的某些部分?
EN

Stack Overflow用户
提问于 2012-04-21 03:48:31
回答 1查看 1.5K关注 0票数 5

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

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

代码语言:javascript
复制
@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()值,但是在when服务器运行时找不到资源,因为我已经将图像servlet配置为只侦听*.png & *.jpg。

代码语言:javascript
复制
@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?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-22 03:09:42

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

不管怎样,这是我写的:

代码语言:javascript
复制
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以使用此自定义后处理器:

代码语言:javascript
复制
jawr.custom.postprocessors.customcsspathrewriter.class=com.bullethq.jawr.postprocessor.CustomCssUrlPathRewriterPostProcessor
jawr.css.bundle.factory.filepostprocessors=customcsspathrewriter
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10252625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档