source: trip-planner-front/node_modules/less-loader/README.md@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 16.3 KB
Line 
1<div align="center">
2 <a href="https://github.com/webpack/webpack">
3 <img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
4 </a>
5</div>
6
7[![npm][npm]][npm-url]
8[![node][node]][node-url]
9[![deps][deps]][deps-url]
10[![tests][tests]][tests-url]
11[![cover][cover]][cover-url]
12[![chat][chat]][chat-url]
13[![size][size]][size-url]
14
15# less-loader
16
17A Less loader for webpack. Compiles Less to CSS.
18
19## Getting Started
20
21To begin, you'll need to install `less` and `less-loader`:
22
23```console
24$ npm install less less-loader --save-dev
25```
26
27Then add the loader to your `webpack` config. For example:
28
29**webpack.config.js**
30
31```js
32module.exports = {
33 module: {
34 rules: [
35 {
36 test: /\.less$/i,
37 loader: [
38 // compiles Less to CSS
39 "style-loader",
40 "css-loader",
41 "less-loader",
42 ],
43 },
44 ],
45 },
46};
47```
48
49And run `webpack` via your preferred method.
50
51## Options
52
53| Name | Type | Default | Description |
54| :---------------------------------------: | :------------------: | :----------------------: | :----------------------------------------------------- |
55| **[`lessOptions`](#lessoptions)** | `{Object\|Function}` | `{ relativeUrls: true }` | Options for Less. |
56| **[`additionalData`](#additionalData)** | `{String\|Function}` | `undefined` | Prepends/Appends `Less` code to the actual entry file. |
57| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps. |
58| **[`webpackImporter`](#webpackimporter)** | `{Boolean}` | `true` | Enables/Disables the default Webpack importer. |
59| **[`implementation`](#implementation)** | `{Object\|String}` | `less` | Setup Less implementation to use. |
60
61### `lessOptions`
62
63Type: `Object|Function`
64Default: `{ relativeUrls: true }`
65
66You can pass any Less specific options to the `less-loader` through the `lessOptions` property in the [loader options](https://webpack.js.org/configuration/module/#rule-options-rule-query). See the [Less documentation](http://lesscss.org/usage/#command-line-usage-options) for all available options in dash-case. Since we're passing these options to Less programmatically, you need to pass them in camelCase here:
67
68#### `Object`
69
70Use an object to pass options through to Less.
71
72**webpack.config.js**
73
74```js
75module.exports = {
76 module: {
77 rules: [
78 {
79 test: /\.less$/i,
80 use: [
81 {
82 loader: "style-loader",
83 },
84 {
85 loader: "css-loader",
86 },
87 {
88 loader: "less-loader",
89 options: {
90 lessOptions: {
91 strictMath: true,
92 },
93 },
94 },
95 ],
96 },
97 ],
98 },
99};
100```
101
102#### `Function`
103
104Allows setting the options passed through to Less based off of the loader context.
105
106```js
107module.exports = {
108 module: {
109 rules: [
110 {
111 test: /\.less$/i,
112 use: [
113 "style-loader",
114 "css-loader",
115 {
116 loader: "less-loader",
117 options: {
118 lessOptions: (loaderContext) => {
119 // More information about available properties https://webpack.js.org/api/loaders/
120 const { resourcePath, rootContext } = loaderContext;
121 const relativePath = path.relative(rootContext, resourcePath);
122
123 if (relativePath === "styles/foo.less") {
124 return {
125 paths: ["absolute/path/c", "absolute/path/d"],
126 };
127 }
128
129 return {
130 paths: ["absolute/path/a", "absolute/path/b"],
131 };
132 },
133 },
134 },
135 ],
136 },
137 ],
138 },
139};
140```
141
142### `additionalData`
143
144Type: `String|Function`
145Default: `undefined`
146
147Prepends `Less` code before the actual entry file.
148In this case, the `less-loader` will not override the source but just **prepend** the entry's content.
149
150This is especially useful when some of your Less variables depend on the environment:
151
152> ℹ Since you're injecting code, this will break the source mappings in your entry file. Often there's a simpler solution than this, like multiple Less entry files.
153
154#### `String`
155
156```js
157module.exports = {
158 module: {
159 rules: [
160 {
161 test: /\.less$/i,
162 use: [
163 "style-loader",
164 "css-loader",
165 {
166 loader: "less-loader",
167 options: {
168 additionalData: `@env: ${process.env.NODE_ENV};`,
169 },
170 },
171 ],
172 },
173 ],
174 },
175};
176```
177
178#### `Function`
179
180##### Sync
181
182```js
183module.exports = {
184 module: {
185 rules: [
186 {
187 test: /\.less$/i,
188 use: [
189 "style-loader",
190 "css-loader",
191 {
192 loader: "less-loader",
193 options: {
194 additionalData: (content, loaderContext) => {
195 // More information about available properties https://webpack.js.org/api/loaders/
196 const { resourcePath, rootContext } = loaderContext;
197 const relativePath = path.relative(rootContext, resourcePath);
198
199 if (relativePath === "styles/foo.less") {
200 return "@value: 100px;" + content;
201 }
202
203 return "@value: 200px;" + content;
204 },
205 },
206 },
207 ],
208 },
209 ],
210 },
211};
212```
213
214##### Async
215
216```js
217module.exports = {
218 module: {
219 rules: [
220 {
221 test: /\.less$/i,
222 use: [
223 "style-loader",
224 "css-loader",
225 {
226 loader: "less-loader",
227 options: {
228 additionalData: async (content, loaderContext) => {
229 // More information about available properties https://webpack.js.org/api/loaders/
230 const { resourcePath, rootContext } = loaderContext;
231 const relativePath = path.relative(rootContext, resourcePath);
232
233 if (relativePath === "styles/foo.less") {
234 return "@value: 100px;" + content;
235 }
236
237 return "@value: 200px;" + content;
238 },
239 },
240 },
241 ],
242 },
243 ],
244 },
245};
246```
247
248### `sourceMap`
249
250Type: `Boolean`
251Default: depends on the `compiler.devtool` value
252
253By default generation of source maps depends on the [`devtool`](https://webpack.js.org/configuration/devtool/) option. All values enable source map generation except `eval` and `false` value.
254
255**webpack.config.js**
256
257```js
258module.exports = {
259 module: {
260 rules: [
261 {
262 test: /\.less$/i,
263 use: [
264 "style-loader",
265 {
266 loader: "css-loader",
267 options: {
268 sourceMap: true,
269 },
270 },
271 {
272 loader: "less-loader",
273 options: {
274 sourceMap: true,
275 },
276 },
277 ],
278 },
279 ],
280 },
281};
282```
283
284### `webpackImporter`
285
286Type: `Boolean`
287Default: `true`
288
289Enables/Disables the default `webpack` importer.
290
291This can improve performance in some cases. Use it with caution because aliases and `@import` at-rules starting with `~` will not work.
292
293**webpack.config.js**
294
295```js
296module.exports = {
297 module: {
298 rules: [
299 {
300 test: /\.less$/i,
301 use: [
302 "style-loader",
303 "css-loader",
304 {
305 loader: "less-loader",
306 options: {
307 webpackImporter: false,
308 },
309 },
310 ],
311 },
312 ],
313 },
314};
315```
316
317### `implementation`
318
319Type: `Object | String`
320
321> ⚠ less-loader compatible with Less 3 and 4 versions
322
323The special `implementation` option determines which implementation of Less to use. Overrides the locally installed `peerDependency` version of `less`.
324
325**This option is only really useful for downstream tooling authors to ease the Less 3-to-4 transition.**
326
327#### Object
328
329**webpack.config.js**
330
331```js
332module.exports = {
333 module: {
334 rules: [
335 {
336 test: /\.less$/i,
337 use: [
338 "style-loader",
339 "css-loader",
340 {
341 loader: "less-loader",
342 options: {
343 implementation: require("less"),
344 },
345 },
346 ],
347 },
348 ],
349 },
350};
351```
352
353#### String
354
355**webpack.config.js**
356
357```js
358module.exports = {
359 module: {
360 rules: [
361 {
362 test: /\.less$/i,
363 use: [
364 "style-loader",
365 "css-loader",
366 {
367 loader: "less-loader",
368 options: {
369 implementation: require.resolve("less"),
370 },
371 },
372 ],
373 },
374 ],
375 },
376};
377```
378
379## Examples
380
381### Normal usage
382
383Chain the `less-loader` with the [`css-loader`](https://github.com/webpack-contrib/css-loader) and the [`style-loader`](https://github.com/webpack-contrib/style-loader) to immediately apply all styles to the DOM.
384
385**webpack.config.js**
386
387```js
388module.exports = {
389 module: {
390 rules: [
391 {
392 test: /\.less$/i,
393 use: [
394 {
395 loader: "style-loader", // creates style nodes from JS strings
396 },
397 {
398 loader: "css-loader", // translates CSS into CommonJS
399 },
400 {
401 loader: "less-loader", // compiles Less to CSS
402 },
403 ],
404 },
405 ],
406 },
407};
408```
409
410Unfortunately, Less doesn't map all options 1-by-1 to camelCase. When in doubt, [check their executable](https://github.com/less/less.js/blob/3.x/bin/lessc) and search for the dash-case option.
411
412### Source maps
413
414To enable sourcemaps for CSS, you'll need to pass the `sourceMap` property in the loader's options. If this is not passed, the loader will respect the setting for webpack source maps, set in `devtool`.
415
416**webpack.config.js**
417
418```js
419module.exports = {
420 devtool: "source-map", // any "source-map"-like devtool is possible
421 module: {
422 rules: [
423 {
424 test: /\.less$/i,
425 use: [
426 "style-loader",
427 {
428 loader: "css-loader",
429 options: {
430 sourceMap: true,
431 },
432 },
433 {
434 loader: "less-loader",
435 options: {
436 sourceMap: true,
437 },
438 },
439 ],
440 },
441 ],
442 },
443};
444```
445
446If 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.
447
448### In production
449
450Usually, it's recommended to extract the style sheets into a dedicated file in production using the [MiniCssExtractPlugin](https://github.com/webpack-contrib/mini-css-extract-plugin). This way your styles are not dependent on JavaScript.
451
452### Imports
453
454First we try to use built-in `less` resolve logic, then `webpack` resolve logic (aliases and `~`).
455
456#### Webpack Resolver
457
458`webpack` provides an [advanced mechanism to resolve files](https://webpack.js.org/configuration/resolve/).
459`less-loader` applies a Less plugin that passes all queries to the webpack resolver if `less` could not resolve `@import`.
460Thus you can import your Less modules from `node_modules`.
461
462```css
463@import "bootstrap/less/bootstrap";
464```
465
466Using `~` is deprecated and can be removed from your code (**we recommend it**), but we still support it for historical reasons.
467Why you can removed it? The loader will first try to resolve `@import` as relative, if it cannot be resolved, the loader will try to resolve `@import` inside [`node_modules`](https://webpack.js.org/configuration/resolve/#resolve-modules).
468Just prepend them with a `~` which tells webpack to look up the [`modules`](https://webpack.js.org/configuration/resolve/#resolve-modules).
469
470```css
471@import "~bootstrap/less/bootstrap";
472```
473
474Default resolver options can be modified by [`resolve.byDependency`](https://webpack.js.org/configuration/resolve/#resolvebydependency):
475
476**webpack.config.js**
477
478```js
479module.exports = {
480 devtool: "source-map", // any "source-map"-like devtool is possible
481 module: {
482 rules: [
483 {
484 test: /\.less$/i,
485 use: ["style-loader", "css-loader", "less-loader"],
486 },
487 ],
488 },
489 resolve: {
490 byDependency: {
491 // More options can be found here https://webpack.js.org/configuration/resolve/
492 less: {
493 mainFiles: ["custom"],
494 },
495 },
496 },
497};
498```
499
500It'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";`
501
502#### Less Resolver
503
504If you specify the `paths` option, modules will be searched in the given `paths`. This is `less` default behavior. `paths` should be an array with absolute paths:
505
506**webpack.config.js**
507
508```js
509module.exports = {
510 module: {
511 rules: [
512 {
513 test: /\.less$/i,
514 use: [
515 {
516 loader: "style-loader",
517 },
518 {
519 loader: "css-loader",
520 },
521 {
522 loader: "less-loader",
523 options: {
524 lessOptions: {
525 paths: [path.resolve(__dirname, "node_modules")],
526 },
527 },
528 },
529 ],
530 },
531 ],
532 },
533};
534```
535
536### Plugins
537
538In order to use [plugins](http://lesscss.org/usage/#plugins), simply set the `plugins` option like this:
539
540**webpack.config.js**
541
542```js
543const CleanCSSPlugin = require('less-plugin-clean-css');
544
545module.exports = {
546 ...
547 {
548 loader: 'less-loader',
549 options: {
550 lessOptions: {
551 plugins: [
552 new CleanCSSPlugin({ advanced: true }),
553 ],
554 },
555 },
556 },
557 ...
558};
559```
560
561> ℹ️ Access to the [loader context](https://webpack.js.org/api/loaders/#the-loader-context) inside the custom plugin can be done using the `pluginManager.webpackLoaderContext` property.
562
563```js
564module.exports = {
565 install: function (less, pluginManager, functions) {
566 functions.add("pi", function () {
567 // Loader context is available in `pluginManager.webpackLoaderContext`
568
569 return Math.PI;
570 });
571 },
572};
573```
574
575### Extracting style sheets
576
577Bundling CSS with webpack has some nice advantages like referencing images and fonts with hashed urls or [hot module replacement](https://webpack.js.org/concepts/hot-module-replacement/) in development. In production, on the other hand, it's not a good idea to apply your style sheets depending on JS execution. Rendering may be delayed or even a [FOUC](https://en.wikipedia.org/wiki/Flash_of_unstyled_content) might be visible. Thus it's often still better to have them as separate files in your final production build.
578
579There are two possibilities to extract a style sheet from the bundle:
580
581- [`extract-loader`](https://github.com/peerigon/extract-loader) (simpler, but specialized on the css-loader's output)
582- [MiniCssExtractPlugin](https://github.com/webpack-contrib/mini-css-extract-plugin) (more complex, but works in all use-cases)
583
584### CSS modules gotcha
585
586There 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).
587
588## Contributing
589
590Please take a moment to read our contributing guidelines if you haven't yet done so.
591
592[CONTRIBUTING](./.github/CONTRIBUTING.md)
593
594## License
595
596[MIT](./LICENSE)
597
598[npm]: https://img.shields.io/npm/v/less-loader.svg
599[npm-url]: https://npmjs.com/package/less-loader
600[node]: https://img.shields.io/node/v/less-loader.svg
601[node-url]: https://nodejs.org
602[deps]: https://david-dm.org/webpack-contrib/less-loader.svg
603[deps-url]: https://david-dm.org/webpack-contrib/less-loader
604[tests]: https://github.com/webpack-contrib/less-loader/workflows/less-loader/badge.svg
605[tests-url]: https://github.com/webpack-contrib/less-loader/actions
606[cover]: https://codecov.io/gh/webpack-contrib/less-loader/branch/master/graph/badge.svg
607[cover-url]: https://codecov.io/gh/webpack-contrib/less-loader
608[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
609[chat-url]: https://gitter.im/webpack/webpack
610[size]: https://packagephobia.now.sh/badge?p=less-loader
611[size-url]: https://packagephobia.now.sh/result?p=less-loader
Note: See TracBrowser for help on using the repository browser.