webpack的sass-loader没有找到没有下划线前缀的基础scss文件

时间:2015-06-23 13:51:47

标签: css node.js sass webpack

最近,我正在使用 webpack sass bower foundation5 开发快速前端开发工具链。

我遇到了一个问题: sass-loader仅使用下划线前缀加载scss文件。

环境

  

node v0.12.4
webpack 1.9.11
node-sass 3.2.0
sass-loader 1.0.2
os osntoo 3.18

webpack.config.js

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

var getDir = function() {
  var args = Array.prototype.slice.call(arguments);
  return path.join.apply(path, [__dirname].concat(args));
};

module.exports = {
  // webpack options 
  context: getDir('./src'),

  entry: {
    test: "./style/test.scss"
  },

  output: {
    path: getDir('./build'),
    filename: "[name].js"
  },
  module: {
    loaders: [
    { test: /\.scss$/, loader: "style!css!sass?outputStyle=expanded&includePaths[]=" + getDir('bower_components', 'foundation', 'scss')}
    ]
  },

  progress: false, // Don't show progress 
  // Defaults to true 

  failOnError: true, // don't report error to grunt if webpack find errors 
  // Use this if webpack errors are tolerable and grunt should continue 

  watch: true, // use webpacks watcher 
  // You need to keep the grunt process alive 

  keepalive: false, // don't finish the grunt task 
  // Use this in combination with the watch option 

  devtool: 'eval'
};

test.scss

@import "settings";
@import "normalize";
@import "foundation/components/panels";
@import "foundation/components/type";

settings.scss

empty file

项目布局

- bower_componets
- package.json
- src
- 风格
--- test.scss
--- settings.scss
- webpack.config.js

当我运行 webpack 命令时,我收到错误:

  

错误   ../~/css-loader!../~/sass-loader?outputStyle=expanded&includePaths[]=/home/ray/test/testsassloader/bower_co   mponents /基础/ SCSS!./风格/ test.scss
  模块构建失败:@import“normalize”;          ^         要导入的文件未找到或不可读:./_ normalize.scss         在/home/ray/test/testsassloader/src/style/test.scss(第2行,第9列)@ ./style/test.scss 4:14-220

虽然,我将 bower_components / foundation / scss / normalize.scss 复制到 bower_components / foundation / scss / _normalize.scss ,但它确实有效。

所以我尝试在没有_normalize.scss的情况下运行node-sass:

 ./node_modules/node-sass/bin/node-sass --include-path=$(pwd)/bower_components/foundation/scss/ src/style/test.scss

有效!!!

我的结论是webpack解析器导致问题! 任何人都可以帮我解决问题?副本做得对吗?

1 个答案:

答案 0 :(得分:2)

在webpack.config中添加bower组件目录以解析modulesDirectories,

{
...
      resolve: {
        modulesDirectories: ['./bower_components', 'node_modules']
      },
      module: {
      ...
      }
...
}

然后,在test.scss上导入这样的基础:

@import "settings";
@import "~foundation/scss/normalize";
@import "~foundation/scss/foundation";