gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:27:19 25 4
gpt4 key购买 nike

我目前正在使用 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?

最佳答案

因此,在尝试了一些不同的想法之后,我最终编写了自己的自定义后处理器来处理这个问题。我尽可能多地重用现有的 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

关于java - 如何停止 Jawr 处理 CSS 文件的某些部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10252625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com