You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+64-6Lines changed: 64 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -78,6 +78,8 @@ module.exports = {
78
78
};
79
79
```
80
80
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
+
81
83
### In production
82
84
83
85
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 = {
116
118
117
119
### Imports
118
120
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).
120
126
121
127
```css
122
128
@import"~bootstrap/less/bootstrap";
123
129
```
124
130
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
+
```
126
179
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.
128
181
129
182
### Plugins
130
183
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:
132
185
133
186
```js
134
187
// webpack.config.js
@@ -138,7 +191,7 @@ module.exports = {
138
191
...
139
192
{
140
193
loader:"less-loader", options: {
141
-
lessPlugins: [
194
+
plugins: [
142
195
newCleanCSSPlugin({ advanced:true })
143
196
]
144
197
}
@@ -163,7 +216,6 @@ To enable CSS source maps, you'll need to pass the `sourceMap` option to the les
163
216
```javascript
164
217
module.exports= {
165
218
...
166
-
devtool:"source-map", // any "source-map"-like devtool is possible
167
219
module: {
168
220
rules: [{
169
221
test:/\.less$/,
@@ -183,8 +235,14 @@ module.exports = {
183
235
};
184
236
```
185
237
238
+
Also checkout the [sourceMaps example](/examples/sourceMaps).
239
+
186
240
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.
187
241
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).
0 commit comments