All Projects → mrf345 → flask_minify

mrf345 / flask_minify

Licence: MIT license
A Flask extension to minify request's response for html, js, css and less.

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to flask minify

Html Compress Twig
Twig extension for compressing HTML and inline CSS/JS using WyriHaximus/HtmlCompress
Stars: ✭ 72 (+22.03%)
Mutual labels:  minify
Terser Webpack Plugin
Terser Plugin
Stars: ✭ 1,687 (+2759.32%)
Mutual labels:  minify
Image Shrinker
App for macOS. Minify your images and graphics with just one drop. Autorenamed in the same place where it comes from. Immediately!
Stars: ✭ 217 (+267.8%)
Mutual labels:  minify
Uglifyjs Webpack Plugin
[deprecated] UglifyJS Plugin
Stars: ✭ 1,343 (+2176.27%)
Mutual labels:  minify
Minify
Minifier of js, css, html and img
Stars: ✭ 120 (+103.39%)
Mutual labels:  minify
Instapack
All-in-one TypeScript and Sass compiler for web applications! 📦 🚀
Stars: ✭ 131 (+122.03%)
Mutual labels:  minify
Babel Plugin Const Enum
Transform TypeScript `const` enums
Stars: ✭ 38 (-35.59%)
Mutual labels:  minify
Autoptimize
Official Autoptimize repo on Github
Stars: ✭ 245 (+315.25%)
Mutual labels:  minify
Serverless Plugin Optimize
Bundle with Browserify, transpile and minify with Babel automatically to your NodeJS runtime compatible JavaScript
Stars: ✭ 122 (+106.78%)
Mutual labels:  minify
Twelvety
An Eleventy starter project built to be fast
Stars: ✭ 195 (+230.51%)
Mutual labels:  minify
Html Minifier Terser
actively maintained fork of html-minifier - minify HTML, CSS and JS code using terser - supports ES6 code
Stars: ✭ 106 (+79.66%)
Mutual labels:  minify
Tiny Html Minifier
Minify HTML in PHP with just a single class
Stars: ✭ 114 (+93.22%)
Mutual labels:  minify
Glup
Some of the gulp tutorial -《gulp笔记》
Stars: ✭ 136 (+130.51%)
Mutual labels:  minify
Embed
Static content embedding for Golang
Stars: ✭ 94 (+59.32%)
Mutual labels:  minify
Yui Css Compressor Php Port
A PHP port of the YUI CSS compressor.
Stars: ✭ 220 (+272.88%)
Mutual labels:  minify
Flask Htmlmin
Flask html response minifier
Stars: ✭ 66 (+11.86%)
Mutual labels:  minify
Minify
CSS & JavaScript minifier, in PHP. Removes whitespace, strips comments, combines files (incl. @import statements and small assets in CSS files), and optimizes/shortens a few common programming patterns.
Stars: ✭ 1,710 (+2798.31%)
Mutual labels:  minify
rollup-plugin-closure-compiler-js
Rollup plugin for optimizing JavaScript with google-closure-compiler-js.
Stars: ✭ 31 (-47.46%)
Mutual labels:  minify
Minify
Go minifiers for web formats
Stars: ✭ 2,824 (+4686.44%)
Mutual labels:  minify
Yii2 Minify View
Yii2 View component with minification css & js
Stars: ✭ 186 (+215.25%)
Mutual labels:  minify

flask_minify

Latest Release
Supported versions
Coverage Percentage security: bandit Code Style Black

Flask extension to parse and minify html, javascript, css and less.

Install:

With pip

  • pip install Flask-Minify

Or from the source

  • git clone https://github.com/mrf345/flask_minify.git
  • cd flask_minify
  • python setup.py install

Setup:

In this example the extension will minify every HTML request, unless it's explicitly bypassed.

from flask import Flask
from flask_minify import Minify

app = Flask(__name__)
Minify(app=app, html=True, js=True, cssless=True)

Another approach is using decorators, you can set the extension to be passive so will only minify the decorated routes

from flask import Flask
from flask_minify import Minify, decorators as minify_decorators

app = Flask(__name__)
Minify(app=app, passive=True)

@app.route('/')
@minify_decorators.minify(html=True, js=True, cssless=True)
def example():
  return '<h1>Example...</h1>'

Options:

Option type Description
app object Flask app instance to be passed (default: None)
html bool minify HTML (default: True)
js bool minify JavaScript output (default: True)
cssless bool minify CSS or Less. (default: True)
fail_safe bool avoid raising error while minifying (default: True)
bypass list endpoints to bypass minifying for, supports Regex (default: [])
bypass_caching list endpoints to bypass caching for, supports Regex (default: [])
caching_limit int limit the number of cached response variations (default: 2).
passive bool disable active minifying, to use decorators instead (default: False)
static bool enable minifying static files css, less and js (default: True)
script_types list script types to limit js minification to (default: [])
parsers dict parsers to handle minifying specific tags, mainly for advanced customization (default: {})

- bypass and bypass_caching

endpoint in this context is the name of the function decorated with @app.route so in the following example the endpoint will be root:

@app.route('/root/<id>')
def root(id):
    return id

both options can handle regex patterns as input so for example, if you want to bypass all routes on a certain blueprint you can just pass the pattern as such:

Minify(app, bypass=['blueprint_name.*'])

- caching_limit

if the option is set to 0, we'll not cache any response, so if you want to disable caching just do that.

- script_types

when using the option include '' (empty string) in the list to include script blocks which are missing the type attribute.

- parsers

allows passing tag specific options to the module responsible for the minification, as well as replacing the default parser with another included option or your own custom one.

In the following example will replace the default style (handles CSS) parser rcssmin with lesscpy:

from flask_minify import Minify, parsers as minify_parsers

parsers = {'style': minify_parsers.Lesscpy}

Minify(app=app, parsers=parsers)

you can override the default parser runtime options as well, as shown in the following example:

from flask_minify import Minify, parsers as minify_parsers

class CustomCssParser(minify_parsers.Lesscpy):
    runtime_options = {
        **minify_parsers.Lesscpy.runtime_options,
        "xminify": False,
    }

parsers = {'style': CustomCssParser}

Minify(app=app, parsers=parsers)

the default parsers are set to {"html": Html, "script": Jsmin, "style": Rcssmin} check out the code for more insight.

Development:

  • Tests: make test
  • Style check: make lint
  • Format code: make format

Breaking changes

0.40

Due to a future deprecation in Flask 2.3, the extension is no longer going to fallback to Flask._app_ctx_stack, it will raise an exception instead (flask_minify.exceptions.MissingApp)

0.33

introduces a breaking change to the expected output, in this release lesscpy will be replaced by cssmin as the default css minifier so no more less compiling by default. in case you don't want that, follow this example.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].