Webpack,sass-loader(或css-loader)嵌套文件导入node_modules。文件未找到

时间:2017-09-03 20:26:18

标签: css node.js webpack css-loader sass-loader

我想使用primercss框架作为sass。我正在使用webpack来构建我的应用程序。我已经下载了npm包并使用以下命令导入它:@import "~primer-css/index.scss";。问题是构建失败并出现此错误:File to import not found or unreadable: primer-core/index.scss,它是primer-css/index.scss文件中的子导入。我认为这是一个与webpack sass-loader或css-loader相关的问题,全栈跟踪如下:

ERROR in ./node_modules/css-loader?{"modules":true,"minimize":true,"sourceMap":true,"importLoaders":2,"localIdentName":"[name]__[local]"}!./node_modules/postcss-loader/lib?{"config":{"path":"/Users/vicaba/Projects/medself/medself-client/development/webpack/postcss.config.js"},"sourceMap":true}!./node_modules/sass-loader/lib/loader.js?{"outputStyle":"expanded","sourceMap":true,"sourceMapContents":true}!./src/theme/theme.scss
Module build failed:
@import "primer-core/index.scss";
^
      File to import not found or unreadable: primer-core/index.scss.
Parent style sheet: /Users/vicaba/Projects/medself/medself-client/development/node_modules/primer-css/index.scss
      in /Users/vicaba/Projects/medself/medself-client/development/node_modules/primer-css/index.scss (line 14, column 1)
Error:
@import "primer-core/index.scss";
^
      File to import not found or unreadable: primer-core/index.scss.
Parent style sheet: /Users/vicaba/Projects/medself/medself-client/development/node_modules/primer-css/index.scss
      in /Users/vicaba/Projects/medself/medself-client/development/node_modules/primer-css/index.scss (line 14, column 1)
    at options.error (/Users/vicaba/Projects/medself/medself-client/development/node_modules/node-sass/lib/index.js:291:26)
 @ ./src/theme/theme.scss 4:14-200
 @ ./src/bootstrap.js
 @ multi ./src/bootstrap.js

ERROR in ./src/theme/theme.scss
Module build failed: ModuleBuildError: Module build failed:
@import "primer-core/index.scss";
^
      File to import not found or unreadable: primer-core/index.scss.
Parent style sheet: /Users/vicaba/Projects/medself/medself-client/development/node_modules/primer-css/index.scss
      in /Users/vicaba/Projects/medself/medself-client/development/node_modules/primer-css/index.scss (line 14, column 1)
    at runLoaders (/Users/vicaba/Projects/medself/medself-client/development/node_modules/webpack/lib/NormalModule.js:194:19)
    at /Users/vicaba/Projects/medself/medself-client/development/node_modules/loader-runner/lib/LoaderRunner.js:364:11
    at /Users/vicaba/Projects/medself/medself-client/development/node_modules/loader-runner/lib/LoaderRunner.js:230:18
    at context.callback (/Users/vicaba/Projects/medself/medself-client/development/node_modules/loader-runner/lib/LoaderRunner.js:111:13)
    at Object.asyncSassJobQueue.push [as callback] (/Users/vicaba/Projects/medself/medself-client/development/node_modules/sass-loader/lib/loader.js:55:13)
    at Object.<anonymous> (/Users/vicaba/Projects/medself/medself-client/development/node_modules/async/dist/async.js:2244:31)
    at Object.callback (/Users/vicaba/Projects/medself/medself-client/development/node_modules/async/dist/async.js:906:16)
    at options.error (/Users/vicaba/Projects/medself/medself-client/development/node_modules/node-sass/lib/index.js:294:32)

加载器配置如下:

{
  test: /\.scss$/,
  use: ExtractTextPlugin.extract({
    fallback: 'style-loader',
    use: [
      {
        loader: 'css-loader',
        options: {
          modules: true,
          minimize: true,
          sourceMap: true,
          importLoaders: 2,
          localIdentName: '[name]__[local]'
        }
      },
      {
        loader: 'postcss-loader',
        options: {
          config: {
            path: path.resolve(__dirname, 'postcss.config.js')
          },
          sourceMap: true
        }
      },
      {
        loader: 'sass-loader',
        options: {
          outputStyle: 'expanded',
          sourceMap: true,
          sourceMapContents: true
        }
      }
    ]
  })
}

任何人都可以帮助我吗?或者说明这个问题?

1 个答案:

答案 0 :(得分:1)

所以我认为我应该说这个问题似乎没有得到解决,如果 sass-loader 正式表示没有问题,它似乎无法解决。因为这是 node-sass 应该如何工作的。

如果人们对阅读官方问题主题感兴趣,那么它仍会在github上的here上打开。线程中描述了几个快速黑客,但是对我来说它们都没有那么好用,虽然它们基本上都做同样的事情,包括sass-imports中的node_modules文件夹。

他们这样做的原因是因为您已经描述了一个简单的文件,如下所示:

 @import "~primer-css/index.scss";

这依赖于node_modules文件夹中的子导入或外部SASS文件。具体来说,这些:

/*!
 * Primer
 * http://primer.github.io
 *
 * Released under MIT license. Copyright (c) 2017 GitHub Inc.
 */

// Primer master file
//
// Imports all Primer files in their intended order for easy mass-inclusion.
// Should you need specific files, you can easily use separate `@import`s.

// Global requirements
@import "primer-core/index.scss";
@import "primer-product/index.scss";
@import "primer-marketing/index.scss";

由于这些子导入中没有一个在其前面有~,因此它们无法解析。

事实上,如果您只是尝试编译primer-support/index.scss,它实际上是开箱即用的,因为它的所有导入仅依赖于它自己的模块,因此它可以解决:

// variables
@import "./lib/variables/typography.scss";
@import "./lib/variables/colors.scss";
@import "./lib/variables/layout.scss";
@import "./lib/variables/misc.scss";

// mixins
@import "./lib/mixins/typography.scss";
@import "./lib/mixins/layout.scss";
@import "./lib/mixins/buttons.scss";
@import "./lib/mixins/misc.scss";

尽管如此,经过一段时间后应该采取的措施似​​乎还有很长的路要走,正如commented in this post

   {
    test: /\.s?css$/,
    loaders: [
      'css',
      'sass?includePaths[]='+ path.resolve(__dirname, 'node_modules') +
        '&includePaths[]='+ path.resolve(__dirname, 'bower_components')
        // tells sass-loader to look in these dirs when resolving files
    ]
  }

我改为:

        {
            test: /\.scss$/,
            use: ExtractTextPlugin.extract({
                fallback: "style-loader",
                use: "css-loader?importLoaders=1!postcss-loader!sass-loader?includePaths[]=" + resolve(__dirname, 'node_modules')
            })
        }

因为我只想检查SCSS,因为我希望将我的CSS和SCSS捆绑在一起并缩小。

但是,对于大多数人来说,这可能不会起作用,所以我想修改一下你的代码示例:

     {
        loader: 'sass-loader',
        options: {
          config: {
            path: path.resolve(__dirname, 'node_modules')
          },
          outputStyle: 'expanded',
          sourceMap: true,
          sourceMapContents: true
        }
      }

但我发现config非常棘手,所以最终我认为包含它的最简单方法就是将加载器use修改为:

sass-loader?includePaths[]=" + resolve(__dirname, 'node_modules')