Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using less-plugin-glob with Webpack

I am trying to migrate the build system for an existing project from gulp to webpack.

It currently has a single entry point .less file which imports various other files as follows:

@import 'bower_components/bootstrap/less/bootstrap.less';
@import 'components/**/*.less';

This writes out a single css file which includes all of the .less files found. It uses https://github.com/just-boris/less-plugin-glob to allow globs.

Over in Webpack I have got as far as trying to use a combination of less-loader, css-loader and style-loader to achieve the same thing. The modules part of my webpack config looks like this:

var lessPluginGlob = require('less-plugin-glob');
...
{
    test: /\.less$/,
    use: [
      'style-loader',
      { loader: 'css-loader', options: { importLoaders: 1 } },
      { loader: 'less-loader', options: { lessPlugins: [lessPluginGlob] }}
    ]
},

and I am trying to require my "entry" less file like so:

require('./app.less');

but no matter what I do I get this:

ERROR in ./~/css-loader?{"importLoaders":1}!./~/less-loader?{"lessPlugins":[{}]}!./app/app.less
Module build failed: Can't resolve './components/**/*.less' in '/Users/matt/web-app/app'

Can anyone please point me in the right direction?

like image 647
Matt Wilson Avatar asked Mar 06 '17 15:03

Matt Wilson


1 Answers

I've made it work like this:

...
{
    loader: 'less-loader',
    options: {
        paths: [
            path.resolve(path.join(__dirname, 'app'))
        ],
        plugins: [
            require('less-plugin-glob')
        ]
    }
}

Check out the source code: https://github.com/webpack-contrib/less-loader/blob/master/src/getOptions.js#L22

I don't know whether it's an intendend behaviour, but if paths is not specified createWebpackLessPlugin being executed first, before less-plugin-glob plugin. And it throws an error since createWebpackLessPlugin knows nothing about next plugins.

So my solution was simply to specify paths to make less-plugin-glob being executed first.

Working example: https://github.com/notagency/webpack2-with-less-plugin-glob

like image 103
Dmitry Avatar answered Oct 13 '22 18:10

Dmitry