Code Monkey home page Code Monkey logo

flask_minify's People

Contributors

abakonski avatar ktibow avatar mrf345 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

flask_minify's Issues

Rendering of Google Recaptcha doesn't work properly after minifying.

So I'm using Flask-wtf's recaptcha field

from flask_wtf import RecaptchaField

And then I'm rendering the recaptcha field using Jinja2

{{ form.recaptcha }}

Which renders the following html:

<script src='https://www.google.com/recaptcha/api.js' async defer></script>
<div class="g-recaptcha" data-sitekey="6LernpkUAAAAAANMNwUpf3RKluSCzaZ_ll16zVZ4"></div>

For some odd reason, my page looks bad, some content is deleted. When I remove the Recaptcha everything works fine and if I don't minify the page where the Recaptcha is being rendered, everything also works fine.

I doubt this is a problem with the Google Recaptcha or Flask-wtf itself. I think this resides in how the HTML is being minified by the htmlmin library that Flask-Minify uses.

Maybe the Recaptcha HTML code is simply not robust enough to be minified or something like that.

For now, I'm just bypassing the route that renders the page with the Recaptcha field in it.

CSS grid-area values are minified into float values

Hi! I'm using Flask-Minify 0.32 which works like a charm. I've found an issue with certain CSS values:

.div2 { grid-area: 2 / 2 / 3 / 4; }
.div3 { grid-area: 1 / 2 / 2 / 3; }

these are minified into

div2{grid-area:0.08333333333333;}

for example, which breaks the grid layout. I've bypassed the routes to work around this, but I thought I'd submit this issue.

Cheers

Exception is raised with default settings if go dependency isn't installed

The description for the go option states

prefer go minifier, if optional go dependency is installed (default: True)

This suggests that if the go dependency is not installed, this option will do nothing. Instead, leaving this option as True causes the parsers to raise an exception from here:

if self.has_go_parser and not minify_go:
raise FlaskMinifyException(
f"Cannot use any Go parsers without installing "
"Go optional dependency: `pip install flask-minify[go]`"
)
@property
def has_go_parser(self):
return any(p.go for p in self.parsers.values())

If a user has a development environment that does not have the extension installed (for example, developing on windows) but still wishes to use the go extension in production, they must explicitly toggle this option based on the code environment, which isn't ideal.

Instead it would be nice if this option was ignored if the go dependency isn't found, as its description suggests it is.

In case it helps anyone else, my current workaround is:

from flask_minify import Minify, parsers
Minify(app, go=bool(parsers.minify_go))

But if it suits your deploy process, you could also do the much simpler:

Minify(app, go=not app.debug)

Some whitespace in template literals is wrongly removed

#!/usr/bin/env python3

from flask import Flask
from flask_minify import minify

app = Flask(__name__)
minify(app=app, js=True)

@app.route('/')
def index():
  return '<script>const x = 42; document.write(`foo ${x} bar`)</script>'

This produces the following response on GET /:

<script>const x=42;document.write(`foo ${x}bar`)</script>

The output is missing a space character after ${x}. The error occurs only if x is enclosed by curly brackets.

Some endpoints are causing errors in Flask-Minify

This is my setup:
minify(app=app, bypass=[r'users\.register', r'users\.contact'])
If I don't supply an argument to the bypass parameter no errors appear.

When I hit ctrl + u Chrome takes me to the source code of a specific page.

For example I'm currently on the home page (http://127.0.0.1:5000/home)
Now I press ctrl + u and it takes me to the source code of the home page
(view-source:http://127.0.0.1:5000/home)
When I do that I'm receiving the following errors:

Traceback (most recent call last): File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\app.py", line 2463, in __call__ return self.wsgi_app(environ, start_response) File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\app.py", line 2449, in wsgi_app response = self.handle_exception(e) File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\app.py", line 1866, in handle_exception reraise(exc_type, exc_value, tb) File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\_compat.py", line 39, in reraise raise value File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request return self.finalize_request(rv) File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\app.py", line 1969, in finalize_request response = self.process_response(response) File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask\app.py", line 2266, in process_response response = handler(response) File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask_minify\main.py", line 151, in main should_bypass = self.get_endpoint_matches(self.bypass) File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask_minify\main.py", line 117, in get_endpoint_matches [compile_re(pattern) for pattern in patterns] File "c:\users\anders\onedrive\computer science\python\projekter\anime-website\env\lib\site-packages\flask_minify\main.py", line 118, in <listcomp> if compiled_pattern.match(self.endpoint)] TypeError: expected string or bytes-like object

Inside the get_endpoint_matches method (def get_endpoint_matches(self, patterns)
self.endpoint returns None when the URL is: view-source:http://127.0.0.1:5000/blabla...

The method should probably just ignore endpoints that are None, and that is exactly my current workaround for now.

original:

matches = [compiled_pattern for compiled_pattern in
                   [compile_re(pattern) for pattern in patterns]
                   if compiled_pattern.match(self.endpoint)]

workaround:

matches = [compiled_pattern for compiled_pattern in
                   [compile_re(pattern) for pattern in patterns]
                   if compiled_pattern.match(self.endpoint)] if self.endpoint else []

I think that endpoints for static files are not being handled properly by Flask-Minify.

Line breaks minifying to a space

I think this may be the same issue reported in #47, but it was self-closed and I didn't find the author's solution satisfying.

Basically, I'm seeing confusing behavior about how line breaks are minified. For example, below is the content of my Jinja2 HTML template (leading spaces intentionally kept in to reflect how it looks in my code):

    <nav class="navbar bg-primary fixed-top navbar-expand-md">
      <div class="container-fluid">
        <button
          class="navbar-toggler"
          type="button"
        >
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="nav-collapse">

It got minified to the following, with spaces between the tags:

<nav class="navbar bg-primary fixed-top navbar-expand-md"> <div class="container-fluid"> <button class="navbar-toggler" type="button"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="nav-collapse">

It's not consistent, though. At the top of that same file (my top-level layout template), I have:

<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="manifest" href="/manifest.json" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="mobile-web-app-capable" content="yes" />
    <meta name="theme-color" content="#2c3e50" />

And that correctly minifies to:

<!DOCTYPE html><html lang="en"> <head><link rel="manifest" href="/manifest.json"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="mobile-web-app-capable" content="yes"><meta name="theme-color" content="#2c3e50">

Any ideas what's going on? It's not the end of the world but as long as I'm saving bytes, I want to save ALL the bytes. Thanks!

CSS rgba() alpha value

A color: rgba(202,242,255,1); directive seems to be converted to a hex value like #caf2ff01, which is not correct, it should be #caf2ffff

Decorator returns an invalid response when cache is enabled

Found an invariant condition in decorators.minify which returns an invalid response whenever caching is enabled

tests/integration.py:166: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.nox/test-3-7/lib/python3.7/site-packages/werkzeug/test.py:1006: in get
    return self.open(*args, **kw)
.nox/test-3-7/lib/python3.7/site-packages/flask/testing.py:227: in open
    follow_redirects=follow_redirects,
.nox/test-3-7/lib/python3.7/site-packages/werkzeug/test.py:970: in open
    response = self.run_wsgi_app(environ.copy(), buffered=buffered)
.nox/test-3-7/lib/python3.7/site-packages/werkzeug/test.py:861: in run_wsgi_app
    rv = run_wsgi_app(self.application, environ, buffered=buffered)
.nox/test-3-7/lib/python3.7/site-packages/werkzeug/test.py:1096: in run_wsgi_app
    app_rv = app(environ, start_response)
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:2464: in __call__
    return self.wsgi_app(environ, start_response)
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:2450: in wsgi_app
    response = self.handle_exception(e)
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:1867: in handle_exception
    reraise(exc_type, exc_value, tb)
.nox/test-3-7/lib/python3.7/site-packages/flask/_compat.py:39: in reraise
    raise value
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:2447: in wsgi_app
    response = self.full_dispatch_request()
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:1953: in full_dispatch_request
    return self.finalize_request(rv)
.nox/test-3-7/lib/python3.7/site-packages/flask/app.py:1968: in finalize_request
    response = self.make_response(rv)

Subdirectories

Files placed in subdirectories are ignored.

Example:
Processed: /app/blueprint/static/css/mycss.css
Ignored: /app/blueprint/static/css/ext/extcss.css

Not necessarily an issue, rather an improvement.

Can you use Flask-Minify with add_url_rule?

I'm using a for loop that iterates through all of my resources:

def make_sender(path, dir):
    def f():
        mimetype = magic.from_file(os.path.join(app.root_path, 'game/'+dir+path), mime=True)
        if "js" in path:
            mimetype = "application/javascript"
        if "css" in path:
            mimetype = "text/css"
        return send_from_directory(os.path.join(app.root_path, 'game/'+dir),
                                   path.replace("/", ""), mimetype=mimetype)
    return f
for file in ['/welcome.css', '/install.js']:
    app.add_url_rule(file, file, make_sender(file, "related"))

It doesn't look like Flask-Minify is working there, though. Is there a way to do that there?

UnicodeEncodeError on Python 2.7

Bump into the following error

File ".../python2.7/site-packages/Flask_Minify-0.27-py2.7.egg/flask_minify/main.py", line 227, in get_minified_or_cached
    key = hashing(content).hexdigest()
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 42768: ordinal not in range(128)

I believe the solution should be

key = hashing(content.encode('utf-8')).hexdigest()

Incompatibility with python3.13

Due the lack of cgi module in the standard library, htmlmin is not compatible with py3.13 therefore flask-minify.
htmlmin seems to be unmantained.
It import cgi in its main.py file but never use it. It is the only incompatibility that I found

html incorrectly minifying \n line breaks.

i have the minifier running on production but not dev. i noticed that in production all occurrences of line breaks do not apply. when they are templated by jinja2, the response html text in a p element literally is on a new line to reflect it. white-space: pre-line; needs to be applied to take effect. the solution is not to ignore \n's in text elements but to replace them with html <br>'s. ill get back to this when i find my solution.

Is pyyaml a needed requirement?

I might be missing something, but pyyaml is a dependency that I don't see used anywhere. Could it be removed from requirements, just to trim one dependency?

Javascript not working after minifying.

following the documentation, only the html portion was minified. my javascript code was still the same even setting js =True and my javascript isnt working properly anymore.

Please make an "init_app" method

So I'm getting these errors when running on the server (not locally)

Traceback (most recent call last):
File "/env/lib/python3.7/site-packages/flask/app.py", line 2311, in wsgi_app
response = self.full_dispatch_request()
File "/env/lib/python3.7/site-packages/flask/app.py", line 1835, in full_dispatch_request
return self.finalize_request(rv)
File "/env/lib/python3.7/site-packages/flask/app.py", line 1852, in finalize_request
response = self.process_response(response)
File "/env/lib/python3.7/site-packages/flask/app.py", line 2131, in process_response
response = handler(response)
File "/env/lib/python3.7/site-packages/flask_minify/init.py", line 65, in toLoopTag
request.url_rule.rule in self.bypass
AttributeError: 'NoneType' object has no attribute 'rule'

And I think it's because Flask-Minify extension isn't getting properly registered with the app as all the other extensions are. The extension needs to have an "init_app" method or something like that.

by the way, I'm using blueprints

Cache KeyError

In my code, I've been using flask_minify, and I've been disabling the cache (for dynamic reasons). When I saw this PR from Dependabot on my repo, I saw that the checks passed, so I merged it in. Whenever I tried to connect to the server, though, it gave me this exception:

 Traceback (most recent call last):
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
     response = self.full_dispatch_request()
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1953, in full_dispatch_request
     return self.finalize_request(rv)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1970, in finalize_request
     response = self.process_response(response)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2267, in process_response
     response = handler(response)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 273, in main
     'style'))
   File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 220, in get_minified_or_cached
     limit_reached and self.cache[self.endpoint].popitem()
 KeyError: 'something'
 Traceback (most recent call last):
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
     response = self.full_dispatch_request()
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1953, in full_dispatch_request
     return self.finalize_request(rv)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1970, in finalize_request
     response = self.process_response(response)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2267, in process_response
     response = handler(response)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 273, in main
     'style'))
   File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 220, in get_minified_or_cached
     limit_reached and self.cache[self.endpoint].popitem()
 KeyError: 'something'
 
 During handling of the above exception, another exception occurred:
 
 Traceback (most recent call last):
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1970, in finalize_request
     response = self.process_response(response)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2267, in process_response
     response = handler(response)
   File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 266, in main
     self.html))
   File "/app/.heroku/python/lib/python3.6/site-packages/flask_minify/main.py", line 220, in get_minified_or_cached
     limit_reached and self.cache[self.endpoint].popitem()
 KeyError: 'something'

What's going on here? How can I fix it?

Release .40 breaks application factories

The refactoring has resulted in applications that use init_app to throw flask_minify.exceptions.MissingApp, as the Minify object only saves app in __init__, but not init_app, and the self.app @Property method no longer has the _app_ctx_stack to fall back to. Fix it by not saving any specific app in self._app and always fall back to Flask.current_app in the self.app @Property.

Would be very useful as a decorator

I have an app which builds js assets at runtime. It would be very useful if I could apply it to a single endpoint that generates those, rather than the whole app and then try and regex all the other endpoints of the app.

Either a decorator to apply to an endpoint or a positive list vs only a negative one would be great.

how to use

I tried using flask_minify, got an error ... help.

Thanks,
Evan

Seems so easy to use ... but not sure what the error is about ...

run.log

pip install error - No such file or directory: /flask-minify/requirements/main.txt

I am using pyenv with Python 2.7

pip install Flask-Minify
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Collecting Flask-Minify
  Using cached Flask-Minify-0.27.tar.gz (6.8 kB)
    ERROR: Command errored out with exit status 1:
     command: /xxx/.pyenv/versions/2.7.18/envs/food2/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-CtdDDJ/flask-minify/setup.py'"'"'; __file__='"'"'/tmp/pip-install-CtdDDJ/flask-minify/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-Uffpej
         cwd: /tmp/pip-install-CtdDDJ/flask-minify/
    Complete output (5 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-CtdDDJ/flask-minify/setup.py", line 18, in <module>
        with open(path.join(requirements_path, 'main.txt')) as f:
    IOError: [Errno 2] No such file or directory: '/tmp/pip-install-CtdDDJ/flask-minify/requirements/main.txt'
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

I am not familiar with python package, but seems like setup.py try to access requirements/* which is not packaged in Flask-Minify-0.28.tar.gz.

It works fine if I install from the source.

Minifying AMP CSS

Hi there,

I'm using flask_minify for an AMP project. AMP requires you to tag style blocks with an HTML attribute like so: <style amp-custom>

Unfortunately this no longer matches with the <style> regex.

Deprecationwarnings

In parser.py in line 95 please migrate from "html-keep-conditional-comments" to the new version of it. If not my console will get spammed

image

Integration with Jinja2 for CSS/JS ?

The HTML gets minified which was the initial goal. But somehow the CSS and JS which are included on the JINJA2 templates are not being minified.

minify = Minify()
minify.fail_safe = False

and then after app created on app factory then minify.init_app(app)

Is this the expected result, that just html and not the CSS + JS if included on JINJA2 are not minified?

Static CSS not minifying

I noticed that for some reason, the CSS wasn't minifying. It was auto-added with app.add_url_rule. Any reasons, solutions, or stuff I could do to help debug?

case and pip install

Oddly enough I had an issue doing
pip install flask-minify

while
pip install Flask-Minify

worked

I had thought case should not matter

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.