Aller au contenu

Processors

GENERATED_ROOT = Path(__file__).parent.resolve() / 'generated' module-attribute

Webpack

compile() staticmethod

Bundle js files with webpack for production.

Source code in staticfiles/processors.py
@staticmethod
def compile():
    """Bundle js files with webpack for production."""
    process = subprocess.Popen(["npm", "run", "compile"])
    process.wait()
    if process.returncode:
        raise RuntimeError(f"Webpack failed with returncode {process.returncode}")

runserver() staticmethod

Bundle js files automatically in background when called in debug mode.

Source code in staticfiles/processors.py
@staticmethod
def runserver() -> subprocess.Popen:
    """Bundle js files automatically in background when called in debug mode."""
    logging.getLogger("django").info("Running webpack server")
    return subprocess.Popen(["npm", "run", "serve"])

Scss

compile(files) staticmethod

Compile scss files to css files.

Source code in staticfiles/processors.py
@staticmethod
def compile(files: CompileArg | Iterable[CompileArg]):
    """Compile scss files to css files."""
    # Generate files inside the generated folder
    # .css files respects the hierarchy in the static folder it was found
    # This converts arg.absolute -> generated/{arg.relative}.scss
    # Example:
    #   app/static/foo.scss          -> generated/foo.css
    #   app/static/bar/foo.scss      -> generated/bar/foo.css
    #   custom/location/bar/foo.scss -> generated/bar/foo.css
    if isinstance(files, Scss.CompileArg):
        files = [files]

    base_args = {"output_style": "compressed", "precision": settings.SASS_PRECISION}

    compiled_files = {
        file.relative.with_suffix(".css"): sass.compile(
            filename=str(file.absolute), **base_args
        )
        for file in files
    }
    for file, content in compiled_files.items():
        dest = GENERATED_ROOT / file
        dest.parent.mkdir(exist_ok=True, parents=True)
        dest.write_text(content)

JS