lasso-js / lasso-less

Lasso.js plugin to support compilation of less dependencies
7 stars 10 forks source link

This module needs an overhaul #2

Closed patrick-steele-idem closed 9 years ago

patrick-steele-idem commented 9 years ago

Currently, each "less" dependency is being rendered as an individual Less file and this is going against the grain of how Less was designed for a number of reasons:

Solution:

Instead of rendering each Less file individually, simply keep track of all of the encountered Less dependencies and do a single rendering at the end. For example:

optimizer.json:

{
  "dependencies": [
    "variables.less",
    "foo.less",
    "bar.less"
  ]
}

Given the following optimizer.json, the Less renderer should only be called once with the following Less code as input:

@import "/absolute/path/to/variables.less";
@import "/absolute/path/to/foo.less";
@import "/absolute/path/to/bar.less";

That is, every "less" dependency should be thought of as a Less @import statement.

There is one big caveat: all resource urls (e.g. url(foo.png')) inside Less files must be pre-resolved since the optimizer-resolve-css-urls filter would lose the context of the individual Less files on the disks.

In addition, @import statements inside Less files should still work as expected. As a result, we can't simply generate @import statements that point to the original Less file on disk. Instead, we would need to load the Less file from disk and and pre-process it to resolve the URLs. In addition, if that Less file has any @imports then those imports need to be resolved and loaded recursively. In the end, we would then pass a single Less string to the Less renderer that will be similar to the following:

@medium-color: red;
@light-color: lighten(@medium-color, 35%);

.foo {
  background-color: @light-color;
  border: 1px solid: @medium-color;
}

.bar {
  background-image: url(/static/foo-0123abc.png);
}