webpack (with sass-loader) - scss file @import does not recognize resolve alias

后端 未结 4 1586
慢半拍i
慢半拍i 2020-12-05 06:58

My project structure:

webpack.config.js
app--

   --> src
   ---->> components
   ------>>> myComponent.js
   ------>>> myComponen         


        
相关标签:
4条回答
  • 2020-12-05 07:17

    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

    0 讨论(0)
  • 2020-12-05 07:19

    Since your webpack.config.js file is already in the /app folder, shouldn’t the alias be:

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

    ?

    0 讨论(0)
  • 2020-12-05 07:25

    Another fix related to this subject, remove .scss

    @import '~scss/common.scss';
    

    should be

    @import '~scss/common';
    
    0 讨论(0)
  • 2020-12-05 07:29

    In my case the dependency is a node module, therefore I can import it like this:

    @import '~node-module-name/variables';

    And when using the actual node module dir name, my editor (PhpStorm) is not showing unresolved path error anymore (the problem which @tkiethanom mentioned). It looks like I need to specify alias in webpack config if I want to use sass style imports (e.g. my-package/colors instead of my-package/_colors.scss), and it seems it doesn't matter what is the name of that alias, as long as I use node module directory name

    0 讨论(0)
提交回复
热议问题