Using less-plugin-glob with Webpack

陌路散爱 提交于 2019-12-23 11:48:23

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/42629652/using-less-plugin-glob-with-webpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!