Skip to content
This repository has been archived by the owner on Oct 23, 2019. It is now read-only.

Support collectstatic #39

Merged
merged 2 commits into from
Jan 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,13 @@ a match.
### Output paths

Be aware that the `output.path` property on config objects is overridden automatically, you can leave
the setting undefined and webpack-build will redirect all output to your `STATIC_ROOT`.
the setting undefined and webpack-build will redirect all output to your `OUTPUT_ROOT`.

To avoid file name collisions, builds are uniquely identified by hashing the options object sent to
webpack-build. By default, your assets are placed in a directory equivalent to

```python
os.path.join(STATIC_ROOT, 'webpack_assets', options_hash)
os.path.join(OUTPUT_ROOT, 'webpack_assets', options_hash)
```


Expand Down Expand Up @@ -384,7 +384,7 @@ from webpack.conf import settings
DEBUG = True

settings.configure(
STATIC_ROOT='/path/to/your/projects/static_root',
OUTPUT_ROOT='/path/to/your/output_root',
STATIC_URL='/static/',
WATCH=DEBUG,
HMR=DEBUG,
Expand All @@ -396,10 +396,10 @@ your settings file. python-webpack introspects Django's settings during startup
from the `WEBPACK` dictionary.


### STATIC_ROOT
### OUTPUT_ROOT

An absolute path to the root directory that you use for static assets. For example,
`'/path/to/your/projects/static_root'`.
`'/path/to/your/output_root'`.

This setting **must** be defined.

Expand Down Expand Up @@ -516,7 +516,7 @@ Default: `None`

### OUTPUT_DIR

The directory in `STATIC_ROOT` which webpack will output all assets to.
The directory in `OUTPUT_ROOT` which webpack will output all assets to.

Default: `'webpack_assets'`

Expand Down Expand Up @@ -569,7 +569,7 @@ Configure webpack to respect your project's configuration

```python
WEBPACK = {
'STATIC_ROOT': STATIC_ROOT,
'OUTPUT_ROOT': '/path/to/your/output_root',
'STATIC_URL': STATIC_URL,
'WATCH': DEBUG,
'HMR': DEBUG,
Expand Down
7 changes: 4 additions & 3 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
)

BUNDLES = os.path.join(BASE_DIR, 'bundles',)
OUTPUT_ROOT = os.path.join(BASE_DIR, 'generated_assets')

WEBPACK = {
'STATIC_ROOT': STATIC_ROOT,
'OUTPUT_ROOT': OUTPUT_ROOT,
'STATIC_URL': STATIC_URL,
'CONTEXT': {
'default_context': 'test'
Expand All @@ -36,7 +37,7 @@
# While webpack-build's cache will check for asset existence,
# watching compilers do not, so we need to ensure that the cache
# is cleared between runs
'CACHE_DIR': os.path.join(STATIC_ROOT, 'cache_dir'),
'CACHE_DIR': os.path.join(OUTPUT_ROOT, 'cache_dir'),
}


Expand All @@ -45,4 +46,4 @@ class ConfigFiles(object):
LIBRARY_CONFIG = os.path.join('library', 'webpack.config.js')
MULTIPLE_BUNDLES_CONFIG = os.path.join('multiple_bundles', 'webpack.config.js')
MULTIPLE_ENTRY_CONFIG = os.path.join('multiple_entry', 'webpack.config.js')
CACHED_CONFIG = os.path.join('cached', 'webpack.config.js')
CACHED_CONFIG = os.path.join('cached', 'webpack.config.js')
10 changes: 5 additions & 5 deletions tests/test_bundles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
from webpack.exceptions import ConfigFileNotFound
from webpack.conf import settings
from .settings import ConfigFiles
from .utils import clean_static_root, read_file
from .utils import clean_output_root, read_file


class TestBundles(unittest.TestCase):
@classmethod
def setUpClass(cls):
clean_static_root()
clean_output_root()

@classmethod
def tearDownClass(cls):
clean_static_root()
clean_output_root()

def test_bundle_raises_config_file_not_found_exception_for_nonexistent_config_files(self):
self.assertRaises(ConfigFileNotFound, webpack, '/file/that/does/not/exist.js')
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_bundle_can_expose_its_output_options(self):
self.assertEqual(
output_options['path'],
os.path.join(
settings.STATIC_ROOT,
settings.OUTPUT_ROOT,
settings.OUTPUT_DIR,
bundle.options['__python_webpack_hash__']
)
Expand All @@ -189,4 +189,4 @@ def test_default_context_can_be_extended(self):
self.assertIn('default_context', bundle.data['buildOptions']['context'])
self.assertEqual(bundle.data['buildOptions']['context']['default_context'], 'test')
self.assertIn('foo', bundle.data['buildOptions']['context'])
self.assertEqual(bundle.data['buildOptions']['context']['foo'], 'bar')
self.assertEqual(bundle.data['buildOptions']['context']['foo'], 'bar')
8 changes: 4 additions & 4 deletions tests/test_django_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from webpack.compiler import webpack
from webpack.exceptions import ConfigFileNotFound
from .settings import ConfigFiles
from .utils import clean_static_root
from .utils import clean_output_root


def render_template_tag(path):
Expand All @@ -25,11 +25,11 @@ class TestDjangoIntegration(unittest.TestCase):

@classmethod
def setUpClass(cls):
clean_static_root()
clean_output_root()

@classmethod
def tearDownClass(cls):
clean_static_root()
clean_output_root()

def test_bundle_urls_can_be_resolved_via_the_static_file_finder_used_by_the_dev_server(self):
bundle = webpack('django_test_app/webpack.config.js')
Expand All @@ -52,4 +52,4 @@ def test_template_tag_raises_on_errors(self):
ConfigFileNotFound,
render_template_tag,
'/non_existent_path',
)
)
18 changes: 9 additions & 9 deletions tests/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
from webpack.conf import Conf
from webpack.manifest import generate_manifest, generate_key, write_manifest, read_manifest, populate_manifest_file
from webpack.compiler import webpack
from .settings import ConfigFiles, STATIC_ROOT, WEBPACK
from .utils import clean_static_root
from .settings import ConfigFiles, OUTPUT_ROOT, WEBPACK
from .utils import clean_output_root


class TestManifest(unittest.TestCase):
@classmethod
def setUpClass(cls):
clean_static_root()
clean_output_root()

@classmethod
def tearDownClass(cls):
clean_static_root()
clean_output_root()

def test_a_manifest_key_is_relative(self):
key = generate_key(ConfigFiles.BASIC_CONFIG)
Expand Down Expand Up @@ -149,7 +149,7 @@ def test_a_manifest_can_be_written_to_and_read_from_disk(self):
),
})

path = os.path.join(STATIC_ROOT, 'foo.json')
path = os.path.join(OUTPUT_ROOT, 'foo.json')

write_manifest(path, manifest)

Expand All @@ -172,7 +172,7 @@ def test_the_manifest_is_used_by_the_compiler(self):
key = generate_key(ConfigFiles.BASIC_CONFIG)
self.assertIn(key, manifest)

path = os.path.join(STATIC_ROOT, 'test_manifest.json')
path = os.path.join(OUTPUT_ROOT, 'test_manifest.json')
write_manifest(path, manifest)

with mock.patch('webpack.compiler.build_server.build', self._raise_if_called):
Expand All @@ -190,7 +190,7 @@ def test_the_manifest_is_used_by_the_compiler(self):
self.assertEqual(bundle.data, manifest[key])

def test_the_manifest_can_be_populated_from_settings(self):
path = os.path.join(STATIC_ROOT, 'test_populate_manifest_file.json')
path = os.path.join(OUTPUT_ROOT, 'test_populate_manifest_file.json')

mock_settings = Conf()
mock_settings.configure(
Expand Down Expand Up @@ -218,7 +218,7 @@ def test_the_manifest_can_be_populated_from_settings(self):
self.assertEqual(manifest, expected)

def test_the_manifest_can_be_populated_from_a_dictionary(self):
path = os.path.join(STATIC_ROOT, 'test_populate_dict_manifest_file.json')
path = os.path.join(OUTPUT_ROOT, 'test_populate_dict_manifest_file.json')

mock_settings = Conf()
mock_settings.configure(
Expand All @@ -243,4 +243,4 @@ def test_the_manifest_can_be_populated_from_a_dictionary(self):
ConfigFiles.BASIC_CONFIG: (),
})

self.assertEqual(manifest, expected)
self.assertEqual(manifest, expected)
8 changes: 4 additions & 4 deletions tests/test_watching.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
from webpack.compiler import webpack
from .settings import BUNDLES
from .utils import write_file, read_file, clean_static_root
from .utils import write_file, read_file, clean_output_root

# The number of seconds that we delay while waiting for
# file changes to be detected
Expand Down Expand Up @@ -36,11 +36,11 @@ class TestWatching(unittest.TestCase):

@classmethod
def setUpClass(cls):
clean_static_root()
clean_output_root()

@classmethod
def tearDownClass(cls):
clean_static_root()
clean_output_root()

def test_source_files_can_be_watched_to_rebuild_a_bundle(self):
self.assertEqual(read_file(PATH_TO_WATCHED_SOURCE_ENTRY), WATCHED_SOURCE_CONTENT)
Expand All @@ -63,4 +63,4 @@ def test_source_files_can_be_watched_to_rebuild_a_bundle(self):
self.assertTrue(len(assets), 1)
contents = read_file(assets[0])
self.assertNotIn('__DJANGO_WEBPACK_WATCH_SOURCE_ONE__', contents)
self.assertIn('__DJANGO_WEBPACK_WATCH_SOURCE_TWO__', contents)
self.assertIn('__DJANGO_WEBPACK_WATCH_SOURCE_TWO__', contents)
6 changes: 3 additions & 3 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def read_file(file_name):
return _file.read()


def clean_static_root():
def clean_output_root():
# Clean out any files generated from previous test runs
if os.path.exists(settings.STATIC_ROOT):
shutil.rmtree(settings.STATIC_ROOT)
if os.path.exists(settings.OUTPUT_ROOT):
shutil.rmtree(settings.OUTPUT_ROOT)
4 changes: 2 additions & 2 deletions webpack/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Conf(conf.Conf):
# Environment configuration
STATIC_ROOT = None
OUTPUT_ROOT = None
STATIC_URL = None
OUTPUT_DIR = 'webpack_assets'
CONFIG_DIRS = None
Expand Down Expand Up @@ -35,7 +35,7 @@ class Conf(conf.Conf):
}

def get_path_to_output_dir(self):
return os.path.join(self.STATIC_ROOT, self.OUTPUT_DIR)
return os.path.join(self.OUTPUT_ROOT, self.OUTPUT_DIR)

def get_public_path(self):
static_url = self.STATIC_URL
Expand Down
7 changes: 2 additions & 5 deletions webpack/django_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ class WebpackFileStorage(FileSystemStorage):
"""
def __init__(self, location=None, base_url=None, *args, **kwargs):
if location is None:
location = settings.STATIC_ROOT
location = settings.OUTPUT_ROOT
if base_url is None:
base_url = settings.STATIC_URL
super(WebpackFileStorage, self).__init__(location, base_url, *args, **kwargs)


class WebpackFinder(BaseStorageFinder):
"""
A staticfiles finder that looks in webpack.conf.settings.STATIC_ROOT for generated bundles.
A staticfiles finder that looks in webpack.conf.settings.OUTPUT_ROOT for generated bundles.

To be used during development with staticfiles' development file server or during deployment.
"""
storage = WebpackFileStorage

def list(self, *args, **kwargs):
return []
8 changes: 4 additions & 4 deletions webpack/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def _setting(overrides, key):


def generate_compiler_options(config_file, extra_context=None, setting_overrides=None):
if not _setting(setting_overrides, 'STATIC_ROOT'):
raise ImproperlyConfigured('webpack.conf.settings.STATIC_ROOT has not been defined.')
if not _setting(setting_overrides, 'OUTPUT_ROOT'):
raise ImproperlyConfigured('webpack.conf.settings.OUTPUT_ROOT has not been defined.')

if not _setting(setting_overrides, 'STATIC_URL'):
raise ImproperlyConfigured('webpack.conf.settings.STATIC_URL has not been defined.')
Expand All @@ -33,7 +33,7 @@ def generate_compiler_options(config_file, extra_context=None, setting_overrides
'context': context,
'outputPath': conf.settings.get_path_to_output_dir(),
'publicPath': conf.settings.get_public_path(),
'staticRoot': _setting(setting_overrides, 'STATIC_ROOT'),
'staticRoot': _setting(setting_overrides, 'OUTPUT_ROOT'),
'staticUrl': _setting(setting_overrides, 'STATIC_URL'),
'aggregateTimeout': _setting(setting_overrides, 'AGGREGATE_TIMEOUT'),
}
Expand All @@ -55,4 +55,4 @@ def generate_compiler_options(config_file, extra_context=None, setting_overrides
# Used in the test suite
options['__python_webpack_hash__'] = options_hash

return options
return options