webpack(带有sass-loader)-scss文件@import无法识别解析别名 [英] webpack (with sass-loader) - scss file @import does not recognize resolve alias

查看:1233
本文介绍了webpack(带有sass-loader)-scss文件@import无法识别解析别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目结构:

webpack.config.js
app--

   --> src
   ---->> components
   ------>>> myComponent.js
   ------>>> myComponent.scss

   --> styles
   ---->> variables.scss

在webpack.config module.exports中:

In webpack.config module.exports:

...
resolve: {
    alias: {
       styles: path.join(__dirname, 'app/styles') 
   }
}

在我的文件中-myComponent.scss:

In my file - myComponent.scss:

@import "styles/variables";

构建bundle.js时出现此错误:

I am getting this error when building bundle.js:

Module build failed:
@import "styles/variables";
^
      File to import not found or unreadable: styles/variables

如何我可以@import sass文件中的别名吗?

How can I @import aliases in sass files?

推荐答案

我能够在样式表上使用我在其中定义的别名使用以下命令进行webpack:

I was able to use, on a stylesheet, an alias that I defined in webpack by using the following:

@import '~alias/variables';

只是在别名前面加上此处

just by prefixing the alias with ~ did the trick for me, as suggested by documentation in here

这篇关于webpack(带有sass-loader)-scss文件@import无法识别解析别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆