如何使用webpack从npm依赖项编译.less文件?

时间:2016-01-28 17:56:26

标签: node.js reactjs gruntjs less webpack

我正在使用npm模块elemental,它包含一个/less文件夹,其中包含所有相关样式的反应ui。我目前正尝试使用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'));

但这似乎不起作用,因为它会产生警告:

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 ^\.\/.*$

我正在解释这意味着您无法使用包含require()内文件的node_modules

修改

请注意,任何地方都没有.css个文件输出,输出/dist/app.js也无法正常工作。

1 个答案:

答案 0 :(得分:2)

为什么不试着写require('elemental/less/elemental.less');

在您的错误消息中:" 5:0-82依赖的请求是表达式"。