加载器列表中的元素应该有“加载器”和“加载器”。或者'装载机'使用sass-loader webpack

时间:2015-10-24 09:15:17

标签: sass loader webpack webpack-dev-server webpack-style-loader

我的webpack loader数组中有这个加载器:

{ test: /\.scss$/, exclude: /node_modules/, loaders: ExtractTextPlugin('style-loader', 'css-loader!sass-loader') }

我正在尝试让webpack构建我的SCSS到CSS,但是我收到了这个错误:

/Users/bli1/Development/Django/CL/cherngloong/cherngloong/cherngloong/node_modules/webpack/node_modules/webpack-core/lib/LoadersList.js:60
    throw new Error("Element from loaders list should have one of the fields 'loader' or 'loaders'");
    ^

Error: Element from loaders list should have one of the fields 'loader' or 'loaders'
    at getLoadersFromObject (/Users/bli1/Development/Django/CL/cherngloong/cherngloong/cherngloong/node_modules/webpack/node_modules/webpack-core/lib/LoadersList.js:60:8)
    at LoadersList.<anonymous> (/Users/bli1/Development/Django/CL/cherngloong/cherngloong/cherngloong/node_modules/webpack/node_modules/webpack-core/lib/LoadersList.js:78:12)
    at Array.map (native)
    at LoadersList.match (/Users/bli1/Development/Django/CL/cherngloong/cherngloong/cherngloong/node_modules/webpack/node_modules/webpack-core/lib/LoadersList.js:70:19)
    at NormalModuleFactory.<anonymous> (/Users/bli1/Development/Django/CL/cherngloong/cherngloong/cherngloong/node_modules/webpack/lib/NormalModuleFactory.js:109:65)
    at /Users/bli1/Development/Django/CL/cherngloong/cherngloong/cherngloong/node_modules/webpack/node_modules/async/lib/async.js:697:13
    at /Users/bli1/Development/Django/CL/cherngloong/cherngloong/cherngloong/node_modules/webpack/node_modules/async/lib/async.js:52:16
    at done (/Users/bli1/Development/Django/CL/cherngloong/cherngloong/cherngloong/node_modules/webpack/node_modules/async/lib/async.js:248:21)
    at /Users/bli1/Development/Django/CL/cherngloong/cherngloong/cherngloong/node_modules/webpack/node_modules/async/lib/async.js:44:16
    at /Users/bli1/Development/Django/CL/cherngloong/cherngloong/cherngloong/node_modules/webpack/node_modules/async/lib/async.js:694:17

这是我的完整webpack配置:

var path = require('path');
var webpack = require('webpack');
var BundleTracker = require('webpack-bundle-tracker');
var ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  entry: [
    './app/index'
  ],

  output: {
      path: path.resolve('./public/bundles/'),
      filename: "[name]-[hash].js",
  },

  plugins: [
    new BundleTracker({filename: './webpack-stats.json'}),
  ],

  module: {
    loaders: [
        { test: /\.css$/, exclude: /node_modules/, loader: 'style!css' },
        { test: /\.json$/, loader: 'json-loader' },
        { test: /\.jsx$/, loaders: ['react-hot', 'babel-loader'], include: path.join(__dirname, 'app') },
        { test: /\.es6$/, exclude: /node_modules/, loader: 'babel-loader?stage=0&optional=runtime'},
        { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?stage=0&optional=runtime'},
        { test: /\.scss$/, exclude: /node_modules/, loaders: ExtractTextPlugin('style-loader', 'css-loader!sass-loader') }
    ]
  },
}

1 个答案:

答案 0 :(得分:4)

Sokra提供了一个如何在此处使用ExtractTextPlugin的示例:https://github.com/webpack/extract-text-webpack-plugin/blob/master/example/webpack.config.js

正如您所看到的,您的示例和他的示例之间存在一些差异。

  • 使用“plugins:[]”数组中的 new 调用ExtractTextPlugin构造函数
  • 插件在loaders数组中的使用方式是调用静态方法.extract(),而不是简单地调用插件构造函数
  • “装载器对象”中的引用用于“loade r:”属性,而不是“loade rs:”,因为您已经放置(装载器是一个选项但需要一个数组)

希望有所帮助!