我目前正在使用Jawr压缩并 bundle 我的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()值,但是随后在Web服务器运行时找不到资源,因为我已将图像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;
}
如果我在图像servlet映射中添加* .woff,则servlet会提示该文件的mime类型无法理解。
有什么办法可以让Jawr不处理这些特定的URL?
最佳答案
因此,在尝试了几种不同的想法之后,我最终编写了自己的自定义后处理器来处理此问题。我将尽可能多地重用现有的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