• This repository has been archived on 31/Oct/2019
  • Stars
    star
    338
  • Rank 124,916 (Top 3 %)
  • Language
    Python
  • License
    GNU General Publi...
  • Created over 9 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Flask extension to manage assets with Webpack.

Hey! This repo is no longer supported!

Flask-Webpack has been deprecated in favor of: https://github.com/nickjj/flask-static-digest

The new Flask-Static-Digest extension is easier to use, easier to configure, works with any (or no) asset build tool and is completely stand alone.

Instead of depending on Webpack to handle md5 tagging assets and creating a manifest, Flask-Static-Digest does all of that and now it even gzips your files too.

This makes it a general purpose extension that you can use to make your static files production ready.

This will be the final commit to this project. Farewell Flask-Webpack, you were a good friend!




PyPI version Build status

What is Flask-Webpack?

Managing assets can be a serious burden. Here's just a few things you get by using this package:

  • Minify assets
  • Attach vendor prefixes to your CSS automatically
  • Optimize image sizes
  • Leverage the CommonJS module system to organize your Javascript
  • Compile Markdown
  • Compile 20+ client side template languages
  • Compile LESS, SASS and any other CSS pre-processor you can imagine
  • Compile Typescript, Coffeescript and any other to-javascript language
  • Compile Ecmascript 6 (ES) down to ES 5
  • Compile React JSX to JS with hot module reloading
  • Near instant compile times, ~20-50ms is common on my workstation
  • Optionally get source maps in development mode
  • Serve your assets from a tricked out local development asset server
  • Cache all assets forever because their file names get md5-tagged
  • The only runtime you need other than Python is NodeJS
  • Never deal with file watchers again because it's all taken care of for you
  • And much more...

All of the features above are the direct result of using Webpack to manage your assets. The huge win here besides the obvious is that the functionality is outside of this package.

That means you have free reign to pick and choose what you want without having to worry about Flask-Webpack versions. If a new Webpack plugin becomes available, you can use it immediately.

What does this package do then?

It sets up a few template tags so you can access the assets inside of your jinja templates.

It means you can type this:

<img src="{{ asset_url_for('images/hamburger.svg') }}" alt="Hamburger">

...and once your jinja template has been compiled, you will see this:

<img src="images/hamburger.d2cb0dda3e8313b990e8dcf5e25d2d0f.svg" alt="Hamburger">

Now you can happily tell your frontend proxy to cache that hamburger image for an entire year. If you ever change the hamburger, the md5 will change but you do not need to change any of your templates because the asset_url_for tag knows how to look it up.

Global template tags

  • asset_url_for(asset_relative_path) to resolve an asset name
  • javascript_tag(*asset_relative_paths) to write out 1 or more script tags
  • stylesheet_tag(*asset_relative_paths) to write out 1 or more stylesheet tags

Both the javascript and stylesheet tags accept multiple arguments. If you give it more than argument it will create as many tags as needed.

Installation

pip install Flask-Webpack

Quick start

from flask import Flask
from flask_webpack import Webpack

webpack = Webpack()

app = Flask(__name__)
webpack.init_app(app)

You can view a complete working example in the test app.

There's also a blog post and short video explaining how to use this extension.

How does it work?

It expects you to have built a manifest file and it handles the rest. You can build this manifest file using a plugin I wrote for Webpack. You can find that plugin here.

This process is done automatically upon starting the dev asset server or building your assets to prepare for a production release. All of that is taken care of in the webpack.config.js file.

Settings

Flask-Webpack is configured like most Flask extensions. Here's the available options:

  • WEBPACK_MANIFEST_PATH: default None
    • Required: You may consider using ./build/manifest.json, it's up to you.
  • WEBPACK_ASSETS_URL: default publicPath from the webpack.config.js file
    • Optional: Use this asset url instead of the publicPath.
    • You would set this to your full domain name or CDN in production.

Learn more

Webpack knowledge

Most of what you'll need to learn is related to Webpack specifically but the example app in this repo is enough to get you started. Here's a few resources to help you get started with Webpack:

Help! My assets do not work outside of development

I see, so basically the problem is you're using the url() function in your stylesheets and are referencing a relative path to an asset, such as:

src: url('../../fonts/CoolFont.eot')

The above works in development mode because that's where the file is located but in production mode the asset is not there. The asset_url_for template helper handles all of this for you on the server side but now you need some assistance on the client side as well.

You have a few options here depending on if you're using CSS, SASS or something else. If you're using straight CSS you will need to pre-prend all of your paths with a special identifier.

If you were to re-write the example from above, it would now be:

src: url('~!file!../../fonts/CoolFont.eot')

That will automatically get expanded to a path that works in every environment.

If you're using SASS you can create your own function to make things easier to work with on a day to day basis. Something like this should suffice:

@function asset-url($path) {
  @return url('~!file!' + $path);
}

Now you can call it like this and everything will work:

src: asset-url('../../fonts/CoolFont.eot')

Feel free to make additional helper functions that let you abstract away the relative prefix such as font-url or image-url. It really depends on how your assets are set up.

Contributors

More Repositories

1

docker-django-example

A production ready example Django app that's using Docker and Docker Compose.
Python
1,148
star
2

build-a-saas-app-with-flask

Learn how to build a production ready web app with Flask and Docker.
HTML
950
star
3

docker-rails-example

A production ready example Rails app that's using Docker and Docker Compose.
Ruby
945
star
4

dotfiles

Settings for various tools I use.
Shell
938
star
5

ansible-docker

Install / Configure Docker and Docker Compose using Ansible.
Python
744
star
6

orats

Opinionated rails application templates.
Ruby
665
star
7

docker-flask-example

A production ready example Flask app that's using Docker and Docker Compose.
Python
583
star
8

ansigenome

A tool to help you gather information and manage your Ansible roles.
Python
447
star
9

rolespec

A test library for testing Ansible roles
Shell
232
star
10

docker-web-framework-examples

Example apps that demonstate how to use Docker with your favorite web frameworks.
Elixir
217
star
11

docker-phoenix-example

A production ready example Phoenix app that's using Docker and Docker Compose.
Elixir
207
star
12

docker-node-example

An example Node / Express app that's using Docker and Docker Compose.
Shell
200
star
13

flask-static-digest

Flask extension to help make your static files production ready by md5 tagging and gzipping them.
Python
154
star
14

notes

A zero dependency shell script that makes it really simple to manage your text notes.
Shell
129
star
15

manifest-revision-webpack-plugin

Write out a manifest file containing your versioned webpack chunks and assets.
JavaScript
124
star
16

flask-db

A Flask CLI extension to help migrate and manage your SQL database.
Python
75
star
17

ansible-nginx

Install and configure nginx (SSL A+ by default) with Ansible.
Jinja
73
star
18

ansible-acme-sh

Install and auto-renew SSL certificates with Let's Encrypt using acme.sh.
71
star
19

wait-until

A zero dependency Bash script that waits until a command of your choosing has run successfully.
Shell
57
star
20

webserver

A zero dependency Python 3 web server to echo back an HTTP request's headers and data.
Python
50
star
21

ansible-user

Create and configure a user for SSH key based logins and passwordless sudo.
48
star
22

dockercon21-docker-best-practices

Reference links for my live demo talk from DockerCon 21.
47
star
23

ansible-fail2ban

Install and configure fail2ban using ansible.
46
star
24

runninginproduction.com

The website for the Running in Production podcast.
HTML
41
star
25

esbuild-copy-static-files

An esbuild plugin to copy static files that changed from a source directory to a destination directory.
Shell
37
star
26

flask-pg-extras

A Flask extension to obtain useful information from your PostgreSQL database.
Python
33
star
27

ansible-swapfile

Create and configure a swap file with Ansible.
32
star
28

ansible-letsencrypt

Install and auto-renew SSL certificates with Let's Encrypt and Ansible.
Python
29
star
29

invoice

Calculate a billable amount, hours and days logged for 1 or more projects.
Shell
18
star
30

nyhackr-cli-dev-env

Reference notes for the Creating a Command Line Driven Development Environment talk.
18
star
31

ansible-playbooks

A collection of ansible playbooks with end to end examples.
Shell
18
star
32

ansible-rails

Deploy a rails application using git with ansible.
Ruby
18
star
33

gowatcher

Reload a specified go program automatically by monitoring a directory.
Shell
16
star
34

docker-community-all-hands

Reference links to every talk I've given for the Docker Community All-Hands events.
15
star
35

deploy-web-apps-with-docker

Rescue yourself from the complexity of DevOps
Dockerfile
15
star
36

title-case-converter

A CLI tool to capitalize words based on industry standard style guides.
Python
14
star
37

sublime-text-3-packages

A list of my Sublime Text 3 packages along with their settings.
Python
14
star
38

flask-secrets

A Flask CLI extension to generate random secret tokens.
Python
14
star
39

verdiff

A CLI tool to diff 2 versions of a Phoenix, Rails, Django or Laravel generated project.
Python
12
star
40

ansible-security

Configure ssh and ufw as well as install fail2ban with ansible.
12
star
41

gemshine

Recursively compare a ruby project's gem versions to their latest versions.
Ruby
12
star
42

ansible-iptables

Configure iptables using Ansible.
12
star
43

lcurl

Visit a site every X seconds in a loop to help detect downtime while testing deployment strategies.
Shell
12
star
44

ansible-sshd

Install and configure openssh-server using Ansible.
12
star
45

demo-for-chattanooga-python-user-group

A demo app for a talk I gave at the Chattanooga python user group.
JavaScript
10
star
46

latest-releases

A command line tool that lets you keep tabs on the latest releases of your favorite tools and libraries.
Shell
10
star
47

ansible-monit

Install monit and configure as many processes as you want with ansible.
8
star
48

passify

A small utility to create a password and wrap bcrypt.
JavaScript
8
star
49

pick-random-youtube-comments

Get a list of top level comments from a YouTube video and then pick N amount of unique comment authors by choosing them randomly.
Python
8
star
50

ansible-bootstrap

Configure a server to run Ansible and install essential packages.
6
star
51

ansible-ferm

Manage iptables with ferm using ansible.
5
star
52

ansible-postgres

Install a bare bones version of postgres with ansible.
5
star
53

ansible-pumacorn

Manage a puma or unicorn rails process with init.d using ansible.
Ruby
5
star
54

docker-faye

A docker image for running a secure Faye (websocket) server.
JavaScript
5
star
55

ansible-nodejs

Install the latest stable version of nodejs with ansible.
4
star
56

ansible-mariadb

Install and configure MariaDB using Ansible.
4
star
57

ansible-sendy

Copy and configure Sendy with Ansible.
C
3
star
58

ansible-locale

Install and configure your system's locale using ansible
3
star
59

ansible-phpfpm

Install and configure php-fpm using Ansible.
2
star
60

ansible-dnsmasq

Install and configure dnsmasq to map a TLD to localhost using ansible.
2
star
61

docker-play-example

A production ready example Play app that's using Docker and Docker Compose.
1
star