Exporting SCSS Variables to JS (auto looping variables)

前端 未结 3 1357
闹比i
闹比i 2021-02-02 10:28

UPDATE: If you plan to implement the export solution, you must place it in a separate file to prevent redundant e

3条回答
  •  失恋的感觉
    2021-02-02 11:05

    Some improvements to the accepted answer:

    • Use camelcase so you will be able to individually export a variable.

    • Set the @each directive outside so it won't generate a new :export at-rule for each variable.


    _variables.scss

    $theme-colors: (
      'someColor': #000,
      'anotherColor': #FFF,
    );
    
    :export {
      @each $key, $value in $theme-colors {
        #{unquote($key)}: $value;
      }
    }
    

    app.js

    import styles from './core-styles/brand/_variables.scss' // { anotherColor: "#FFF", someColor: "#000" }
    import { someColor } from './core-styles/brand/_variables.scss' // #000
    

    Side note: I prefer using quotes inside Sass Maps, but you can omit them.

提交回复
热议问题