flask-assets 可以将多个 css/js 文件合并为一个文件。
pip install Flask-Assets
如果需要将其压缩为min需要安装cssmin、jimin
pip install cssmin jsmin
如果项目用scss
pip install libsass
app = Flask(__name__)
assets_init(app)
assets = Environment(app)
css = Bundle('scss/*.scss', output='dist/style.css', filters='libsass,cssmin')
assets.register("css", css)
js = Bundle("js/*.js", output="dist/index.js", filters="jsmin")
assets.register("js", js)
html里这样写
<head>
<meta charset="UTF-8"/>
<title>Title</title>
{% assets 'css' %}
<link rel="stylesheet" href="{{ ASSET_URL }}" />
{% endassets %} {% assets 'js' %}
<script type="text/javascript" src="{{ ASSET_URL }}"></script>
{% endassets %}
</head>
原方式引进,需要在python中写入
css.build()
js.build()
<link href="{{ url_for('static', filename='dist/style.css') }}" rel="stylesheet">
<script src="{{ url_for('static', filename='dist/index.js') }}"></script>
# 调试环境不打包
app.config['ASSETS_DEBUG'] = True