Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature/static build #1700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 30 additions & 1 deletion django_extensions/management/commands/runserver_plus.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
import logging
import os
import subprocess
import threading
import re
import socket
import sys
Expand All @@ -15,7 +17,8 @@
from django.core.management.color import color_style
from django.core.servers.basehttp import get_internal_wsgi_application
from django.dispatch import Signal
from django.utils.autoreload import get_reloader
from django.utils.autoreload import get_reloader, DJANGO_AUTORELOAD_ENV

from django.views import debug as django_views_debug

try:
Expand Down Expand Up @@ -183,6 +186,13 @@ def add_arguments(self, parser):
"the Werkzeug server.")
parser.add_argument("--nopin", dest="nopin", action="store_true", default=False,
help="Disable the PIN in werkzeug. USE IT WISELY!")
parser.add_argument("--static-build", dest="static_build", action="store_true", default=False,
help="Run a static build command in another thread.",)
parser.add_argument("--static-build-command", dest="static_command", default="npm run dev",
help="This static build command will be run in another thread.",)
parser.add_argument("--static-build-quiet", action="store_true", dest="static_quiet", default=False,
help="Suppress the output of the webpack build command.",)


if USE_STATICFILES:
parser.add_argument('--nostatic', action="store_false", dest='use_static_handler', default=True,
Expand Down Expand Up @@ -293,6 +303,9 @@ def postmortem(request, exc_type, exc_value, tb):
with monkey_patch_cursordebugwrapper(print_sql=options["print_sql"], print_sql_location=options["print_sql_location"], truncate=truncate, logger=logger.info, confprefix="RUNSERVER_PLUS"):
self.inner_run(options)




def get_handler(self, *args, **options):
"""Return the default WSGI handler for the runner."""
return get_internal_wsgi_application()
Expand Down Expand Up @@ -398,6 +411,22 @@ def make_environ(self):
if not use_reloader:
os.environ['WERKZEUG_RUN_MAIN'] = 'true'

if os.environ.get(DJANGO_AUTORELOAD_ENV) != "true" and \
os.environ.get('WERKZEUG_RUN_MAIN') != "true" and \
options['static_build']:
self.stdout.write("Starting static build thread.")
quiet = options["static_quiet"]
command = options["static_command"]
kwargs = {"shell": True}
if quiet:
# if --quiet, suppress command's output:
kwargs.update({"stdin": subprocess.PIPE, "stdout": subprocess.PIPE})
static_thread = threading.Thread(
target=subprocess.run, args=(command,), kwargs=kwargs
)
static_thread.start()


# Don't run a second instance of the debugger / reloader
# See also: https://github.com/django-extensions/django-extensions/issues/832
if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':
Expand Down
12 changes: 12 additions & 0 deletions docs/runserver_plus.rst
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,18 @@ If the PIN is entered too many times incorrectly the server needs to be restarte
exploit the debugger. Never enable the debugger in production.**


Using a static build command
------------

If you want to use a build server such as webpack or vite.js to compile static assets in the background while you develop
You can pass ``--static-build`` to enable the server to run a background subprocess to watch and build your static assets.
You can define the subprocess command to run with the argument ``--static-build-command``, which defaults to ``npm run dev``

Additionally, by default the output of the build command will merge with the servers output, but you can pass the option ``--static-build-quiet``
to silence the output.



.. _gh625: https://github.com/django-extensions/django-extensions/issues/625
.. _Werkzeug: http://werkzeug.pocoo.org/
.. _Watchdog: https://pypi.python.org/pypi/watchdog