简体   繁体   English

了解Webpack SASS加载器和文件路径

[英]Understanding Webpack SASS loader and file paths

I got some troubles to understand how the file path resolution works. 我很难理解文件路径解析的工作原理。 I am using both CSS and SASS loaders in a standard way: 我以标准方式同时使用CSS和SASS加载程序:

var ExtractTextPlugin = require('extract-text-webpack-plugin'); var ExtractTextPlugin = require('extract-text-webpack-plugin');

module: {
    loaders: [
        { test: /\.scss$/, loader: ExtractTextPlugin.extract('css!sass') }
    ]
},
plugins: [
    new ExtractTextPlugin('build/[name].min.css', {
        allChunks: true
    })
]

So, I expect all my SASS to be compiled and render in build/app.min.css file. 因此,我希望所有SASS都可以在build/app.min.css文件中进行编译和呈现。 It works well. 它运作良好。 Except I got issues with fonts loading and includes. 除非我在字体加载和包含方面遇到问题。

Let's consider I work on a /src/sass/style.scss file, and I want to include the font-awesome SASS file as following: 让我们考虑一下我在/src/sass/style.scss文件中工作,并且我想包含字体真棒的SASS文件,如下所示:

@import '~font-awesome/scss/font-awesome.scss';

It doesn't work. 没用 Yet, if I use a relative path such as: 但是,如果我使用相对路径,例如:

@import '../../node_modules/font-awesome/scss/font-awesome.scss';

It works. 有用。

Then, I want to change the font-awesome font path to match correct folder: 然后,我想更改字体真棒字体路径以匹配正确的文件夹:

$fa-font-path: '~font-awesome/fonts';

It also works. 它也可以。 Which seems logical to me, as, as far as I understood the concept, the ~ refers to the node_modules folder. 就我所知,按照我的理解, ~表示node_modules文件夹。

So, why does it work with the font path and not with the SASS import? 那么,为什么它与字体路径一起工作而不与SASS导入一起工作? I would like to use ~ notation too, as this is a file used by several projects, some of them requiring a ../../../node_modules . 我也想使用~表示法,因为这是多个项目使用的文件,其中一些项目需要../../../node_modules

Thanks for your help! 谢谢你的帮助! :) :)

wow, you write the article that made my life much easier on Webpack, and I have the privilege of helping you out in return. 哇,您写的这篇文章使我在Webpack上的生活变得更加轻松,我有幸为您提供帮助。

I think the issue here is that the first argument being used with ExtractTextPlugin isn't supposed to specify the file path, just the file location. 我认为这里的问题是,与ExtractTextPlugin一起使用的第一个参数不应指定文件路径,而仅指定文件位置。

So you can try changing that to just [name].min.css and specify the output location elsewhere. 因此,您可以尝试将其更改为[name].min.css并在其他位置指定输出位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM