Skip to content

Commit acae887

Browse files
added config loader for webpack and view helper for hashed assets (#174)
* added config loader for webpack and view helper for hashed assets * cleanup * rm javascript_tag, replaced with asset_path * ASSETS instead of assets * added better error messages
1 parent cc2f337 commit acae887

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

baseframe/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import absolute_import
44
from pytz import timezone, UTC
55
from flask import g, Blueprint, request, current_app
6+
import json
67
from coaster.assets import split_namespec
78
from flask_assets import Environment, Bundle
89
from flask_caching import Cache
@@ -172,6 +173,23 @@ def init_app(self, app, requires=[], ext_requires=[], bundle_js=None, bundle_css
172173
app.assets.register('css_all', css_all)
173174
app.register_blueprint(self, static_subdomain=subdomain)
174175

176+
# Optional config for a client app to use a manifest file
177+
# to load fingerprinted assets
178+
# If used with webpack, the client app is expected to specify its own webpack.config.js
179+
# Set `ASSETS_MANIFEST_PATH` in `app.config` to the path for `manifest.json`.
180+
# Eg: "static/build/manifest.json"
181+
# Set `ASSET_BASE_PATH` in `app.config` to the path in which the compiled assets are present.
182+
# Eg: "static/build"
183+
if app.config.get('ASSET_MANIFEST_PATH'):
184+
# Load assets into config from a manifest file
185+
with app.open_resource(app.config['ASSET_MANIFEST_PATH']) as f:
186+
asset_bundles = json.loads(f.read())
187+
if app.config.get('assets'):
188+
raise ValueError("Loading assets via a manifest file needs the `ASSETS` config key to be unused")
189+
app.config['assets'] = {}
190+
for asset_key, asset_path in asset_bundles['assets'].items():
191+
app.config['assets'][asset_key] = asset_path
192+
175193
app.config.setdefault('CACHE_KEY_PREFIX', 'flask_cache_' + app.name + '/')
176194
nwcacheconfig = dict(app.config)
177195
nwcacheconfig['CACHE_KEY_PREFIX'] = 'networkbar_'

baseframe/views.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,22 @@ def ext_assets(assets):
8585
return gen_assets_url(assets)
8686

8787

88+
def asset_path(bundle_key):
89+
if not current_app.config.get('ASSET_BASE_PATH'):
90+
raise LookupError("Missing base path for assets. Ensure `ASSET_BASE_PATH` is set to the path where the asset can be found. Eg: `/static/`")
91+
if not current_app.config.get('assets').get(bundle_key):
92+
raise LookupError("Missing asset file for {bundle}.".format(bundle=bundle_key))
93+
return '{path}/{bundle}'.format(
94+
path=current_app.config['ASSET_BASE_PATH'],
95+
bundle=current_app.config['assets'][bundle_key])
96+
97+
8898
@baseframe.app_context_processor
8999
def baseframe_context():
90100
return {
91101
'networkbar_links': networkbar_links,
92102
'csrf_token': generate_csrf,
103+
'asset_path': asset_path,
93104
}
94105

95106

0 commit comments

Comments
 (0)