简体   繁体   中英

How to compile .less files from an npm dependency using webpack?

I'm using the npm module elemental , which contains a /less folder with all the relevant styling for its react ui. I'm currently trying to do this with less-loader :

/tasks/config/webpack.js :

'use strict';

let path = require('path');
let ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = function(grunt) {

  grunt.config.set('webpack', {
    client: {
      entry: {
        app: `${process.cwd()}/src/app.jsx`,
      },
      output: {
        filename: '[name].js',
        chunkFilename: '[id].js',
        path: `${process.cwd()}/dist`,
      },
      module: {
        loaders: [
          {
            test: /\.jsx?$/,
            exclude: /(node_modules|bower_components|dist)/,
            loader: 'babel',
            query: {
              presets: ['react', 'es2015'],
            },
          },
          {
            test: /\.less$/,
            loader: ExtractTextPlugin.extract('style-loader', 'css-loader!less-loader'),
          },
        ],
      },
      plugins: [
        new ExtractTextPlugin('[name].css'),
      ],
    },
  });

  grunt.loadNpmTasks('grunt-webpack');

};

/src/app.jsx :

'use strict';

let path = require('path');

require(path.resolve(process.cwd(), 'node_modules/elemental/less/elemental.less'));

But this doesn't seem to work, as it generates a warning:

WARNING in ./src/app.jsx
Critical dependencies:
5:0-82 the request of a dependency is an expression
 @ ./src/app.jsx 5:0-82

WARNING in ./src ^\.\/.*$
Module not found: Error: a dependency to an entry point is not allowed
 @ ./src ^\.\/.*$

I'm interpreting this to mean that you can't use require() s that include files from inside node_modules .

Edit

Just to note, there are no .css files output anywhere, nor does the output /dist/app.js work properly.

Why not try just write require('elemental/less/elemental.less'); ?

In your error message: "5:0-82 the request of a dependency is an expression ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM