Persisting SCSS variables in rails asset pipeline?

前端 未结 4 745
被撕碎了的回忆
被撕碎了的回忆 2021-01-01 14:56

I\'m upgrading a rails app with lots of SCSS stylesheets to use the asset pipeline, and need to include some global variables and mixins for each file.

Adding severa

相关标签:
4条回答
  • 2021-01-01 15:12

    The default manifest syntax isn't powerful enough to give you useful Sass features like shared variables, mixins, etc. Instead, you should:

    1. Rename application.css to application.scss (or application.css.scss in Rails 4 or earlier)
    2. Instead of using the

      /*
       *= require variables
       *= require mixins
       *= require_tree .
       */
      

      nonsense, you should now use

      @import "variables";
      @import "mixins";
      @import "blah"; // import each SCSS file in your project like this.
      

      This will ensure you have full benefit of your variables and mixins throughout your project, and you are kept as DRY as Sass allows.

    0 讨论(0)
  • 2021-01-01 15:17

    Replace require with @import and remove any = require even if you think you've commented it out.

    0 讨论(0)
  • 2021-01-01 15:19

    Doesn't seem to be possible. Ended up prepending each file with @import 'includes/all'; and including everything else from includes/all.css.scss.

    0 讨论(0)
  • 2021-01-01 15:33

    Simply importing the necessary file from each Scss or Sass file seems to have worked for me. For example, I have a colors.scss file that includes some constants like this:

    $black: #222;
    

    I require it in my application.css manifest along with some other files:

    /*
     *= require colors
     *= require buttons
    */
    

    In my buttons.css.scss file, I simply do this to avoid the error:

    @import "colors";
    
    0 讨论(0)
提交回复
热议问题