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

Mr.*_*. C 5 css sass node.js webpack

最近,我正在研究带有webpack,sass,bowerfoundation5的快速前端开发工具链.

我遇到一个问题:sass-loader只加载带下划线前缀的 scss文件.

环境

node v0.12.4
webpack 1.9.11
node-sass 3.2.0
sass-loader 1.0.2
os gentoo 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'
};
Run Code Online (Sandbox Code Playgroud)

test.scss

@import "settings";
@import "normalize";
@import "foundation/components/panels";
@import "foundation/components/type";
Run Code Online (Sandbox Code Playgroud)

settings.scss

empty file
Run Code Online (Sandbox Code Playgroud)

项目布局

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

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

错误在../~/css-loader!../~/sass-loader?outputStyle=expanded&includePaths[]=/home/ray/test/testsassloader/bower_co mponents/foundation/scss!./ style/test.scss
模块构建失败:@import"normalize"; ^要导入的文件未找到或不可读:/home/ray/test/testsassloader/src/style/test.scss中的./_normalize.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
Run Code Online (Sandbox Code Playgroud)

有用!!!

我得出结论,这是webpack解析器引起的问题!! 任何人都可以帮我解决问题?副本做得对吗?

小智 2

在 webpack.config 添加 Bower 组件目录来解析 moduleDirectories,

{
...
      resolve: {
        modulesDirectories: ['./bower_components', 'node_modules']
      },
      module: {
      ...
      }
...
}
Run Code Online (Sandbox Code Playgroud)

然后,在 test.scss 中导入基础:

@import "settings";
@import "~foundation/scss/normalize";
@import "~foundation/scss/foundation";
Run Code Online (Sandbox Code Playgroud)