我的LESS @import路径应该是什么? [英] What should my LESS @import path be?

查看:681
本文介绍了我的LESS @import路径应该是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是这种情况:



我正在运行Django 1.3.1,利用staticfiles和django-compression(最新的稳定),除其他外,编译LESS文件。



我有一个资产目录被挂接到静态文件中, STATICFILES_DIRS (用于项目范围的静态资源)。在该目录中,我有一个css目录,并且包含一个包含LESS变量和mixins的lib.less文件。



所以物理路径是< project_root> /assets/css/lib.less ,并以 /static/css/lib.less p>

在我的一个应用程序的静态目录中,我有另一个LESS文件需要导入上面的一个。物理路径为< project_root> /myapp/static/myapp/css/file.less ,它将在 / static / myapp / css / file.less



我的第一个想法是:

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

(即基于URL,从 / static / myapp / css / static / ,然后遍历到 /static/css/lib.less )。



但是,工作,我已经尝试了每一个URL和物理路径的组合,我可以想到,所有这些都给我的模板中的$ code> FilterError ,由于不能任何人都有任何想法,实际的导入路径应该是什么?

解决方案

在django-compression源中准确地跟踪错误来自哪里。事实证明是直接从shell传递。这让我删除了所有的变量,而且从字面上只是想得到 lessc 编译器来解析文件。



原来,它需要从源文件到文件的相对路径,以物理文件系统路径导入。所以我不得不把所有的东西都回到我的< project_root> 然后引用 assets / css / lib.less 从那里最终工作的实际进口是:

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

很奇怪的是,lessc会接受绝对文件系统路径(即 /path/to/project/assets/css/lib.less )。我不知道为什么。



更新(02/08/2012)



有一个完整的DUH时刻,当我终于把我的代码推到我的分期环境,并运行 collectstatic 。我正在使用的@import路径在开发中工作正常,因为这是文件然后的物理路径,但是一旦 collectstatic 已经做到了这一点,一切都移动到相对于< project_root> / static /



我玩弄了使用的想法 c $ c> @import路径的符号链接,但我决定从长远来看,这是非常复杂和脆弱的。



SO ...我将所有LESS文件分解并移动到< project_root> / assets / css / 下,合理化将LESS文件移出应用程序,因为它们与项目级文件绑定以便运行,它们本身就是项目级本身。


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.

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.

So the physical path is <project_root>/assets/css/lib.less and it's served at /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.

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).

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.

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

解决方案

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.

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:

@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.

UPDATE (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/.

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.

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.

这篇关于我的LESS @import路径应该是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆