我的LESS @import路徑應該是什么?

[英]What should my LESS @import path be?


Here's the scenario:

這是場景:

I'm running Django 1.3.1, utilizing staticfiles, and django-compressor (latest stable) to, among other things, compile LESS files.

我正在運行Django 1.3.1,利用靜態文件和django-compressor(最新穩定版)來編譯LESS文件。

I have an "assets" directory that's hooked into staticfiles with STATICFILES_DIRS (for project-wide static resources). In that directory I have a "css" directory and in that a "lib.less" file that contains LESS variables and mixins.

我有一個“assets”目錄,它與STATICFILES_DIRS(用於項目范圍的靜態資源)掛鈎到靜態文件中。在該目錄中,我有一個“css”目錄,其中包含一個包含LESS變量和mixins的“lib.less”文件。

So the physical path is <project_root>/assets/css/lib.less and it's served at /static/css/lib.less.

所以物理路徑是 /assets/css/lib.less,它在/static/css/lib.less中提供。

In one of my apps' static directory, I have another LESS file that needs to import the one above. The physical path for that is <project_root>/myapp/static/myapp/css/file.less and it would be served at /static/myapp/css/file.less.

在我的一個應用程序的靜態目錄中,我有另一個LESS文件,需要導入上面的那個。其物理路徑是 /myapp/static/myapp/css/file.less,它將在/static/myapp/css/file.less中提供。

My first thought was:

我的第一個想法是:

@import "../../css/lib.less"

(i.e. based on the URL, go up to levels from /static/myapp/css to /static/, then traverse down into /static/css/lib.less).

(即基於URL,從/ static / myapp / css升級到/ static /,然后遍歷到/static/css/lib.less)。

However, that doesn't work, and I've tried just about every combination of URLs and physical paths I can think of and all of them give me FilterErrors in the template, resulting from not being able to find the file to import.

但是,這不起作用,我已經嘗試了幾乎所有我能想到的URL和物理路徑的組合,並且所有這些組合都給了我模板中的FilterErrors,因為無法找到要導入的文件。

Anyone have any ideas what the actual import path should be?

任何人都有任何想法,實際的導入路徑應該是什么?

2 个解决方案

#1


11  

After tracking down exactly where the error was coming from in the django-compressor source. It turns out to be directly passed from the shell. Which clued me into removing all the variables and literally just trying to get the lessc compiler to parse the file.

在精確跟蹤django壓縮器源中錯誤的來源之后。事實證明,它直接從shell傳遞。這讓我想要刪除所有變量,並且只是試圖讓lessc編譯器解析文件。

Turns out it wants a relative path from the source file to the file to be imported in terms of the physical filesystem path. So I had to back all the way out to my <project_root> and then reference assets/css/lib.less from there. The actual import that finally worked was:

事實證明,它希望從物理文件系統路徑導入從源文件到文件的相對路徑。所以我不得不一直回到我的 ,然后從那里引用assets / css / lib.less。最終工作的實際導入是:

@import "../../../../assets/css/lib.less"

What's very odd though is that lessc would not accept an absolute filesystem path (i.e. /path/to/project/assets/css/lib.less). I'm not sure why.

但奇怪的是,lessc不接受絕對文件系統路徑(即/path/to/project/assets/css/lib.less)。我不知道為什么。

UPDATE (02/08/2012)

更新(02/08/2012)

Had a complete "DUH" moment when I finally pushed my code to my staging environment and ran collectstatic. The @import path I was using worked fine in development because that was the physical path to the file then, but once collectstatic has done it's thing, everything is moved around and relative to <project_root>/static/.

當我最終將我的代碼推送到我的臨時環境並運行collectstatic時,有一個完整的“DUH”時刻。我正在使用的@import路徑在開發中運行良好,因為那是文件的物理路徑,但是一旦collectstatic完成了它的事情,一切都被移動並相對於 / static /。

I toyed with the idea of using symbolic links to try to match up the pre and post-collectstatic @import paths, but I decided that that was far too complicated and fragile in the long run.

我玩弄了使用符號鏈接來嘗試匹配收集前和收集后的@import路徑的想法,但我認為從長遠來看,這太復雜和脆弱了。

SO... I broke down and moved all the LESS files together under <project_root>/assets/css/, and rationalized moving the LESS files out of the apps because since they're tied to a project-level file in order to function, they're inherently project-level themselves.

所以......我在 / assets / css /下分解並移動了所有LESS文件,並合理化了將LESS文件移出應用程序,因為它們與項目級文件綁定以便運行,他們本身就是項目級別的。

#2


4  

I'm sort of in the same bind and this is what I've come up with for the most recent versions of compressor and lessc to integrate with staticfiles. Hopefully this will help some other people out

我有點同樣的綁定,這是我為最新版本的壓縮器和lessc與靜態文件集成而提出的。希望這會幫助其他人

As far as I can tell from experimenting, lessc doesn't have a notion of absolute or relative paths. Rather, it seems to maintain a search path, which includes the current directory, the containing directory of the less file, and whatever you pass to it via --include-path

據我所知,在實驗中,lessc沒有絕對或相對路徑的概念。相反,它似乎維護一個搜索路徑,其中包括當前目錄,less文件的包含目錄,以及通過--include-path傳遞給它的任何內容

so in my configuration for compressor I put

所以在我配置的壓縮機我把

COMPRESS_PRECOMPILERS = (
    ('text/less', 'lessc --include-path=%s {infile} {outfile}' % STATIC_ROOT),
)

Say, after running collectstatic I have bootstrap living at

比如說,在跑完collectstatic之后,我有生活在的引導

STATIC_ROOT/bootstrap/3.2.0/bootstrap.css. 

Then from any less file, I can now write

然后從任何較少的文件,我現在可以寫

@import (less, reference) "/bootstrap/3.2.0/bootstrap.css"

which allows me to use the bootstrap classes as less mixins in any of my less files!

這允許我在任何較少的文件中使用bootstrap類作為less mixins!

Every time I update a less file, I have to run collectstatic to aggregate them in a local directory so that compressor can give less the right source files to work on. Otherwise, compressor handles everything smoothly. You can also use collectstatic -l to symlink, which means you only need to collect the files when you add a new one.

每次我更新一個less文件時,我都必須運行collectstatic將它們聚合在一個本地目錄中,以便壓縮器可以減少正確的源文件。否則,壓縮機可以順利處理一切您還可以將collectstatic -l用於符號鏈接,這意味着您只需在添加新文件時收集文件。

I'm considering implementing a management command to smooth out the development process that either subclasses runserver to call collectstatic each time the server is reloaded, or uses django.utils.autoreload directly to call collectstatic when things are updated.

我正在考慮實現一個管理命令,以平滑每次重新加載服務器時子類runserver調用collectstatic的開發過程,或者在更新內容時直接使用django.utils.autoreload來調用collectstatic。

Edit (2014/12/01): My approach as outlined above requires a local static root. I was using remote storage with offline compression in my production environment, so deployment requires a couple extra steps. In addition to calling collectstatic to sync the static files to the remote storage, I call collectstatic with different django config file that uses local storage. After I have collected the files locally, I can call 'compress', having configured it to upload the result files to remote storage, but look in local storage for source files.

編輯(2014/12/01):我上面概述的方法需要本地靜態根。我在生產環境中使用具有脫機壓縮的遠程存儲,因此部署需要幾個額外的步驟。除了調用collectstatic將靜態文件同步到遠程存儲器之外,我還使用不同的django配置文件調用collectstatic,該文件使用本地存儲。在我本地收集文件后,我可以調用'compress',將其配置為將結果文件上傳到遠程存儲,但在本地存儲中查找源文件。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/01/24/720ef4d73dfca4292017e54b4eecc606.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com