Autoprefixer过滤器不工作在Flask_Assets中 [英] Autoprefixer Filter Not Working in Flask_Assets
问题描述
我试图通过按照 Flask_Assets文档,但似乎没有应用过滤器。这里是我的代码:
$ b $> $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' {'import_name':__name__}
flask_app =烧瓶(** flask_args)
from flask_assets导入环境,包$ b $ assets =环境(flask_app)
assets.config ['AUTOPREFIXER_BIN'] ='postcss'
assets.config ['AUTOPREFIXER_BROWSERS'] = ['> 1%']
css_min = Bundle('../ styles / mycss.css',filters ='autoprefixer',output ='styles / test.css')
assets.register('css_assets' ,css_min)
@ flask_app.route('/')
def landing_page():
html ='<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 3.2 Final // EN> \
< head> {%assetscss_assets%} \
< link rel =stylesheethref ={{ASSET_URL}} \
{%endassets%} \
< h1> Hello World< / h1> \
< p>只是一个烧瓶测试< / p>'
return render_template_string(html),200
如果__name__ =='__main__':
flask_app.run(host ='0.0.0.0',port = 5000)
我已经能够成功应用cssmin,pyscss,uglifyjs和jsmin过滤器。我也可以在命令行上运行autoprefixer来成功编译转换后的输出:
postcss --use autoprefixer --autoprefixer.browsers > 1%-o test.css mycss.css
但是,当试图运行autoprefixer通过flask_assets注册,该过程既不会引发错误,也不会花费所需的时间进行编译。它确实会生成输出文件,但是当我检查生成的文件时,没有应用前缀。
更新:每当尝试为ANY过滤器配置选项时都会发生。我一直无法让uglifyjs接受'UGLIFYJS_EXTRA_ARGS',或者让pyscss过滤器使用'PYSCSS_STYLE'来采用新的样式。我尝试使用os.environ ['AUTOPREFIXER_BIN']将这些配置设置为环境变量,并尝试通过flask.config ['AUTOPREFIXER_BIN']传递它们。但是在运行过滤器时,没有应用配置设置。我也不清楚在代码本身的哪个地方,配置选项是由Bundle或Environment构建的。
一个SO帖子声称已经找到一种方法来使配置设置生效,但帖子没有显示如何设置flask_assets来获取这些选项的整个工作流程。
也许有人可以帮助我理解我在做什么错误?
Autoprefixer:
。您只是没有使用最新版本的 Autoprefixer 。如果你看看这个链接中的发行版的历史,从版本 6.0.0 ,它开始使用 postcss 。您的代码将适用于6.0.0以前的版本。
Webassets已经为版本提供了支持 /blub/master/src/webassets/film/autooprefixer.py#L60\" rel =nofollow noreferrertitle =Source Link> autoprefixer6 filter。
因此,您只需要在初始化包时更改过滤器,如下所示:
$ p code $ css_min = Bundle('../ styles / mycss.css',filters ='autoprefixer6',output ='styles / test.css' )
其他过滤器的配置:
不要使用 os.environ
,这不是为Flask和 flask-extensions 。为扩展设置配置的最常见(也是首选)方式是使用配置本身,而在大型项目中,这是使用单独的配置文件完成的。这些扩展将从flask的配置文件中取得它的配置选项。
根据你使用的扩展名,你也可以像你一样单独设置配置,但是很少使用,从我目前看到的。
请检查Flask的配置相关文档,了解如何正确设置应用程序配置的一些很好的例子。
from flask import Flask,render_template_string $ b $ from flask_assets import Environment,Bundle
#构建flask应用程序对象
flask_args = {'import_name':__name__}
flask_app = Flask(** flask_args)
$ b assets = Environment(flask_app)
#指定bin路径(可选),仅在全局安装时才需要
assets.config ['AUTOPREFIXER_BIN'] ='path / to / postcss'
assets.config ['AUTOPREFIXER_BR OWSERS'] = ['> 1%',]
#使用autoprefixer6更新过滤器
css_min = Bundle('../ styles / mycss.css',filters ='autoprefixer6',
output ='styles / test .css')
assets.register('css_assets',css_min)
$ b $ flask_app.route('/')
def landing_page():
html ='<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 3.2 Final // EN> \
< head> {%assetscss_assets%} \
< link rel =stylesheethref ={{ASSET_URL}}type =text / css> \
{%endassets%} \
< title> Hello< ; / title> \
< / head> \
< h1> Hello World< / h1> \
< p>只是一个烧瓶的测试< / p> '
返回render_template_string(html),200
$ b如果__name__ =='__main__':
flask_app.run(host ='0.0.0.0',port = 5000)
记得清理p重新生成的文件,如果源css / js没有改变,即删除输出文件,和 .webassets-cache
文件夹。
1 除了代码风格&格式化约定!
I have tried to get the autoprefixer filter to work with flask_assets by following the instructions in the Flask_Assets documentation, but it does not appear to apply the filter. Here is my code:
# construct flask app object
from flask import Flask, render_template_string
flask_args = { 'import_name': __name__ }
flask_app = Flask(**flask_args)
from flask_assets import Environment, Bundle
assets = Environment(flask_app)
assets.config['AUTOPREFIXER_BIN'] = 'postcss'
assets.config['AUTOPREFIXER_BROWSERS'] = [ '> 1%' ]
css_min = Bundle('../styles/mycss.css', filters='autoprefixer', output='styles/test.css')
assets.register('css_assets', css_min)
@flask_app.route('/')
def landing_page():
html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\
<head>{% assets "css_assets" %}\
<link rel="stylesheet" href="{{ ASSET_URL }}" type="text/css">\
{% endassets %}\
<title>Hello</title>\
</head>\
<h1>Hello World</h1>\
<p>Just a test of flask</p>'
return render_template_string(html), 200
if __name__ == '__main__':
flask_app.run(host='0.0.0.0', port=5000)
I have been able to apply the cssmin, pyscss, uglifyjs and jsmin filters successfully. I can also run autoprefixer on the command line to successfully compile a transformed output:
postcss --use autoprefixer --autoprefixer.browsers "> 1%" -o test.css mycss.css
However, when trying to run autoprefixer through flask_assets registration, the process neither throws an error nor does it seem to take the required time to compile. It does produce the output file but when I examine the resulting file, none of the prefixes have been applied.
UPDATE: This problem seems to occur whenever attempting to configure options for ANY filter. I have not been able to get uglifyjs to accept 'UGLIFYJS_EXTRA_ARGS' or for the pyscss filter to adopt a new style using 'PYSCSS_STYLE' either. I have tried to set these configuration as environmental variables using os.environ['AUTOPREFIXER_BIN'] as well as attempting to pass them through flask.config['AUTOPREFIXER_BIN']. But none of the configuration settings have been applied when the filter is run. It is also not clear to me where in the code itself the configuration options are constructed by either Bundle or Environment.
One SO post claims to have found a way to get a configuration setting to work, but the post does not show the entire workflow of how flask_assets needs to be setup to ingest these options.
Perhaps someone can help me understand what I am doing wrong?
Autoprefixer:
There is nothing wrong with your code1. You are just not using the correct filter for the latest version of Autoprefixer. If you look at the history of the releases in that link, since version 6.0.0, it started using postcss. Your code will work for versions older than 6.0.0.
Webassets has provided support for versions after 6.0.0 (inclusive), by providing the autoprefixer6
filter.
Therefore all you have to do is change the filter(s) while initializing your bundle, like so:
css_min = Bundle('../styles/mycss.css', filters='autoprefixer6', output='styles/test.css')
Other Filters' Configurations:
Don't use os.environ
, that is not the way to set configuration variables for Flask and flask-extensions. The most common (and preferred) way to set configuration for extensions is by using the flask Config itself, and in large projects this is done using a separate config file. The extensions will pickup its configuration options from flask's config.
Depending on which extension you use, you can also set the config separately like you have done, but that is rarely used, from what I have seen so far.
Please check the Flask's Configuration related documentation for some good examples on how to setup configuration for your app "properly".
from flask import Flask, render_template_string
from flask_assets import Environment, Bundle
# construct flask app object
flask_args = {'import_name': __name__}
flask_app = Flask(**flask_args)
assets = Environment(flask_app)
# specify the bin path (optional), required only if not globally installed
assets.config['AUTOPREFIXER_BIN'] = 'path/to/postcss'
assets.config['AUTOPREFIXER_BROWSERS'] = ['> 1%', ]
# use the autoprefixer6 updated filter
css_min = Bundle('../styles/mycss.css', filters='autoprefixer6',
output='styles/test.css')
assets.register('css_assets', css_min)
@flask_app.route('/')
def landing_page():
html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\
<head>{% assets "css_assets" %}\
<link rel="stylesheet" href="{{ ASSET_URL }}" type="text/css">\
{% endassets %}\
<title>Hello</title>\
</head>\
<h1>Hello World</h1>\
<p>Just a test of flask</p>'
return render_template_string(html), 200
if __name__ == '__main__':
flask_app.run(host='0.0.0.0', port=5000)
Remember to clean out the previously generated files, if the source css/js has not changed, i.e remove the output files, and the .webassets-cache
folder.
1Except for code style & formatting conventions!
这篇关于Autoprefixer过滤器不工作在Flask_Assets中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!