Webpack2 加载和提取 LESS 文件 [英] Webpack2 loading and extracting LESS file

查看:43
本文介绍了Webpack2 加载和提取 LESS 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置 Webpack2 来处理我的 LESS 文件并从中创建一个单独的 CSS.但是,我不断收到错误消息.我无法找到概述流程的 Webpack2 示例,因此不确定我遗漏了什么.

I am trying to setup Webpack2 to process my LESS files and create a separate CSS from it. I keep getting an error however. I've had trouble locating Webpack2 examples outlining the process so not sure what I am missing.

我的 Webpack 配置:

My Webpack config:

module.exports = {
  entry: {
    'public': [
      './src/client/styles/public.js'
    ]
  },
  output: {
   ...
  },
  module: {
      {
       test: /.*\.less$/,
        loader: ExtractTextPlugin.extract({ loader: 'less-loader', fallbackLoader: 'style-loader' })
      }
    ]

  },
  plugins: [
    new ExtractTextPlugin({ filename: '[name].css', disable: false, allChunks: true })
  ]
}

public.js 文件(我也尝试将 less 文件直接传递给 entry,结果相同):

The public.js file (I also tried passing the less file directly to entry with same result):

require('style.less')

style.less 文件

a { color: red; }

错误:

ERROR in ./~/less-loader!./src/client/styles/style.less
Module parse failed: /node_modules/less-loader/index.js!/src/client/styles/style.less Unexpected token (1:2)
You may need an appropriate loader to handle this file type.
| a {
|   color: red;
| }

从消息中可以看出,less loader 正在正确地传递文件,但中断了.我试过一个空白文件,但后来我得到:

It appears from the message that the less loader is correctly being passed the file but breaks. I've tried a blank file but then I get:

TypeError: text.forEach is not a function

版本:

"extract-text-webpack-plugin": "^2.0.0-beta.4",
"less": "^2.7.1",
"less-loader": "^2.2.3",
"style-loader": "^0.13.1",
"webpack": "^2.1.0-beta.25",
"webpack-dev-server": "^2.1.0-beta.5",
"yargs": "~3.5.4"

可能是什么问题?

推荐答案

好的.. 搞定了:

 loader: ExtractTextPlugin.extract({ 
     loader:[ 'css', 'less' ], 
     fallbackLoader: 'style-loader' 
 })

编辑 这是针对 Webpack v2 的 - 感谢 @thelastshadow 关于 Webpack 4 问题的注释 此处.

Edit This was for Webpack v2 - thanks to @thelastshadow for note on Webpack 4 issue here.

这篇关于Webpack2 加载和提取 LESS 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆