Skip to content

[Question] Can you suggest a way to @import less code form a different app #749

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
camilotorresf opened this issue Aug 17, 2021 · 2 comments

Comments

@camilotorresf
Copy link

Hi,

I'm migrating a project from Django 1.11 to 3.2, and also migrating django-pipeline from 1.6.9 to 2.0.6.
Now, we have many django apps and in one app I'm importing less (CSS compiler) code from another app:

in admin_main/static/admin_main/less/common.less, which is located inside the admin_main app I have

@import "../../common/third_party/bootstrap/less/variables";
@import "../../common/less/styles/flexbox.less";
@import "../../common/less/lib/spacing.less";
@import "../../common/less/lib/ng-animate.less"

All these 4 less files are located in the common app:
common/static/common/third_party/bootstrap/less/variables.less
common/static/common/less/styles/flexbox.less
common/static/common/less/lib/spacing.less
common/static/common/less/lib/ng-animate.less

The problem is when collecting statics and compiling admin_main/static/admin_main/less/common.less, lessc cannot find the imported files, because these files are in a complete different directory, and the relative paths cannot be found.

With django-pipeline 1.6.9 these files were found, because the file to process was not the project file, but the collected file inside my collectedstatic directory: deploy/admin/collectedstatic/admin_main/less/common.less, and of course the 4 imported files are also inside the collectedstatic directory, and all works, because lessc can find them in the same collectedstatic directory.

I have found that the problem was introduced by this commit:
63d1182
When I revert the commit it works as before. For now, I'm going to fork and use a customized version of django-pipeline.

I kindly ask: is it possible, and how do you suggest to solve these relative imports from a different Django application with the official django-pipeline package?

Is it OK to create a Pull Request to modify that commit and only use the project_infile when PIPELINE_COLLECTOR_ENABLED == False ?

Thank you for the time you can spend on this.

camilotorresf added a commit to camilotorresf/django-pipeline that referenced this issue Aug 17, 2021
If the collector is enabled (PIPELINE_COLLECTOR_ENABLED is True), use
the collected file. If the disabled (False), use the original project
source file to check if it is outdated and pick up the changes.

This will allow for static files (in particular less CSS compiler) files
to do relative imports from different apps.

See: jazzband#749
@camilotorresf
Copy link
Author

I created this PR:
#750

struan added a commit to mysociety/caps that referenced this issue Oct 19, 2021
Importing other apps styles is handled differently in live and dev
environments as the dev environment processes the non collected file and
live processes the collected file.

There is an existing ticket for this: jazzband/django-pipeline#749
which has a PR. To fix this we've forked the fixed version and are using
that directly from github rather than pulling in an official package.
@ex5
Copy link

ex5 commented Mar 26, 2025

Also running into this problem with SASS @imports referencing files from outside the app. Our setup is very similar to described above: imported files are located in a different directory listed under STATICFILES_DIRS, and referenced by files inside an app.

It seems to be possible to write the imports in a way that they
(a) either work during collectstatic and then break in dev server with CompileError ... Error: File to import not found or unreadable, or
(b) work in dev server and fail with the same error during collectstatic.

In (a) imports must be relative to the STATIC_DIR and in (b) they must be relative to the app's directory with the source file using the @import. Having both work appears to be impossible, unless relative paths accidentally have the same nesting relative to both STATIC_DIR and to the app's static.

It's really a showstopper for us, because we have styles reused in different Django projects.

It looks like there are a couple of forgotten PRs that attempt to fix the problem:

#750
#150

as well as an abandoned discussion in #487

@peymanslh any suggestions on how to make this work?
(not sure whom to tag, so tagging whomever shows up in recent merges)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants