How to use absolute path to import custom scss, when using react + webpack?

后端 未结 4 1427
北恋
北恋 2021-02-05 04:47

Inside a scss file, I\'m trying to import custom, widely used chunk of scss (in a React/SASS/Webpack stack).

So that I can use a shared mixin.

Let\'s say I\'m cr

4条回答
  •  一生所求
    2021-02-05 05:27

    You could add stylesheets to the Webpack modules with resolve.modules

    // webpack.config.js
    const path = require('path')
    
    module.exports = {
      // ...
      resolve: {
        modules: [
          'node_modules',
          path.join(__dirname, 'path/to/stylesheets'),
        ],
      },
    }
    

    And sass-loader allows you to import _common-btn-styles.scss from the modules. https://webpack.js.org/loaders/sass-loader/#resolving-import-at-rules

    @import "~_common-btn-styles.scss";
    
    .my-admin-btn {
      // Use the shared mixins
    }
    

提交回复
热议问题