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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With