Aller au contenu

Storage

JS

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)

ManifestPostProcessingStorage

Bases: ManifestStaticFilesStorage

url(name, *, force=False)

Get the URL for a file, convert .scss calls to .css ones and .ts to .js

Source code in staticfiles/storage.py
def url(self, name: str, *, force: bool = False) -> str:
    """Get the URL for a file, convert .scss calls to .css ones and .ts to .js"""
    # This name swap has to be done here
    # Otherwise, the manifest isn't aware of the file and can't work properly
    path = Path(name)
    if path.suffix == ".scss":
        # Compile scss files automatically in debug mode
        if settings.DEBUG:
            Scss.compile(
                [
                    Scss.CompileArg(absolute=Path(p), relative=Path(name))
                    for p in find(name, all=True)
                ]
            )
        name = str(path.with_suffix(".css"))

    elif path.suffix == ".ts":
        name = str(path.with_suffix(".js"))

    return super().url(name, force=force)