django-yarn
django-yarn copied to clipboard
Automatically pull in npm dependencies as staticfiles in your Django project
django-yarn
Want to use yarn/yarn modules in your django project without vendoring them? django-yarn serves as a wrapper around the yarn command-line program as well as a staticfiles finder.
Installation
-
$ pip install django-yarn
- Install yarn, then install yarn (
npm install -g yarn
). If you use a private registry, make sure your.yarnrc
is set up to connect to it - Have a
package.json
at the root of your project, listing your dependencies - Add
yarn.finders.YarnFinder
toSTATICFILES_FINDERS
- Configure your
settings.py
-
$ yarn add
with the command line, or with Python:from yarn.finders import yarn_add; yarn_add()
-
$ ./manage.py collectstatic
will copy all selected node_modules files into yourSTATIC_ROOT
.
Configuration
-
YARN_ROOT_PATH
: absolute path to the yarn "root" directory - this is where yarn will look for yourpackage.json
, put yournode_modules
folder and look for a.yarnrc
file -
YARN_EXECUTABLE_PATH
: (optional) defaults to whereveryarn
is on your PATH. If you specify this, you can override the path to theyarn
executable. This is also an absolute path. -
YARN_STATIC_FILES_PREFIX
: (optional) Your yarn files will end up under this path inside static. I usually use something like 'js/lib' (so your files will be in /static/js/lib/react.js for example) but you can leave it blank and they will just end up in the root. -
YARN_FILE_PATTERNS
: (optional) By default, django-yarn will expose all files innode_modules
to Django as staticfiles. You may not want all of them to be exposed. You can pick specific files by adding some additional configuration:YARN_FILE_PATTERNS = { 'react': ['react.js'], 'express': ['lib/*.js', 'index.js'] }
Keys are the names of the npm/yarn modules, and values are lists containing strings. The strings match against glob patterns.
-
YARN_FINDER_USE_CACHE
: (default True) A boolean that enables cache in the finder. If enabled, the file list will be computed only once, when the server is started.
yarn add
If you want to run yarn add
programmatically, you can do:
from yarn.finders import yarn_add
yarn_add()
Changelog
- V1.0.0 - Initial release based on django-npm v1.0.0.