Skip to content

Commit 8c38dc5

Browse files
committed
Install plugins from lessLoader option
1 parent 29866af commit 8c38dc5

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ module.exports = {
5353

5454
See the [LESS documentation](http://lesscss.org/usage/#command-line-usage-options) for all available options. LESS translates dash-case to camelCase.
5555

56+
### LESS plugins
57+
58+
In order to use [plugins](http://lesscss.org/usage/#plugins), simply define
59+
the `lessLoader.lessPlugins` option. You can also change the options key
60+
with a query parameter: `"style!css!less?config=lessLoaderCustom"`.
61+
62+
``` javascript
63+
var LessPluginCleanCSS = require('less-plugin-clean-css');
64+
65+
module.exports = {
66+
module: {
67+
loaders: [...]
68+
},
69+
lessLoader:
70+
lessPlugins: [
71+
new LessPluginCleanCSS({advanced: true})
72+
]
73+
};
74+
```
75+
5676
## Note on imports
5777

5878
webpack provides an [advanced mechanism to resolve files](http://webpack.github.io/docs/resolving.html). The less-loader stubs less' `fileLoader` and passes all queries to the webpack resolving engine. Thus you can import your less-modules from `node_modules` or `bower_components`. Just prepend them with a `~` which tells webpack to look-up the [`modulesDirectories`](http://webpack.github.io/docs/configuration.html#resolve-modulesdirectories)

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = function(source) {
1818
var cb = this.async();
1919
var isSync = typeof cb !== "function";
2020
var finalCb = cb || this.callback;
21+
var configKey = query.config || 'lessLoader';
2122
var config = {
2223
filename: this.resource,
2324
paths: [],
@@ -44,6 +45,11 @@ module.exports = function(source) {
4445
config.plugins = config.plugins || [];
4546
config.plugins.push(webpackPlugin);
4647

48+
// If present, add custom LESS plugins.
49+
if (this.options[configKey]) {
50+
config.plugins = config.plugins.concat(this.options[configKey].lessPlugins || []);
51+
}
52+
4753
// not using the `this.sourceMap` flag because css source maps are different
4854
// @see https://github.com/webpack/css-loader/pull/40
4955
if (query.sourceMap) {

test/index.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ describe("less-loader", function() {
3636
query: "?sourceMap",
3737
devtool: "source-map"
3838
});
39+
test("should install plugins", "url-path", {
40+
query: "?config=lessLoaderTest",
41+
lessPlugins: [
42+
{ wasCalled: false, install: function() {this.wasCalled = true;} }
43+
],
44+
after: function(testVariables) {
45+
this.lessPlugins[0].wasCalled.should.be.true;
46+
}
47+
});
3948
it("should report error correctly", function(done) {
4049
webpack({
4150
entry: path.resolve(__dirname, "../index.js") + "!" +
@@ -109,6 +118,9 @@ function test(name, id, testOptions) {
109118
path: __dirname + "/output",
110119
filename: "bundle.js",
111120
libraryTarget: "commonjs2"
121+
},
122+
lessLoaderTest: {
123+
lessPlugins: testOptions.lessPlugins || []
112124
}
113125
}, function onCompilationFinished(err, stats) {
114126
var actualMap;

0 commit comments

Comments
 (0)