Skip to content

Commit 936d8df

Browse files
authored
Merge pull request #176 from webpack-contrib/docs/update-readme
docs: update readme
2 parents 3cdc410 + f44f1fb commit 936d8df

File tree

1 file changed

+64
-6
lines changed

1 file changed

+64
-6
lines changed

README.md

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ module.exports = {
7878
};
7979
```
8080

81+
Unfortunately, Less doesn't map all options 1-by-1 to camelCase. When in doubt, [check their executable and search for the dash-case option](https://github.com/less/less.js/blob/3.x/bin/lessc).
82+
8183
### In production
8284

8385
Usually, it's recommended to extract the style sheets into a dedicated file in production using the [ExtractTextPlugin](https://github.com/webpack-contrib/extract-text-webpack-plugin). This way your styles are not dependent on JavaScript:
@@ -116,19 +118,70 @@ module.exports = {
116118

117119
### Imports
118120

119-
webpack provides an [advanced mechanism to resolve files](https://webpack.js.org/configuration/resolve/). The less-loader applies a Less plugin that passes all queries to the webpack resolving engine. Thus you can import your less-modules from `node_modules`. Just prepend them with a `~` which tells webpack to look-up the [`modules`](https://webpack.js.org/configuration/resolve/#resolve-modules).
121+
Starting with less-loader 4, you can now choose between Less' builtin resolver and webpack's resolver. By default, webpack's resolver is used.
122+
123+
#### webpack resolver
124+
125+
webpack provides an [advanced mechanism to resolve files](https://webpack.js.org/configuration/resolve/). The less-loader applies a Less plugin that passes all queries to the webpack resolver. Thus you can import your Less modules from `node_modules`. Just prepend them with a `~` which tells webpack to look up the [`modules`](https://webpack.js.org/configuration/resolve/#resolve-modules).
120126

121127
```css
122128
@import "~bootstrap/less/bootstrap";
123129
```
124130

125-
It's important to only prepend it with `~`, because `~/` resolves to the home-directory. webpack needs to distinguish between `bootstrap` and `~bootstrap` because css- and less-files have no special syntax for importing relative files. Writing `@import "file"` is the same as `@import "./file";`
131+
It's important to only prepend it with `~`, because `~/` resolves to the home-directory. webpack needs to distinguish between `bootstrap` and `~bootstrap`, because CSS and Less files have no special syntax for importing relative files. Writing `@import "file"` is the same as `@import "./file";`
132+
133+
##### Non-Less imports
134+
135+
Using webpack's resolver, you can import any file type. You just need a loader that exports valid Less code. Often, you will also want to set the `issuer` condition to ensure that this rule is only applied on imports originating from Less files:
136+
137+
```
138+
// webpack.config.js
139+
module.exports = {
140+
...
141+
module: {
142+
rules: [{
143+
test: /\.js$/,
144+
issuer: /\.less$/,
145+
use: [{
146+
loader: "js-to-less-loader"
147+
}]
148+
}]
149+
}
150+
};
151+
```
152+
153+
#### Less resolver
154+
155+
If you specify the `paths` option, the less-loader will not use webpack's resolver. Modules, that can't be resolved in the local folder, will be searched in the given `paths`. This is Less' default behavior. `paths` should be an array with absolute paths:
156+
157+
```js
158+
// webpack.config.js
159+
module.exports = {
160+
...
161+
module: {
162+
rules: [{
163+
test: /\.less$/,
164+
use: [{
165+
loader: "style-loader"
166+
}, {
167+
loader: "css-loader"
168+
}, {
169+
loader: "less-loader", options: {
170+
paths: [
171+
path.resolve(__dirname, "node_modules")
172+
]
173+
}
174+
}]
175+
}]
176+
}
177+
};
178+
```
126179

127-
Also please note that for [CSS modules](https://github.com/css-modules/css-modules), relative file paths do not work as expected. Please see [this issue for the explanation](https://github.com/webpack-contrib/less-loader/issues/109#issuecomment-253797335).
180+
In this case, all webpack features like importing non-Less files or aliasing won't work of course.
128181

129182
### Plugins
130183

131-
In order to use [plugins](http://lesscss.org/usage/#plugins), simply set the `lessPlugins` option like this:
184+
In order to use [plugins](http://lesscss.org/usage/#plugins), simply set the `plugins` option like this:
132185

133186
```js
134187
// webpack.config.js
@@ -138,7 +191,7 @@ module.exports = {
138191
...
139192
{
140193
loader: "less-loader", options: {
141-
lessPlugins: [
194+
plugins: [
142195
new CleanCSSPlugin({ advanced: true })
143196
]
144197
}
@@ -163,7 +216,6 @@ To enable CSS source maps, you'll need to pass the `sourceMap` option to the les
163216
```javascript
164217
module.exports = {
165218
...
166-
devtool: "source-map", // any "source-map"-like devtool is possible
167219
module: {
168220
rules: [{
169221
test: /\.less$/,
@@ -183,8 +235,14 @@ module.exports = {
183235
};
184236
```
185237

238+
Also checkout the [sourceMaps example](/examples/sourceMaps).
239+
186240
If you want to edit the original Less files inside Chrome, [there's a good blog post](https://medium.com/@toolmantim/getting-started-with-css-sourcemaps-and-in-browser-sass-editing-b4daab987fb0). The blog post is about Sass but it also works for Less.
187241

242+
### CSS modules gotcha
243+
244+
There is a known problem with Less and [CSS modules](https://github.com/css-modules/css-modules) regarding relative file paths in `url(...)` statements. [See this issue for an explanation](https://github.com/webpack-contrib/less-loader/issues/109#issuecomment-253797335).
245+
188246
<h2 align="center">Maintainer</h2>
189247

190248
<table>

0 commit comments

Comments
 (0)