如何从当前webpack配置的sass-loader获取有效的css?

时间:2016-08-23 12:34:05

标签: sass webpack css-loader sass-loader

我即将为我的公司创建一个模式库。我使用以下软件:

  • css-loader@0.23.1
  • extract-text-webpack-plugin@1.0.1
  • node-sass@3.8.0
  • sass-loader@4.0.0
  • style-loader@0.13.1
  • webpack@1.13.2
  • resolve-url-loader@1.6.0
  • node@6.3.1
  • npm@3.10.3

在我的电脑上(Windows 10)。

我的目标是使用@import "colors"语句并获取相应的文件../patterns/globals/_colors.css。我创建了一个存储库,以简化代码向您展示我的情况:https://github.com/jbrieske/webpack-sass-fail。我尝试了sass-loader的includePaths配置和带有属性root的resolve-url-loader。但到目前为止都没有工作......

在我第一次使用sass-loader和includePaths运行(提交:14622da5e6eb77ec51644601b94942c3b51eb76a)时,我收到了以下错误报告:

Hash: 396f0bfb9d565b6f60f0
Version: webpack 1.13.2
Time: 522ms
    + 1 hidden modules

ERROR in ./styles.scss
Module build failed: ModuleNotFoundError: Module not found: Error: Cannot resolve 'file' or 'directory' ./colors in ~/projects/webpack-sass-fail/app
    at ~/projects/webpack-sass-fail/app/node_modules/webpack/lib/Compilation.js:229:38
    at onDoneResolving (~/projects/webpack-sass-fail/app/node_modules/webpack/lib/NormalModuleFactory.js:29:20)
    at ~/projects/webpack-sass-fail/app/node_modules/webpack/lib/NormalModuleFactory.js:85:20
    at ~/projects/webpack-sass-fail/app/node_modules/async/lib/async.js:726:13
    at ~/projects/webpack-sass-fail/app/node_modules/async/lib/async.js:52:16
    at done (~/projects/webpack-sass-fail/app/node_modules/async/lib/async.js:241:17)
    at ~/projects/webpack-sass-fail/app/node_modules/async/lib/async.js:44:16
    at ~/projects/webpack-sass-fail/app/node_modules/async/lib/async.js:723:17
    at ~/projects/webpack-sass-fail/app/node_modules/async/lib/async.js:167:37
    at ~/projects/webpack-sass-fail/app/node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js:24:19
    at onResolved (~/projects/webpack-sass-fail/app/node_modules/enhanced-resolve/lib/Resolver.js:38:18)
    at ~/projects/webpack-sass-fail/app/node_modules/enhanced-resolve/lib/Resolver.js:127:10
    at ~/projects/webpack-sass-fail/app/node_modules/enhanced-resolve/lib/Resolver.js:191:15
    at applyPluginsParallelBailResult.createInnerCallback.log (~/projects/webpack-sass-fail/app/node_modules/enhanced-resolve/lib/Resolver.js:110:4)
    at loggingCallbackWrapper (~/projects/webpack-sass-fail/app/node_modules/enhanced-resolve/lib/createInnerCallback.js:21:19)
    at ~/projects/webpack-sass-fail/app/node_modules/tapable/lib/Tapable.js:134:6
    at Tapable.<anonymous> (~/projects/webpack-sass-fail/app/node_modules/enhanced-resolve/lib/DirectoryDescriptionFilePlugin.js:24:12)
    at Storage.finished (~/projects/webpack-sass-fail/app/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:38:16)
    at ReadFileContext.callback (~/projects/webpack-sass-fail/app/node_modules/graceful-fs/graceful-fs.js:78:16)
    at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:365:13)
Child extract-text-webpack-plugin:
        + 2 hidden modules

    ERROR in ./~/css-loader!./styles.scss
    Module not found: Error: Cannot resolve 'file' or 'directory' ./colors in ~\projects\webpack-sass-fail\app
     @ ./~/css-loader!./styles.scss 3:10-66

    ERROR in ./~/css-loader!./styles.scss
    Module not found: Error: Cannot resolve 'file' or 'directory' ./typography in ~\projects\webpack-sass-fail\app
     @ ./~/css-loader!./styles.scss 4:10-70

使用resolve-url-loader进行第二次运行(提交:3dfb374d7a548169d4d1d890b27895a0f8d18a21)后,我收到了另一个错误报告:

Hash: 0f2ad6d2bedd426d776a
Version: webpack 1.13.2
Time: 605ms
    Asset     Size  Chunks             Chunk Names
bundle.js  1.42 kB       0  [emitted]  app
   [0] ./client.js 27 bytes {0} [built]
    + 1 hidden modules

ERROR in ./styles.scss
Module build failed: ModuleBuildError: Module build failed: CssSyntaxError: css-loader!~/projects/webpack-sass-fail/patterns/globals/colors.scss:14:9: Unknown word
You tried to parse SCSS with the standard CSS parser; try again with the postcss-scss parser
    at Input.error (~/projects/webpack-sass-fail/app/node_modules/postcss/lib/input.js:120:22)
    at Parser.unknownWord (~/projects/webpack-sass-fail/app/node_modules/postcss/lib/parser.js:457:26)
    at Parser.word (~/projects/webpack-sass-fail/app/node_modules/postcss/lib/parser.js:174:14)
    at Parser.loop (~/projects/webpack-sass-fail/app/node_modules/postcss/lib/parser.js:60:26)
    at parse (~/projects/webpack-sass-fail/app/node_modules/postcss/lib/parse.js:26:16)
    at new LazyResult (~/projects/webpack-sass-fail/app/node_modules/postcss/lib/lazy-result.js:80:24)
    at Processor.process (~/projects/webpack-sass-fail/app/node_modules/postcss/lib/processor.js:200:12)
    at processCss (~/projects/webpack-sass-fail/app/node_modules/css-loader/lib/processCss.js:188:11)
    at Object.module.exports (~/projects/webpack-sass-fail/app/node_modules/css-loader/lib/loader.js:24:2)
    at DependenciesBlock.onModuleBuildFailed (~/projects/webpack-sass-fail/app/node_modules/webpack-core/lib/NormalModuleMixin.js:315:19)
    at nextLoader (~/projects/webpack-sass-fail/app/node_modules/webpack-core/lib/NormalModuleMixin.js:270:31)
    at ~/projects/webpack-sass-fail/app/node_modules/webpack-core/lib/NormalModuleMixin.js:292:15
    at context.callback (~/projects/webpack-sass-fail/app/node_modules/webpack-core/lib/NormalModuleMixin.js:148:14)
    at Object.<anonymous> (~/projects/webpack-sass-fail/app/node_modules/css-loader/lib/loader.js:32:18)
    at ~/projects/webpack-sass-fail/app/node_modules/css-loader/lib/processCss.js:211:3
    at runMicrotasksCallback (internal/process/next_tick.js:58:5)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)
Child extract-text-webpack-plugin:
        + 4 hidden modules

    ERROR in ./~/css-loader!../patterns/globals/colors.scss
    Module build failed: CssSyntaxError: css-loader!~/projects/webpack-sass-fail/patterns/globals/colors.scss:14:9: Unknown word
    You tried to parse SCSS with the standard CSS parser; try again with the postcss-scss parser
        at Input.error (~/projects/webpack-sass-fail/app/node_modules/postcss/lib/input.js:120:22)
        at Parser.unknownWord (~/projects/webpack-sass-fail/app/node_modules/postcss/lib/parser.js:457:26)
        at Parser.word (~/projects/webpack-sass-fail/app/node_modules/postcss/lib/parser.js:174:14)
        at Parser.loop (~/projects/webpack-sass-fail/app/node_modules/postcss/lib/parser.js:60:26)
        at parse (~/projects/webpack-sass-fail/app/node_modules/postcss/lib/parse.js:26:16)
        at new LazyResult (~/projects/webpack-sass-fail/app/node_modules/postcss/lib/lazy-result.js:80:24)
        at Processor.process (~/projects/webpack-sass-fail/app/node_modules/postcss/lib/processor.js:200:12)
        at processCss (~/projects/webpack-sass-fail/app/node_modules/css-loader/lib/processCss.js:188:11)
        at Object.module.exports (~/projects/webpack-sass-fail/app/node_modules/css-loader/lib/loader.js:24:2)
     @ ./~/css-loader!./styles.scss 3:10-89

    ERROR in ./~/css-loader!../patterns/globals/typography.scss
    Module not found: Error: Cannot resolve 'file' or 'directory' ./colors in ~/projects/webpack-sass-fail/patterns/globals
     @ ./~/css-loader!../patterns/globals/typography.scss 3:10-76

我尝试使用以下内容编译一个simle sass:

$grey: lightgrey;
$family: "Tahoma";

body {
  font-size: 12px;
}

p {
  border: 1px solid $grey;
  font-family: $family;
}

使用上面提到的webpack配置我没有收到任何错误消息,但是像这样的CSS代码无效:

$family: "Tahoma";
$grey: lightgrey;

body {
  font-size: 12px;
}

p {
  border: 1px solid $grey;
  font-family: $family;
}

我不明白失败的原因在哪里。

祝你好运, JBrieske

编辑:在@IRus的帮助下,我设法解决了我的问题。您可以在上述存储库中找到工作示例:https://github.com/jbrieske/webpack-sass-fail

1 个答案:

答案 0 :(得分:5)

模块构建失败:ModuleNotFoundError:找不到模块:错误:无法解析&#39; file&#39;或者&#39;目录&#39; 〜/ projects / webpack-sass-fail / app

中的./colors

color中将color.scss重命名为typography.scss。或者在webpack配置中正确设置resolve

您尝试使用标准CSS解析器解析SCSS;再次使用postcss-scss解析器

ExtractTextPlugin方式更改加载程序:

{test: /\.scss$/, loader: ExtractTextPlugin.extract('style', 'css!resolve-url!sass')}
// or
{ test: /\.scss$/, loader: ExtractTextPlugin.extract('style', ['css', 'resolve-url', 'sass']) }

因为这相当于:

{test: /\.scss$/, loader: ExtractTextPlugin.extract('style', 'css')}
{test: /\.scss$/, loader: ExtractTextPlugin.extract('style', 'css', 'resolve-url', 'sass')}

比你得到新的错误:

未定义的变量:&#34; $ grey&#34;。

这只是在styles.scss中拼写错误:

// border: 1px solid $grey;
border: 1px solid $gray;