简体   繁体   English

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

[英]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. 我正在使用npm模块elemental ,其中包含一个/less文件夹,该文件夹具有react ui的所有相关样式。 I'm currently trying to do this with less-loader : 我目前正在尝试使用less-loader来做到这一点:

/tasks/config/webpack.js : /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 : /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 . 我将其解释为您不能使用包含来自node_modules内部文件的require()

Edit 编辑

Just to note, there are no .css files output anywhere, nor does the output /dist/app.js work properly. 请注意,任何地方都没有输出.css文件,输出/dist/app.js/dist/app.js正常工作。

Why not try just write require('elemental/less/elemental.less'); 为什么不尝试只写require('elemental/less/elemental.less'); ?

In your error message: "5:0-82 the request of a dependency is an expression ". 在您的错误消息中:“ 5:0-82依赖项的请求是一个表达式 ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 npm使用cpx复制和编译LESS文件 - npm copy and compile LESS files using cpx 如何指示webpack观察NPM链接依赖项(包)中的更改 - How to instruct webpack to watch changes in an NPM linked dependency (package) 无法解决依赖关系:npm ERR! 来自 uglifyjs-webpack-plugin@2.2.0 的对等 webpack@"^4.0.0" - Could not resolve dependency: npm ERR! peer webpack@"^4.0.0" from uglifyjs-webpack-plugin@2.2.0 使用带有 webpack 的 npm run build 后如何使用 nginx 提供静态文件 - How to serve static files with nginx after using npm run build with webpack 如何在Windows环境中通过Rake任务调用Nodejs来编译一些LESS文件? - How do I call Nodejs in Windows environment from a Rake task to compile some LESS files? 使用较少的中间件将多个源CSS文件编译为一个? - Using less-middleware to compile multiple source CSS files into one? 如何使用 npm 命令编译打字稿? - How to compile typescript using npm command? 如何设置Grunt文件以观看和编译较少的文件 - How to setup a grunt file to watch and compile less files npm:引用一个peer依赖; 如何从对等依赖项中对齐版本 - npm: refer to a peer dependency; how to align the version from a peer dependency 如何打包一个npm模块? - How to webpack a npm module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM