Sass加载程序无法在webpack

时间:2015-06-19 07:39:34

标签: node.js sass webpack

我试图在我的webpack配置中获得* .scss文件,但是当我运行webpack build命令时,我一直收到以下错误:

ERROR in ./~/css-loader!./~/sass-loader!./app/styles.scss
Module build failed: TypeError: Cannot read property 'sections' of null
at new SourceMapConsumer (/Users/sean/Development/playground/webpack.sass.test/node_modules/css-loader/node_modules/postcss/node_modules/source-map/lib/source-map/source-map-consumer.js:23:21)
at PreviousMap.consumer (/Users/sean/Development/playground/webpack.sass.test/node_modules/css-loader/node_modules/postcss/lib/previous-map.js:37:34)
at new Input (/Users/sean/Development/playground/webpack.sass.test/node_modules/css-loader/node_modules/postcss/lib/input.js:42:28)
at parse (/Users/sean/Development/playground/webpack.sass.test/node_modules/css-loader/node_modules/postcss/lib/parse.js:17:17)
at new LazyResult (/Users/sean/Development/playground/webpack.sass.test/node_modules/css-loader/node_modules/postcss/lib/lazy-result.js:54:47)
at Processor.process (/Users/sean/Development/playground/webpack.sass.test/node_modules/css-loader/node_modules/postcss/lib/processor.js:30:16)
at processCss (/Users/sean/Development/playground/webpack.sass.test/node_modules/css-loader/lib/processCss.js:168:24)
at Object.module.exports (/Users/sean/Development/playground/webpack.sass.test/node_modules/css-loader/lib/loader.js:21:15)
@ ./app/styles.scss 4:14-117

我不能为我的生活找出原因。这是一个非常基本的设置。

我创建了一个Dropbox共享,最低限度说明了这一点: https://www.dropbox.com/s/quobq29ngr38mhx/webpack.sass.test.zip?dl=0

解压缩然后运行:

npm install
webpack

这是我的webpack.config.js文件:

var path = require('path')
var webpack = require('webpack')

module.exports = {
  devtool: 'eval',
  entry: [
    './app'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'index.js',
    publicPath: '/dist/'
  },
  plugins: [
    new webpack.NoErrorsPlugin()
  ],
  resolve: {
    extensions: ['', '.js']
  },
  module: {
    loaders: [{
      test: /\.scss$/,
      loader: 'style-loader!css-loader!sass-loader'
    }]
  }
}

index.js条目文件:

require('./styles.scss');

alert('foo bar baz');

以及styles.scss文件:

body {
  background-color: #000;
}

它似乎遵循sass-loader文档站点的建议,但我无法运行它。

:(

有关我的环境的信息:

node - 0.12.4
npm  - 2.10.1
os   - OS X Yosemite

3 个答案:

答案 0 :(得分:23)

I have managed to get another workaround working that doesn't involve editing the css-loader libraries within my npm_modules directory (as per the answer by @chriserik).

If you add '?sourceMap' to the sass loader the css loader seems to handle the output.

Here is my updated configuration:

var path = require('path')
var webpack = require('webpack')

module.exports = {
  devtool: 'eval',
  entry: [
    './app'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'index.js',
    publicPath: '/dist/'
  },
  plugins: [
    new webpack.NoErrorsPlugin()
  ],
  resolve: {
    extensions: ['', '.js']
  },
  module: {
    loaders: [{
      test: /\.scss$/,
      loader: 'style!css!sass?sourceMap'
    }]
  }
}

P.S. I even expanded this test to include a compass-mixins include, and this worked too.

答案 1 :(得分:5)

遇到同样的问题后,我发现了这个:http://i.stack.imgur.com/mTO53.jpg][picture]

显然,现在的解决方案是手动修改/node_modules/css-loader/lib/loader.js的第17-19行

if(map && typeof map !== "string") {
    map = JSON.stringify(map);
}

这解决了我的问题。

答案 2 :(得分:0)

通过将source-map选项设置为true来解决问题(如其他答案中所示)。

但是如果您在查询字符串中发现混乱的传递选项,则有另一种选择;

用于配置sass加载器,您可以在webpack配置对象中创建sassLoader属性:

module.exports = {
  devtool: 'eval',
  entry: [
    './app'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'index.js',
    publicPath: '/dist/'
  },
  plugins: [
    new webpack.NoErrorsPlugin()
  ],
  resolve: {
    extensions: ['', '.js']
  },
  module: {
    loaders: [{
      test: /\.scss$/,
      loader: 'style!css!sass'
      // loader: ExtractPlugin.extract('style', 'css!sass'),
    }]
  },
  sassLoader: {
    sourceMap: true
  },
}