我在设置使用django压缩器替换带有数据URI的URL时出现问题。以下是相关设置:
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
]
STATICFILES_DIRS = [
os.path.join(PROJECT_DIR, 'static'),
os.path.join(BASE_DIR, 'foo', 'bar'),
os.path.join(BASE_DIR, 'foo', 'baz'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = os.environ.get('STATIC_FILE_URL', '/static/')
CSS_INCLUDE_PATHS = '.:{foo}'.format(growth=os.path.join(BASE_DIR, 'foo'))
LESSC_COMMAND = 'node_modules/.bin/lessc {include_paths} {{infile}} {{outfile}} --autoprefix="> 1%"'.format(
include_paths="--include-path='{paths}'".format(paths=CSS_INCLUDE_PATHS)
)
COMPRESS_PRECOMPILERS = (
('text/less', LESSC_COMMAND),
('text/javascript', 'node_modules/.bin/rollup {infile} -c rollup.config.js --output {outfile}'),
)
COMPRESS_CSS_FILTERS = [
'compressor.filters.css_default.CssAbsoluteFilter',
'compressor.filters.datauri.CssDataUriFilter',
]
# 5KB limit
COMPRESS_DATA_URI_MAX_SIZE = 5 * 1024
# Explicitly enabled so it works in DEBUG mode as well
COMPRESS_ENABLED = True
但是,我仍然在生成的CSS中看到图像中的非数据网址(并且肯定有一些小于5KB的图片)。
我错过了什么?
编辑:删节模板和样式
模板
<!DOCTYPE html>
<html>
<head>
{% load compress %}
{% load static from staticfiles %}
{% include "foo/head-base.html" %}
{% compress css %}
<link type="text/less" rel="stylesheet" href="{% static 'static/styles/style.less' %}" />
{% endcompress %}
</head>
<body>
<!-- loading some templates here -->
{% compress js %}
<script src="{% static 'foo/scripts/script.js' %}" type="text/javascript"></script>
{% endcompress %}
风格
// Define the root path.
@static_root: '/static';
// some styles here
// and a bunch of imports near the end
@import "foo";
@import "bar";
// ...etc