less.ruby icon indicating copy to clipboard operation
less.ruby copied to clipboard

Reimporting files clobber redefinitions

Open bruce opened this issue 16 years ago • 1 comments

The following example was tested using 1.2.8 (GitHub-hosted gem). It shows how importing a file can cause its definitions to clobber re-definitions made since the first import.

Note: I prepended #s with a backslashes in the example to prevent GitHub's autolinking of issue #'s. Apparently in code blocks the backslash stays behind; it goes without saying these backslashes aren't present in my actual less/css markup.

File main.less (importing some-framework.css to use .red):

@import "some-framework.css";

body { background: \#fff; }
p { .red; }

@import "_secondary";

File _secondary.less (also importing some-framework.css to use .red):

@import "some-framework.css";

td { .red; }

File some-framework.css

body { background: \#000; }

.red {
  color: red;
}

This following is the output (main.css) of running lessc main.less. You can see here the second import of some-framework.css in _secondary.less has caused the body background set in main.less to be clobbered. This is effectively a re-import.

body { background: \#000000; }
.red { color: red; }
body { background: \#ffffff; }
p { color: red; }
body { background: \#000000; }
.red, td { color: red; }

The real world example of this problem is using a framework like blueprint, and trying to modify the background -- only to have it clobbered when you import other files (that also need blueprint to use .column, etc).

bruce avatar Sep 28 '09 21:09 bruce

Something I did as a quick hack when I ran into this problem was to modify less.tt temporarily to output the list of files imported (at import), like this:

unless env.root.imported.include?(path)
  env.root.imported << path
  p env.root.imported # I added this -BW
  env.rules += Less::Engine.new(File.new(path)).to_tree.rules
end

Here was the output when I ran the example I noted in this issue:

["/Users/bruce/less-test/some-framework.css"]
["/Users/bruce/less-test/some-framework.css", "/Users/bruce/less-test/_secondary.less"]
["/Users/bruce/less-test/some-framework.css"]

This makes me wonder if env.root.imported survives across files, or if it's being cleared.

Thanks!

bruce avatar Sep 28 '09 21:09 bruce