| 1 |
Metadata-Version: 1.1 |
| 2 |
Name: django-webpack-loader |
| 3 |
Version: 0.6.0 |
| 4 |
Summary: Transparently use webpack with django |
| 5 |
Home-page: https://github.com/owais/django-webpack-loader |
| 6 |
Author: Owais Lone |
| 7 |
Author-email: [email protected] |
| 8 |
License: UNKNOWN |
| 9 |
Download-URL: https://github.com/owais/django-webpack-loader/tarball/0.6.0 |
| 10 |
Description: django-webpack-loader |
| 11 |
===================== |
| 12 |
|
| 13 |
|Join the chat at https://gitter.im/owais/django-webpack-loader| |Build |
| 14 |
Status| |Coverage Status| |
| 15 |
|
| 16 |
Read http://owaislone.org/blog/webpack-plus-reactjs-and-django/ for a |
| 17 |
detailed step by step guide on setting up webpack with django using this |
| 18 |
library. |
| 19 |
|
| 20 |
Use webpack to generate your static bundles without django's staticfiles |
| 21 |
or opaque wrappers. |
| 22 |
|
| 23 |
Django webpack loader consumes the output generated by |
| 24 |
`webpack-bundle-tracker <https://github.com/owais/webpack-bundle-tracker>`__ |
| 25 |
and lets you use the generated bundles in django. |
| 26 |
|
| 27 |
A `changelog <CHANGELOG.md>`__ is also available. |
| 28 |
|
| 29 |
Maintainers |
| 30 |
----------- |
| 31 |
|
| 32 |
In order to overcome the lack of support for Markdown on PyPi, building |
| 33 |
this package can use `pandoc <http://pandoc.org/installing.html>`__ |
| 34 |
along with `pypandoc <https://pypi.python.org/pypi/pypandoc>`__ to |
| 35 |
convert the README.md into a Restructured Text format compatible with |
| 36 |
PyPI. This requires installing ``pandoc`` for your operating system |
| 37 |
(installation instructions on the pandoc site), and ``pypandoc`` which |
| 38 |
will be installed if you: |
| 39 |
|
| 40 |
:: |
| 41 |
|
| 42 |
pip install -r requirements-dev.txt |
| 43 |
|
| 44 |
before uploading to PyPI. |
| 45 |
|
| 46 |
If pandoc or pypandoc fails, the README.md file will be uploaded as it |
| 47 |
was before this enhancement. |
| 48 |
|
| 49 |
Compatibility |
| 50 |
------------- |
| 51 |
|
| 52 |
Test cases cover Django>=1.6 on Python 2.7 and Python>=3.3. 100% code |
| 53 |
coverage is the target so we can be sure everything works anytime. It |
| 54 |
should probably work on older version of django as well but the package |
| 55 |
does not ship any test cases for them. |
| 56 |
|
| 57 |
Install |
| 58 |
------- |
| 59 |
|
| 60 |
.. code:: bash |
| 61 |
|
| 62 |
npm install --save-dev webpack-bundle-tracker |
| 63 |
|
| 64 |
pip install django-webpack-loader |
| 65 |
|
| 66 |
Configuration |
| 67 |
------------- |
| 68 |
|
| 69 |
Assumptions |
| 70 |
~~~~~~~~~~~ |
| 71 |
|
| 72 |
Assuming ``BASE_DIR`` in settings refers to the root of your django app. |
| 73 |
|
| 74 |
.. code:: python |
| 75 |
|
| 76 |
import sys |
| 77 |
import os |
| 78 |
|
| 79 |
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 80 |
|
| 81 |
Assuming ``assets/`` is in ``settings.STATICFILES_DIRS`` like |
| 82 |
|
| 83 |
.. code:: python |
| 84 |
|
| 85 |
STATICFILES_DIRS = ( |
| 86 |
os.path.join(BASE_DIR, 'assets'), |
| 87 |
) |
| 88 |
|
| 89 |
Assuming your webpack config lives at ``./webpack.config.js`` and looks |
| 90 |
like this |
| 91 |
|
| 92 |
.. code:: javascript |
| 93 |
|
| 94 |
var path = require('path'); |
| 95 |
var webpack = require('webpack'); |
| 96 |
var BundleTracker = require('webpack-bundle-tracker'); |
| 97 |
|
| 98 |
module.exports = { |
| 99 |
context: __dirname, |
| 100 |
entry: './assets/js/index', |
| 101 |
output: { |
| 102 |
path: path.resolve('./assets/webpack_bundles/'), |
| 103 |
filename: "[name]-[hash].js" |
| 104 |
}, |
| 105 |
|
| 106 |
plugins: [ |
| 107 |
new BundleTracker({filename: './webpack-stats.json'}) |
| 108 |
] |
| 109 |
} |
| 110 |
|
| 111 |
Default Configuration |
| 112 |
~~~~~~~~~~~~~~~~~~~~~ |
| 113 |
|
| 114 |
.. code:: python |
| 115 |
|
| 116 |
WEBPACK_LOADER = { |
| 117 |
'DEFAULT': { |
| 118 |
'CACHE': not DEBUG, |
| 119 |
'BUNDLE_DIR_NAME': 'webpack_bundles/', # must end with slash |
| 120 |
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'), |
| 121 |
'POLL_INTERVAL': 0.1, |
| 122 |
'TIMEOUT': None, |
| 123 |
'IGNORE': ['.+\.hot-update.js', '.+\.map'] |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
CACHE |
| 128 |
^^^^^ |
| 129 |
|
| 130 |
.. code:: python |
| 131 |
|
| 132 |
WEBPACK_LOADER = { |
| 133 |
'DEFAULT': { |
| 134 |
'CACHE': not DEBUG |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
When ``CACHE`` is set to True, webpack-loader will read the stats file |
| 139 |
only once and cache the result. This means web workers need to be |
| 140 |
restarted in order to pick up any changes made to the stats files. |
| 141 |
|
| 142 |
BUNDLE\_DIR\_NAME |
| 143 |
^^^^^^^^^^^^^^^^^ |
| 144 |
|
| 145 |
.. code:: python |
| 146 |
|
| 147 |
WEBPACK_LOADER = { |
| 148 |
'DEFAULT': { |
| 149 |
'BUNDLE_DIR_NAME': 'bundles/' # end with slash |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
``BUNDLE_DIR_NAME`` refers to the dir in which webpack outputs the |
| 154 |
bundles. It should not be the full path. If ``./assets`` is one of you |
| 155 |
static dirs and webpack generates the bundles in |
| 156 |
``./assets/output/bundles/``, then ``BUNDLE_DIR_NAME`` should be |
| 157 |
``output/bundles/``. |
| 158 |
|
| 159 |
If the bundle generates a file called ``main-cf4b5fab6e00a404e0c7.js`` |
| 160 |
and your STATIC\_URL is ``/static/``, then the ``<script>`` tag will |
| 161 |
look like this |
| 162 |
|
| 163 |
.. code:: html |
| 164 |
|
| 165 |
<script type="text/javascript" src="/static/output/bundles/main-cf4b5fab6e00a404e0c7.js"/> |
| 166 |
|
| 167 |
STATS\_FILE |
| 168 |
^^^^^^^^^^^ |
| 169 |
|
| 170 |
.. code:: python |
| 171 |
|
| 172 |
WEBPACK_LOADER = { |
| 173 |
'DEFAULT': { |
| 174 |
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json') |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
``STATS_FILE`` is the filesystem path to the file generated by |
| 179 |
``webpack-bundle-tracker`` plugin. If you initialize |
| 180 |
``webpack-bundle-tracker`` plugin like this |
| 181 |
|
| 182 |
.. code:: javascript |
| 183 |
|
| 184 |
new BundleTracker({filename: './webpack-stats.json'}) |
| 185 |
|
| 186 |
and your webpack config is located at ``/home/src/webpack.config.js``, |
| 187 |
then the value of ``STATS_FILE`` should be |
| 188 |
``/home/src/webpack-stats.json`` |
| 189 |
|
| 190 |
IGNORE |
| 191 |
^^^^^^ |
| 192 |
|
| 193 |
``IGNORE`` is a list of regular expressions. If a file generated by |
| 194 |
webpack matches one of the expressions, the file will not be included in |
| 195 |
the template. |
| 196 |
|
| 197 |
POLL\_INTERVAL |
| 198 |
^^^^^^^^^^^^^^ |
| 199 |
|
| 200 |
``POLL_INTERVAL`` is the number of seconds webpack\_loader should wait |
| 201 |
between polling the stats file. The stats file is polled every 100 |
| 202 |
miliseconds by default and any requests to are blocked while webpack |
| 203 |
compiles the bundles. You can reduce this if your bundles take shorter |
| 204 |
to compile. |
| 205 |
|
| 206 |
**NOTE:** Stats file is not polled when in production (DEBUG=False). |
| 207 |
|
| 208 |
TIMEOUT |
| 209 |
^^^^^^^ |
| 210 |
|
| 211 |
``TIMEOUT`` is the number of seconds webpack\_loader should wait for |
| 212 |
webpack to finish compiling before raising an exception. ``0``, ``None`` |
| 213 |
or leaving the value out of settings disables timeouts. |
| 214 |
|
| 215 |
Usage |
| 216 |
----- |
| 217 |
|
| 218 |
Manually run webpack to build assets. |
| 219 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 220 |
|
| 221 |
One of the core principles of django-webpack-loader is to not manage |
| 222 |
webpack itself in order to give you the flexibility to run webpack the |
| 223 |
way you want. If you are new to webpack, check one of the |
| 224 |
`examples <https://github.com/owais/django-webpack-loader/tree/master/examples>`__, |
| 225 |
read `my detailed blog |
| 226 |
post <http://owaislone.org/blog/webpack-plus-reactjs-and-django/>`__ or |
| 227 |
check `webpack docs <http://webpack.github.io/>`__. |
| 228 |
|
| 229 |
Settings |
| 230 |
~~~~~~~~ |
| 231 |
|
| 232 |
Add ``webpack_loader`` to ``INSTALLED_APPS`` |
| 233 |
|
| 234 |
:: |
| 235 |
|
| 236 |
INSTALLED_APPS = ( |
| 237 |
... |
| 238 |
'webpack_loader', |
| 239 |
) |
| 240 |
|
| 241 |
Templates |
| 242 |
~~~~~~~~~ |
| 243 |
|
| 244 |
.. code:: html+django |
| 245 |
|
| 246 |
{% load render_bundle from webpack_loader %} |
| 247 |
|
| 248 |
{% render_bundle 'main' %} |
| 249 |
|
| 250 |
``render_bundle`` will render the proper ``<script>`` and ``<link>`` |
| 251 |
tags needed in your template. |
| 252 |
|
| 253 |
``render_bundle`` also takes a second argument which can be a file |
| 254 |
extension to match. This is useful when you want to render different |
| 255 |
types for files in separately. For example, to render CSS in head and JS |
| 256 |
at bottom we can do something like this, |
| 257 |
|
| 258 |
.. code:: html+django |
| 259 |
|
| 260 |
{% load render_bundle from webpack_loader %} |
| 261 |
|
| 262 |
<html> |
| 263 |
<head> |
| 264 |
{% render_bundle 'main' 'css' %} |
| 265 |
</head> |
| 266 |
<body> |
| 267 |
.... |
| 268 |
{% render_bundle 'main' 'js' %} |
| 269 |
</body> |
| 270 |
</head> |
| 271 |
|
| 272 |
Multiple webpack projects |
| 273 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 274 |
|
| 275 |
Version 2.0 and up of webpack loader also supports multiple webpack |
| 276 |
configurations. The following configuration defines 2 webpack stats |
| 277 |
files in settings and uses the ``config`` argument in the template tags |
| 278 |
to influence which stats file to load the bundles from. |
| 279 |
|
| 280 |
.. code:: python |
| 281 |
|
| 282 |
WEBPACK_LOADER = { |
| 283 |
'DEFAULT': { |
| 284 |
'BUNDLE_DIR_NAME': 'bundles/', |
| 285 |
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'), |
| 286 |
}, |
| 287 |
'DASHBOARD': { |
| 288 |
'BUNDLE_DIR_NAME': 'dashboard_bundles/', |
| 289 |
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats-dashboard.json'), |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
.. code:: html+django |
| 294 |
|
| 295 |
{% load render_bundle from webpack_loader %} |
| 296 |
|
| 297 |
<html> |
| 298 |
<body> |
| 299 |
.... |
| 300 |
{% render_bundle 'main' 'js' 'DEFAULT' %} |
| 301 |
{% render_bundle 'main' 'js' 'DASHBOARD' %} |
| 302 |
|
| 303 |
<!-- or render all files from a bundle --> |
| 304 |
{% render_bundle 'main' config='DASHBOARD' %} |
| 305 |
|
| 306 |
<!-- the following tags do the same thing --> |
| 307 |
{% render_bundle 'main' 'css' 'DASHBOARD' %} |
| 308 |
{% render_bundle 'main' extension='css' config='DASHBOARD' %} |
| 309 |
{% render_bundle 'main' config='DASHBOARD' extension='css' %} |
| 310 |
|
| 311 |
<!-- add some extra attributes to the tag --> |
| 312 |
{% render_bundle 'main' 'js' 'DEFAULT' attrs='async chatset="UTF-8"'%} |
| 313 |
</body> |
| 314 |
</head> |
| 315 |
|
| 316 |
File URLs instead of html tags |
| 317 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 318 |
|
| 319 |
If you need the URL to an asset without the HTML tags, the ``get_files`` |
| 320 |
template tag can be used. A common use case is specifying the URL to a |
| 321 |
custom css file for a Javascript plugin. |
| 322 |
|
| 323 |
``get_files`` works exactly like ``render_bundle`` except it returns a |
| 324 |
list of matching files and lets you assign the list to a custom template |
| 325 |
variable. For example, |
| 326 |
|
| 327 |
.. code:: html+django |
| 328 |
|
| 329 |
{% get_files 'editor' 'css' as editor_css_files %} |
| 330 |
CKEDITOR.config.contentsCss = '{{ editor_css_files.0.publicPath }}'; |
| 331 |
|
| 332 |
<!-- or list down name, path and download url for every file --> |
| 333 |
<ul> |
| 334 |
{% for css_file in editor_css_files %} |
| 335 |
<li>{{ css_file.name }} : {{ css_file.path }} : {{ css_file.publicPath }}</li> |
| 336 |
{% endfor %} |
| 337 |
</ul> |
| 338 |
|
| 339 |
Refer other static assets |
| 340 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 341 |
|
| 342 |
``webpack_static`` template tag provides facilities to load static |
| 343 |
assets managed by webpack in django templates. It is like django's built |
| 344 |
in ``static`` tag but for webpack assets instead. |
| 345 |
|
| 346 |
In the below example, ``logo.png`` can be any static asset shipped with |
| 347 |
any npm or bower package. |
| 348 |
|
| 349 |
.. code:: html+django |
| 350 |
|
| 351 |
{% load webpack_static from webpack_loader %} |
| 352 |
|
| 353 |
<!-- render full public path of logo.png --> |
| 354 |
<img src="{% webpack_static 'logo.png' %}"/> |
| 355 |
|
| 356 |
The public path is based on ``webpack.config.js`` |
| 357 |
`output.publicPath <https://webpack.js.org/configuration/output/#output-publicpath>`__. |
| 358 |
|
| 359 |
From Python code |
| 360 |
~~~~~~~~~~~~~~~~ |
| 361 |
|
| 362 |
If you want to access the webpack asset path information from your |
| 363 |
application code then you can use the function in the |
| 364 |
``webpack_loader.utils`` module. |
| 365 |
|
| 366 |
.. code:: python |
| 367 |
|
| 368 |
>>> utils.get_files('main') |
| 369 |
[{'url': '/static/bundles/main.js', u'path': u'/home/mike/root/projects/django-webpack-loader/tests/assets/bundles/main.js', u'name': u'main.js'}, |
| 370 |
{'url': '/static/bundles/styles.css', u'path': u'/home/mike/root/projects/django-webpack-loader/tests/assets/bundles/styles.css', u'name': u'styles.css'}] |
| 371 |
>>> utils.get_as_tags('main') |
| 372 |
['<script type="text/javascript" src="/static/bundles/main.js" ></script>', |
| 373 |
'<link type="text/css" href="/static/bundles/styles.css" rel="stylesheet" />'] |
| 374 |
|
| 375 |
How to use in Production |
| 376 |
------------------------ |
| 377 |
|
| 378 |
**It is up to you**. There are a few ways to handle this. I like to have |
| 379 |
slightly separate configs for production and local. I tell git to ignore |
| 380 |
my local stats + bundle file but track the ones for production. Before |
| 381 |
pushing out newer version to production, I generate a new bundle using |
| 382 |
production config and commit the new stats file and bundle. I store the |
| 383 |
stats file and bundles in a directory that is added to the |
| 384 |
``STATICFILES_DIR``. This gives me integration with collectstatic for |
| 385 |
free. The generated bundles are automatically collected to the target |
| 386 |
directory and synched to S3. |
| 387 |
|
| 388 |
``./webpack_production.config.js`` |
| 389 |
|
| 390 |
.. code:: javascript |
| 391 |
|
| 392 |
var config = require('./webpack.config.js'); |
| 393 |
var BundleTracker = require('webpack-bundle-tracker'); |
| 394 |
|
| 395 |
config.output.path = require('path').resolve('./assets/dist'); |
| 396 |
|
| 397 |
config.plugins = [ |
| 398 |
new BundleTracker({filename: './webpack-stats-prod.json'}) |
| 399 |
] |
| 400 |
|
| 401 |
// override any other settings here like using Uglify or other things that make sense for production environments. |
| 402 |
|
| 403 |
module.exports = config; |
| 404 |
|
| 405 |
``settings.py`` |
| 406 |
|
| 407 |
.. code:: python |
| 408 |
|
| 409 |
if not DEBUG: |
| 410 |
WEBPACK_LOADER.update({ |
| 411 |
'BUNDLE_DIR_NAME': 'dist/', |
| 412 |
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats-prod.json') |
| 413 |
}) |
| 414 |
|
| 415 |
You can also simply generate the bundles on the server before running |
| 416 |
collectstatic if that works for you. |
| 417 |
|
| 418 |
Extra |
| 419 |
----- |
| 420 |
|
| 421 |
Jinja2 Configuration |
| 422 |
~~~~~~~~~~~~~~~~~~~~ |
| 423 |
|
| 424 |
If you need to output your assets in a jinja template we provide a |
| 425 |
Jinja2 extension that's compatible with the `Django |
| 426 |
Jinja <https://github.com/niwinz/django-jinja>`__ module and Django 1.8. |
| 427 |
|
| 428 |
To install the extension add it to the django\_jinja ``TEMPLATES`` |
| 429 |
configuration in the ``["OPTIONS"]["extension"]`` list. |
| 430 |
|
| 431 |
.. code:: python |
| 432 |
|
| 433 |
TEMPLATES = [ |
| 434 |
{ |
| 435 |
"BACKEND": "django_jinja.backend.Jinja2", |
| 436 |
"OPTIONS": { |
| 437 |
"extensions": [ |
| 438 |
"django_jinja.builtins.extensions.DjangoFiltersExtension", |
| 439 |
"webpack_loader.contrib.jinja2ext.WebpackExtension", |
| 440 |
], |
| 441 |
} |
| 442 |
} |
| 443 |
] |
| 444 |
|
| 445 |
Then in your base jinja template: |
| 446 |
|
| 447 |
.. code:: html |
| 448 |
|
| 449 |
{{ render_bundle('main') }} |
| 450 |
|
| 451 |
-------------- |
| 452 |
|
| 453 |
Enjoy your webpack with django :) |
| 454 |
|
| 455 |
.. |Join the chat at https://gitter.im/owais/django-webpack-loader| image:: https://badges.gitter.im/Join%20Chat.svg |
| 456 |
:target: https://gitter.im/owais/django-webpack-loader?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge |
| 457 |
.. |Build Status| image:: https://travis-ci.org/owais/django-webpack-loader.svg?branch=master |
| 458 |
:target: https://travis-ci.org/owais/django-webpack-loader |
| 459 |
.. |Coverage Status| image:: https://coveralls.io/repos/owais/django-webpack-loader/badge.svg?branch=master&service=github |
| 460 |
:target: https://coveralls.io/github/owais/django-webpack-loader?branch=master |
| 461 |
|
| 462 |
Keywords: django,webpack,assets |
| 463 |
Platform: UNKNOWN |
| 464 |
Classifier: Programming Language :: Python :: 2.6 |
| 465 |
Classifier: Programming Language :: Python :: 2.7 |
| 466 |
Classifier: Programming Language :: Python :: 3.3 |
| 467 |
Classifier: Programming Language :: Python :: 3.4 |
| 468 |
Classifier: Programming Language :: Python :: 3.5 |
| 469 |
Classifier: Framework :: Django |
| 470 |
Classifier: Environment :: Web Environment |
| 471 |
Classifier: License :: OSI Approved :: MIT License |