Coder Social home page Coder Social logo

jingo-minify's Introduction

Jingo Minify

Jingo Minify is DEPRECATED

In version 1.8, Django added support for multiple template engines, and provided a Jinja2 backend. The django-jinja project leverages that to support Jinja2, while Jingo does not.

django-jinja is recommended for new projects. Jingo >=0.8 supports Django 1.8, but it will not be maintained beyond version 0.9, and will not support Django 1.9 or above. If you're already using Jingo, and not ready to make the switch, Jingo should continue to work for now, though not without some effort.

0.9 will be the last release of Jingo, unless a new maintainer comes along with a new direction.

Since Jingo is no longer maintained, Jingo Minify is also deprecated.

As of 0.9, Jingo's built-in helpers are provided via a Jinja2 extension to simplify moving away from Jingo. The entire jingo/ext.py file can be copied into another project, or referenced as 'jingo.ext.JingoExtension'. Used in this way, Jingo plays nicely with django-jinja (and theoretically Django's built-in Jinja2 backend).

Jingo Minify is an CSS/JS bundler and minifier for use with Jingo, a connector to use Jinja2 templates with Django.

image

Installing Jingo Minify

Requirements

  • Django 1.4
  • Jingo and Jinja2. Jingo Minify is not designed for Django templates.

One of the following:

  • Java. If you want to use YUI Compressor.
  • NodeJS. If you want to use UglifyJS and clean-css.

Optionally:

  • less. Jingo Minify supports less, if you have lessc available.
  • sass. Jingo Minify supports sass, if you have sass available.
  • stylus. Jingo Minify supports stylus, if you have stylus available.

Installation

Configure the following settings:

# Jingo Minify uses the YUICompressor internally, so needs Java.
JAVA_BIN = '/path/to/java'

# If you want to use less, set this:
LESS_BIN = '/path/to/lessc' # Probably just 'lessc'

# If you want to use sass, set this:
SASS_BIN = '/path/to/sass'

# If you want to use node-based minifiers, set these:
UGLIFY_BIN = '/path/to/uglifyjs' # Probably just 'uglify'
CLEANCSS_BIN = '/path/to/cleancss' # Probably just 'cleancss'

# If you want to use a specific git executable, set this:
GIT_BIN = '/path/to/git'  # Default to 'git'

# If you use a different git root for assets
JINGO_MINIFY_ASSETS_GIT_ROOT = '.'

# If you want a different JINGO_MINIFY_ROOT than static
JINGO_MINIFY_ROOT = '/var/www/example.com/static/'

# Add jingo_minify to INSTALLED_APPS
INSTALLED_APPS = (
    # ...
    'jingo_minify',
    # ...
)

# This is the important part.
MINIFY_BUNDLES = {
    'css': {},
    'js': {},
}

Note: If you're using Django 1.4, but want to use MEDIA_ROOT and MEDIA_URL for static assets instead of conventional Django 1.4 STATIC_ROOT and STATIC_URL, you should also set:

JINGO_MINIFY_USE_STATIC = False

Configuring

Jingo Minify deals with bundles, which lets you organize your code into multiple files but combine them into very few groups for your users to download.

Bundles are set up in the MINIFY_BUNDLES setting. For example:

MINIFY_BUNDLES = {
    'css': {
        'common': (
            'css/reset.css',
            'css/layout.css',
        ),
    },
    'js': {
        'common': (
            'js/lib/jquery.js',
            'js/common.js',
        ),
    },
}

This creates one CSS bundle and one JS bundle, both called common. The file paths are relative to the MEDIA_ROOT setting.

You can create any number or combination of CSS and JS bundles, and include any number of files in each, but do not create empty bundles.

Using Bundled Files

For development, you probably don't want to rebundle the files all the time. Just set

TEMPLATE_DEBUG = True

in your settings, and Jingo Minify will automatically use the uncompressed files. Set TEMPLATE_DEBUG to False to use the bundled versions.

In Templates

To include a bundle in a template, use either the css or js functions. For example:

{# My Jinja2 Template #}
<html>
<head>
  <title>My Page</title>
  {{ css('common') }}
</head>
<body>
  <h1>My page</h1>
  {{ js('common') }}
</body>
</html>

This will include the code (<link> and <script> tags) to include the bundles on the page. It will generate the HTML for either the individual files or the bundled files based on TEMPLATE_DEBUG.

Media Types

The css() helper will, by default, generate <link> tags with a media attribute set to screen,projection,tv. You can override this by passing an optional second parameter to the css() helper, e.g.:

{{ css('print-bundle', 'print') }}

This would create a <link> tag with media="print".

Bundling and Minifying

To bundle and minify your CSS and JS, run the management command:

./manage.py compress_assets

This will create two files per bundle in your media directory, one that looks like bundle-all.js (or .css) and one that looks like bundle-min.js. Only the *-min.* version will be used. It also creates a file called build.py along side manage.py that contains unique IDs based on the SHA of the current git checkout.

Minifier Options

You can choose between YUICompressor (Java) or UglifyJS/clean-css (node) for minifying. You don't have to do anything to get YUICompressor working.

If you want to use the node counterparts, just add UGLIFY_BIN and CLEANCSS_BIN (set to the correct paths, of course) to your settings.py. You can see the actual syntax if you look at the Installation section of this README.

Cache Busting Individual Images

Depending on your CDN, you may need to cache-bust assets referenced in the CSS. To do this, add the following to your settings:

CACHEBUST_IMGS = True

It will go through your CSS, and find any reference to local resources. It will append the short id for the commit that most recently modified the resource, so that it only cache busts resources that are actually modified.

The list of images that couldn't be found can be displayed by running the command with --verbosity=2 (or -v2).

manage.py compress_assets -v2

Note

This is off by default. It does a lot of I/O, so be careful if you have large amounts of massive images. Additionally, it uses a hash of the file. This isn't 100% collision proof, but it should be more than good enough.

Using LESS

If you want to use less files and have LESS_BIN defined, LESS is supported automatically in a few ways.

  • To use a LESS file, simply include a file in a CSS bundle that ends with .less.
  • For development, if you want to use the LESS JavaScript runtime compiler, you'll have to figure out how to include it in your project.
  • If you want to compile LESS on the server, even in development, add a setting: LESS_PREPROCESS = True. Your LESS files will be recompiled on every request.
  • In production, LESS files are automatically compiled before being bundled with the rest of the CSS.

Using SASS or Stylus

If you want to use sass or stylus files, you must define `SASS_BIN or STYLUS_BIN, respectively.

  • To use a SASS or Stylus file, simply include a file in a CSS bundle that ends with .sass or .scss (SASS) or .styl (Stylus).
  • Your SASS/Stylus files, if changed, will be recompiled on every request -even in development.
  • In production, Sass/Stylus files are automatically compiled before being bundled with the rest of the CSS.

Running tests

To run the tests:

$ python run_tests.py

jingo-minify's People

Contributors

cvan avatar davedash avatar diox avatar gkoberger avatar jbalogh avatar jlongster avatar jsocol avatar kumar303 avatar mythmon avatar nickolay avatar oremj avatar ozten avatar potch avatar rehandalal avatar ubernostrum avatar vinyll avatar willkg avatar yohanboniface 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

Watchers

 avatar  avatar  avatar  avatar  avatar

jingo-minify's Issues

UnicodeDecodeError with inline_css()

/jingo_minify/helpers.py in _build_html

return jinja2.Markup('\n'.join((wrapping % item for item in items)))

The string that could not be encoded/decoded was: ,tv">���/*!

Add inline_js Method

Is there any reason to avoid inline JavaScript? Could save a request for tiny JS file content.

the media-attribute

In https://github.com/jsocol/jingo-minify/blob/master/jingo_minify/helpers.py the media-attribute is set to screen,projection,tv if it's not set by the media-param.

  1. Is there any particular reason for this?
  2. For performance it's better to just include one css-file and specify the media inside of that with eg. @media print{} or/and @media handheld {}.
  3. I think it would be more appropriate to not add the media-attribute at all, In HTML5 If the media attribute is omitted, it defaults to "all". [http://developers.whatwg.org/semantics.html#attr-link-media].

Tests fail locally when TEMPLATE_DEBUG is True

If TEMPLATE_DEBUG is True, then jingo-minify will generate URLs to individual static files instead of generating a minified file name.

If DEBUG is False, then jingo-minify will not use the staticfiles finders to generate the path to a file.

If both of these statements are true, and you are depending on the staticfiles finders in your development install, then an exception will be thrown when jingo-minify tries to find the last-modified time of a non-existent file.

Django forces DEBUG to be False when the tests are running, and TEMPLATE_DEBUG is often true in local settings on development installs. Thus the issue!

A possible fix would be to rely on only one or the other, and not both settings, but I don't know if there are any implications with that.

Minifying CSS Files w/ @media Rules Breaks Rules

Take the following media query for example:

only screen and (max-width: 760px)

After minification with standard minifier, the following is output:

only screen and(max-width: 760px)

The missing space after and breaks the media query :(

Files should not be created when LESS et al. fail

This is a general issue, but I'm going to use a specific case to demonstrate. In Kitsune, if LESS is not installed, or LESS_BIN is misconfigured and jingo-minify can't find LESS, than those style files are still generated. In particular, for every *.less, file, an empty *.less.css file is generated.

This is a problem because even when LESS is installed after this, those empty files cause jingo-minify to not regenerate that style file. This is frustrating and difficult to track down.

Ideally one or both of these would happen:

  • If the command to compile a style file (*.less, for example) fails (return code != 0), thent he output file should not be created.
  • If the output file exists, but is 0 bytes, the compilation command will be called anyways (currently jingo-minify doesn't regenerate the file unless the source has been changed.)

Serve less files with rel=stylesheet/less

jingo-minify generates link nodes for .less files with the same attributes as CSS. Instead, do this:

<link rel="stylesheet/less" type="text/css" href="styles.less">

(courtesy of lesscss.org)

More command line options

Make it so we can override all settings via the command line. For example:

manage.py compress_assets --cachebust_imgs

refactor less/stylus/sass support

There are a lot of preprocessors out there and currently support for them is hard-coded into jingo-minify. This really needs a better solution.

At a minimum, it should get abstracted out. Maybe allow for plugin support for additional things so we don't have to maintain them all, too.

LESS compile broken in DEV mode using staticfiles

It saves the built files to the static root dir instead of putting them next to the original files like it does in non-staticfiles mode. When DEBUG=True Django doesn't serve anything from the STATIC_ROOT dir. I believe I've found the issue and I should have a PR ready shortly.

Support less via less.js during development

If I add some less files to a bundle and set TEMPLATE_DEBUG=True, jingo-minify should add a script tag for less.js as well, so that the less files are interpreted on the fly. This keeps me from having to install node.js and lessc and all that jazz unless I want to.

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.