Skip to content

Commit 86fe96d

Browse files
authored
Merge pull request #491 from jhnns/patch-1
Add rewriteUrls option
2 parents 7ae5c53 + 2aee8a6 commit 86fe96d

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

content/usage/less-options.md

+44-15
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,70 @@ Allows you to add a path to every generated import and url in your css. This doe
2222

2323
For instance, if all the images the css use are in a folder called resources, you can use this option to add this on to the URL's and then have the name of that folder configurable.
2424

25-
#### Relative URLs
25+
#### Rewrite URLs
2626

2727
| | |
2828
|---|---|
29-
| `lessc -ru`<br>`lessc --relative-urls` | `{ relativeUrls: true }` |
29+
| `lessc -ru=off`<br>`lessc --rewrite-urls=off` | `{ rewriteUrls: 'off' }` |
30+
| `lessc -ru=all`<br>`lessc --rewrite-urls=all` | `{ rewriteUrls: 'all' }` |
31+
| `lessc -ru=local`<br>`lessc --rewrite-urls=local` | `{ rewriteUrls: 'local' }` |
3032

31-
By default URLs are kept as-is, so if you import a file in a sub-directory that references an image, exactly the same URL will be output in the css. This option allows you to re-write URL's in imported files so that the URL is always relative to the base imported file. E.g.
33+
By default URLs are kept as-is (`off`), so if you import a file in a sub-directory that references an image, exactly the same URL will be output in the css. This option allows you to rewrite URLs in imported files so that the URL is always relative to the base file that has been passed to Less. E.g.
3234

33-
```less
34-
# main.less
35-
@import "files/backgrounds.less";
36-
# files/backgrounds.less
37-
.icon-1 {
38-
background-image: url('images/lamp-post.png');
35+
```css
36+
/* main.less */
37+
@import "global/fonts.less";
38+
```
39+
40+
```css
41+
/* global/fonts.less */
42+
@font-face {
43+
font-family: 'MyFont';
44+
src: url('myfont/myfont.woff2') format('woff2');
3945
}
4046
```
4147

42-
this will output the following normally
48+
With nothing set or with `rewriteUrls: 'off'`, compiling `main.less` will output:
4349

4450
```css
45-
.icon-1 {
46-
background-image: url('images/lamp-post.png');
51+
/* main.less */
52+
/* global/fonts.less */
53+
@font-face {
54+
font-family: 'MyFont';
55+
src: url('myfont/myfont.woff2') format('woff2');
4756
}
4857
```
4958

50-
but with `relativeUrls: true`, it will instead output
59+
With `rewriteUrls: 'all'`, it will output:
5160

5261
```css
53-
.icon-1 {
54-
background-image: url('files/images/lamp-post.png');
62+
/* main.less */
63+
/* global/fonts.less */
64+
@font-face {
65+
font-family: 'MyFont';
66+
src: url('./global/myfont/myfont.woff2') format('woff2');
5567
}
5668
```
5769

70+
With `rewriteUrls: 'local'`, it will only rewrite URLs that are explicitly relative (those starting with a `.`):
71+
72+
```css
73+
url('./myfont/myfont.woff2') /* becomes */ url('./global/myfont/myfont.woff2')
74+
url('myfont/myfont.woff2') /* stays */ url('myfont/myfont.woff2')
75+
```
76+
77+
This can be useful in case you're combining Less with [CSS Modules](https://github.com/css-modules/css-modules) which use similar resolving semantics like Node.js.
78+
5879
You may also want to consider using the data-uri function instead of this option, which will embed images into the css.
5980

81+
#### Relative URLs (deprecated)
82+
83+
| | |
84+
|---|---|
85+
| `lessc -ru`<br>`lessc --relative-urls` | `{ relativeUrls: true }` |
86+
87+
Has been replaced by `rewriteUrls: "all"`.
88+
6089
#### Strict Math
6190

6291
| | |

0 commit comments

Comments
 (0)