|
1 | 1 | # Less Plugin Module Resolver |
2 | 2 |
|
| 3 | + [](https://codecov.io/gh/bundle-matters/less-plugin-module-resolver) [](https://www.npmjs.com/package/less-plugin-module-resolver) [](https://npmcharts.com/compare/less-plugin-module-resolver?minimal=true) [](https://www.npmjs.com/package/less-plugin-module-resolver) |
| 4 | + |
| 5 | +> Inspired by [less-plugin-npm-import](https://github.com/less/less-plugin-npm-import) and [babel-plugin-module-resolver](https://github.com/tleunen/babel-plugin-module-resolver) |
| 6 | +
|
| 7 | +Adds the ability for less to set alias imports. Especially to handle usage like `~` in [webpack](https://webpack.js.org/loaders/less-loader/#imports) when using Rollup or other bundlers. |
| 8 | + |
| 9 | +## Install |
| 10 | + |
| 11 | +```shell |
| 12 | +# use npm |
| 13 | +npm install babel-plugin-module-resolver |
| 14 | + |
| 15 | +# use yarn |
| 16 | +yarn add babel-plugin-module-resolver |
| 17 | + |
| 18 | +# use pnpm |
| 19 | +pnpm install babel-plugin-module-resolver |
| 20 | +``` |
| 21 | + |
3 | 22 | ## Usage |
4 | 23 |
|
| 24 | +```less |
| 25 | +@import (less) '~normalize.css'; |
| 26 | +``` |
| 27 | + |
| 28 | +```js |
| 29 | +const less = require('less'); |
| 30 | +const ModuleResolverPlugin = require('less-plugin-module-resolver'); |
| 31 | + |
| 32 | +less.render('input', { |
| 33 | + plugins: [ |
| 34 | + new ModuleResolverPlugin({ |
| 35 | + alias: { |
| 36 | + '~': '', |
| 37 | + }, |
| 38 | + }), |
| 39 | + ], |
| 40 | +}); |
| 41 | +``` |
| 42 | + |
| 43 | +## Plugin Options |
| 44 | + |
| 45 | +### **rootDir** |
| 46 | + |
| 47 | +The directory to resolve less files. Default to `''`. |
| 48 | + |
| 49 | +### **alias** |
| 50 | + |
| 51 | +```js |
| 52 | +const less = require('less'); |
| 53 | +const ModuleResolverPlugin = require('less-plugin-module-resolver'); |
| 54 | + |
| 55 | +less.render('input', { |
| 56 | + plugins: [ |
| 57 | + new ModuleResolverPlugin({ |
| 58 | + rootDir: path.resolve(__dirname, 'src'), |
| 59 | + alias: { |
| 60 | + $: (_, restPath) => `styles${restPath}`), |
| 61 | + '^@mixins$': 'styles/mixins/index.less', |
| 62 | + }, |
| 63 | + }), |
| 64 | + ], |
| 65 | +}); |
| 66 | +``` |
| 67 | + |
| 68 | +When using the above configs: |
| 69 | + |
| 70 | +```less |
| 71 | +# origin |
| 72 | +@import "@mixins"; |
| 73 | +@import "$/variables/index.less"; |
| 74 | + |
| 75 | +# converted to |
| 76 | +@import "/User/me/project-foo/src/styles/mixins/index.less"; |
| 77 | +@import "/User/me/project-foo/src/styles/variables/index.less"; |
| 78 | +``` |
| 79 | + |
| 80 | +The alias config to resolve import less files. Alias key should be string including `regexp string`, corresponding replacer value should be string. You can use function replacer when the situation is more complicated. |
| 81 | + |
| 82 | +**More detailed api about alias see below:** |
| 83 | + |
| 84 | +#### **alias key** |
| 85 | + |
| 86 | +Alias key should be a `normal string` or `regexp string`. Mind that regexp string will pass to `RegexpConstructor` and should be not enclosed between slashes but started with `^` or end with `$`. |
| 87 | + |
| 88 | +##### More docs about `RegexpPatternString` |
| 89 | + |
| 90 | +- [`RegexpPatternString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp) |
| 91 | +- [`RegExp#literal_notation_and_constructor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/RegExp#literal_notation_and_constructor) |
| 92 | + |
| 93 | +Note that you can use `special replacement patterns` from `String.prototype.replace`, like `$1`, `$n`. See [String_Replace#specifying_a_string_as_a_parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter) |
| 94 | + |
5 | 95 | ```js |
6 | 96 | const less = require('less'); |
7 | 97 | const ModuleResolverPlugin = require('less-plugin-module-resolver'); |
8 | 98 |
|
9 | 99 | less.render('input', { |
10 | | - plugins: [new ModuleResolverPlugin()], |
| 100 | + plugins: [ |
| 101 | + new ModuleResolverPlugin({ |
| 102 | + rootDir: path.resolve(__dirname, 'src'), |
| 103 | + alias: { |
| 104 | + '^@theme/(.+)': 'styles/theme/$1.less', |
| 105 | + }, |
| 106 | + }), |
| 107 | + ], |
11 | 108 | }); |
12 | 109 | ``` |
| 110 | + |
| 111 | +#### **alias value** |
| 112 | + |
| 113 | +Alias value should be a string replacer or function replacer. The function replacer is similar to the 2rd argument/callback in `String.prototype.replace`. |
| 114 | + |
| 115 | +```ts |
| 116 | +type StringReplacer = string; |
| 117 | + |
| 118 | +type FunctionReplacer = (substring: string, ...args: any[]) => string; |
| 119 | +``` |
| 120 | + |
| 121 | +## License |
| 122 | + |
| 123 | +MIT |
0 commit comments