-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a python loader for the manifest files
- Loading branch information
1 parent
ca325d1
commit 091c09c
Showing
11 changed files
with
343 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,6 @@ docs/_build/ | |
|
||
# PyBuilder | ||
target/ | ||
|
||
.idea | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
python-webpack-manifest | ||
======================= | ||
|
||
Docs pending |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nose==1.3.7 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"status": "built", | ||
"errors": null, | ||
"files": { | ||
"main": [ | ||
"foo/bar.js", | ||
"woz/bar.css", | ||
"bar/woz.png" | ||
], | ||
"foo": [ | ||
"foo/bar.js", | ||
"woz/bar.js", | ||
"bar/woz.js" | ||
], | ||
"bar": [ | ||
"foo/bar.css", | ||
"woz/bar.css", | ||
"bar/woz.js" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"status": "errors", | ||
"errors": [ | ||
"error 1", | ||
"error 2" | ||
], | ||
"files": null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"status": "building", | ||
"errors": null, | ||
"files": null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"status": "unknown status", | ||
"errors": null, | ||
"files": null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import os | ||
import unittest | ||
from webpack_manifest import webpack_manifest | ||
|
||
TEST_ROOT = os.path.dirname(__file__) | ||
|
||
|
||
class TestBundles(unittest.TestCase): | ||
def test_raises_exception_for_missing_manifest(self): | ||
self.assertRaises( | ||
webpack_manifest.WebpackManifestFileError, | ||
webpack_manifest.read, | ||
'/path/that/does/not/exist', | ||
) | ||
|
||
def test_manifest_provide_rendered_elements(self): | ||
manifest = webpack_manifest.load( | ||
os.path.join(TEST_ROOT, 'test_manifest_1.json'), | ||
static_url='/static/', | ||
) | ||
self.assertEqual(manifest.main.js, '<script src="/static/foo/bar.js"></script>') | ||
self.assertEqual(manifest.main.css, '<link rel="stylesheet" href="/static/woz/bar.css">') | ||
|
||
self.assertEqual( | ||
manifest.foo.js, | ||
( | ||
'<script src="/static/foo/bar.js"></script>' | ||
'<script src="/static/woz/bar.js"></script>' | ||
'<script src="/static/bar/woz.js"></script>' | ||
) | ||
) | ||
self.assertEqual(manifest.foo.css, '') | ||
|
||
self.assertEqual(manifest.bar.js, '<script src="/static/bar/woz.js"></script>') | ||
self.assertEqual( | ||
manifest.bar.css, | ||
( | ||
'<link rel="stylesheet" href="/static/foo/bar.css">' | ||
'<link rel="stylesheet" href="/static/woz/bar.css">' | ||
) | ||
) | ||
|
||
def test_non_trailing_slash_static_url_handled(self): | ||
manifest = webpack_manifest.load( | ||
os.path.join(TEST_ROOT, 'test_manifest_1.json'), | ||
static_url='/static', | ||
) | ||
self.assertEqual(manifest.main.js, '<script src="/static/foo/bar.js"></script>') | ||
self.assertEqual(manifest.main.css, '<link rel="stylesheet" href="/static/woz/bar.css">') | ||
|
||
def test_errors_handled(self): | ||
try: | ||
webpack_manifest.load( | ||
os.path.join(TEST_ROOT, 'test_manifest_2.json'), | ||
static_url='/static', | ||
) | ||
self.assertFalse('should not reach this') | ||
except webpack_manifest.WebpackError as e: | ||
self.assertEqual( | ||
e.message, | ||
'Webpack errors: \n\nerror 1\n\nerror 2', | ||
) | ||
|
||
def test_status_handled(self): | ||
try: | ||
webpack_manifest.load( | ||
os.path.join(TEST_ROOT, 'test_manifest_2.json'), | ||
static_url='/static', | ||
) | ||
self.assertFalse('should not reach this') | ||
except webpack_manifest.WebpackError as e: | ||
self.assertEqual( | ||
e.message, | ||
'Webpack errors: \n\nerror 1\n\nerror 2', | ||
) | ||
|
||
def test_handles_timeouts_in_debug(self): | ||
path = os.path.join(TEST_ROOT, 'test_manifest_3.json') | ||
try: | ||
webpack_manifest.load( | ||
path, | ||
static_url='/static', | ||
debug=True, | ||
timeout=1, | ||
) | ||
self.assertFalse('should not reach this') | ||
except webpack_manifest.WebpackManifestBuildingStatusTimeout as e: | ||
self.assertEqual( | ||
e.message, | ||
'Timed out reading the webpack manifest at "{}"'.format(path), | ||
) | ||
|
||
def test_handles_unknown_statuses(self): | ||
path = os.path.join(TEST_ROOT, 'test_manifest_4.json') | ||
try: | ||
webpack_manifest.load( | ||
path, | ||
static_url='/static', | ||
) | ||
self.assertFalse('should not reach this') | ||
except webpack_manifest.WebpackManifestStatusError as e: | ||
self.assertEqual( | ||
e.message, | ||
'Unknown webpack manifest status: "unknown status"', | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
""" | ||
webpack_manifest.py - https://github.com/markfinger/python-webpack-manifest | ||
The MIT License (MIT) | ||
Copyright (c) 2015 Mark Finger | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
Description | ||
----------- | ||
Manifest loader that allows you to include references to files built by webpack. | ||
Handles manifests generated by the `webpack-yam-plugin`. | ||
Usage | ||
----- | ||
``` | ||
import webpack_manifest | ||
manifest = webpack_manifest.load( | ||
# An absolute path to a manifest file | ||
path='/abs/path/to/manifest.json', | ||
# The root url that your static assets are served from | ||
static_url='/static/', | ||
# optional args... | ||
# ---------------- | ||
# Ensures that the manifest is flushed every time you call `load(...)` | ||
# If webpack is currently building, it will also delay until it's ready. | ||
# You'll want to set this to True in your development environment | ||
debug=False, | ||
# Max timeout (in seconds) that the loader will wait while webpack is building. | ||
# This setting is only used when the `debug` argument is True | ||
timeout=60, | ||
) | ||
# `load` returns a manifest object with properties that match the names of | ||
# the entries in your config. The properties matching your entries have | ||
# `js` and `css` properties that are pre-rendered strings that point to | ||
# all your JS and CSS assets. Note: If you don't name your entry, webpack | ||
# will automatically name it "main". | ||
# A string containing pre-rendered script elements for the "main" entry | ||
manifest.main.js # '<script src="/static/path/to/file.js"><script><script ... >' | ||
# A string containing pre-rendered link elements for the "main" entry | ||
manifest.main.css # '<link rel="stylesheet" href="/static/path/to/file.css"><link ... >' | ||
# A string containing pre-rendered link elements for the "vendor" entry | ||
manifest.vendor.css # '<link rel="stylesheet" href="/static/path/to/file.css"><link ... >' | ||
``` | ||
""" | ||
|
||
|
||
import os | ||
import json | ||
import time | ||
import datetime | ||
|
||
MANIFEST_CACHE = {} | ||
|
||
BUILDING_STATUS = 'building' | ||
BUILT_STATUS = 'built' | ||
ERRORS_STATUS = 'errors' | ||
|
||
|
||
def load(path, static_url, debug=False, timeout=60): | ||
if debug or path not in MANIFEST_CACHE: | ||
manifest = build(path, static_url, debug, timeout) | ||
|
||
if not debug: | ||
MANIFEST_CACHE[path] = manifest | ||
|
||
return manifest | ||
|
||
return MANIFEST_CACHE[path] | ||
|
||
|
||
def build(path, static_url, debug, timeout): | ||
start = datetime.datetime.now() | ||
timeout_delta = datetime.timedelta(seconds=timeout) | ||
|
||
data = read(path) | ||
status = data.get('status', None) | ||
|
||
if debug: | ||
# Lock up the process and wait for webpack to finish building | ||
while status == BUILDING_STATUS: | ||
time.sleep(0.1) | ||
if start + timeout_delta > datetime.datetime.now(): | ||
raise WebpackManifestBuildingStatusTimeout( | ||
'Timed out reading the webpack manifest at "{}"'.format(path) | ||
) | ||
data = read(path) | ||
status = data.get('status', None) | ||
|
||
if status == ERRORS_STATUS: | ||
raise WebpackError( | ||
'Webpack errors: \n\n{}'.format( | ||
'\n\n'.join(data['errors']) | ||
) | ||
) | ||
|
||
if status != BUILT_STATUS: | ||
raise WebpackManifestStatusError('Unknown webpack manifest status: "{}"'.format(status)) | ||
|
||
return WebpackManifest(data['files'], static_url) | ||
|
||
|
||
class WebpackManifest(object): | ||
def __init__(self, files, static_url): | ||
for entry in files: | ||
manifest_entry = WebpackManifestEntry(files[entry], static_url) | ||
setattr(self, entry, manifest_entry) | ||
|
||
|
||
class WebpackManifestEntry(object): | ||
def __init__(self, rel_paths, static_url): | ||
self.js = '' | ||
self.css = '' | ||
|
||
# Frameworks tend to be inconsistent about what they | ||
# allow with regards to static urls | ||
if not static_url.endswith('/'): | ||
static_url += '/' | ||
|
||
# Build strings of elements that can be dumped into a template | ||
for rel_path in rel_paths: | ||
name, ext = os.path.splitext(rel_path) | ||
rel_url = '/'.join(rel_path.split(os.path.sep)) | ||
if ext == '.js': | ||
self.js += '<script src="{}{}"></script>'.format(static_url, rel_url) | ||
elif ext == '.css': | ||
self.css += '<link rel="stylesheet" href="{}{}">'.format(static_url, rel_url) | ||
|
||
self._contents = rel_paths | ||
self._static_url = static_url | ||
|
||
|
||
def read(path): | ||
if not os.path.isfile(path): | ||
raise WebpackManifestFileError('Path "{}" is not a file or does not exist'.format(path)) | ||
|
||
if not os.path.isabs(path): | ||
raise WebpackManifestFileError('Path "{}" is not an absolute path to a file'.format(path)) | ||
|
||
with open(path, 'r') as manifest_file: | ||
content = manifest_file.read().encode('utf-8') | ||
|
||
return json.loads(content) | ||
|
||
|
||
class WebpackManifestFileError(Exception): | ||
pass | ||
|
||
|
||
class WebpackError(Exception): | ||
pass | ||
|
||
|
||
class WebpackManifestStatusError(Exception): | ||
pass | ||
|
||
|
||
class WebpackManifestBuildingStatusTimeout(Exception): | ||
pass | ||
|