Does webpack import fewer files using glob expressions?

I am trying to import less files automatically using webpack less-loader, but glob expressions do not work in root.less file.

In detail, I replace the gulp builder webpack, and I can use this template:

@import 'widgets/**/*.less'; 

to import fewer files into gulp automatically (check this link for the glob expression ). However, this pattern is not valid in webpack and less-loader seems to not support them.

I tried using the require.context method of webpack, but I cannot configure the import of orders or files. I need to require fewer files in a logical sequence because I use global variables (mixins, color codes, etc.). Therefore, this option is also unavailable.

import '../components/variables.less'; 
importAll(require.context('../components/', true, /\.less$/)); // Cannot set a sequence. 
// 'Variables' cannot be found even though I added it above 

So it seems I need to import each file manually, which is very painful. So I wonder if there is a way to import files automatically?

Thanks for the help!

0
source share
2 answers

I added a parameter pathsand it works:

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

Read more here: fooobar.com/questions/1671619 / ...

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

+2

glob, . , , :

   // update entry less file
var dir = require('node-dir');

function updateEntryLessFile() {
    dir.readFiles(path.resolve(__dirname, 'path-for-less-files-folder'), {
        match: /.less$/
    },
    (err, context, next) => { next() },
    (err, files) => {
        if (err) throw err;
        var wstream = fs.createWriteStream(
            path.resolve(__dirname, 'path-to-entry/entry.less')
        );
        _.each(files, path => {
            const relativePath = path.split('client/')[1]; // get relative path from full path
            wstream.write(`@import '~${relativePath}';\n`);
        });
        wstream.end();
    });
}

// When less files are added or removed, it updates entry.less file.
var watcher = require('chokidar').watch(path.resolve(__dirname, 'client/'), {
  persistent: true,
  ignoreInitial: true
});

// Something to use when events are received.
var log = console.log.bind(console);
// Add event listeners.
watcher
  .on('add', path => {
      if (new RegExp('.less').test(path)) {
          log(`Less file: ${path} has been added`)
          updateEntryLessFile();
      }
  })
  .on('unlink', path => {
      if (new RegExp('.less').test(path)) {
          log(`Less file: ${path} has been removed`)
          updateEntryLessFile();
      }
  });

updateEntryLessFile();

, , webpack- webpack .

, ! .

+1

Source: https://habr.com/ru/post/1671637/


All Articles