Code Monkey home page Code Monkey logo

sass-vars-loader's Introduction

Sass Vars Loader

Import Sass vars from Webpack config or from JS/JSON files

Travis Maintainability Codecov npm version npm installs dependencies

This loader allows you to use Sass variables defined in:
  • ✅ JSON Files
  • ✅ JavaScript Files
  • ✅ Inlined in Webpack Config
  • Supports both syntax types:
  • ✅ SASS Syntax
  • ✅ SCSS Syntax
  • Supports hot reload:
  • ✅ HMR Enabled

  • Install

    using npm

    npm install @epegzz/sass-vars-loader --save-dev

    using yarn

    yarn add @epegzz/sass-vars-loader --dev

    Usage

    Look at the Example Webpack Config File to see how to use this loader in conjunction with style-loader and css-loader

    Option 1: Inline Sass vars in the webpack config

    // styles.css:
    
    .some-class {
      background: $greenFromWebpackConfig;
    }
    // webpack.config.js
    
    var path = require('path');
    
    module.exports = {
      entry: './src/index.js',
      module: {
        rules: [{
          test: /\.scss$/,
          use: [
            // Inserts all imported styles into the html document
            { loader: "style-loader" },
    
            // Translates CSS into CommonJS
            { loader: "css-loader" },
    
            // Compiles Sass to CSS
            { loader: "sass-loader", options: { includePaths: ["app/styles.scss"] } },
    
            // Reads Sass vars from files or inlined in the options property
            { loader: "@epegzz/sass-vars-loader", options: {
              syntax: 'scss',
              // Option 1) Specify vars here
              vars: {
                greenFromWebpackConfig: '#0f0'
              }
            }
          }]
        }]
      },
      output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
      }
    };

    Option 2: Load Sass vars from JSON file

    // config/sassVars.json
    
    {
      "purpleFromJSON": "purple"
    }
    // styles.css:
    
    .some-class {
      background: $purpleFromJSON;
    }
    // webpack.config.js
    
    var path = require('path');
    
    module.exports = {
      entry: './src/index.js',
      module: {
        rules: [{
          test: /\.scss$/,
          use: [
            // Inserts all imported styles into the html document
            { loader: "style-loader" },
    
            // Translates CSS into CommonJS
            { loader: "css-loader" },
    
            // Compiles Sass to CSS
            { loader: "sass-loader", options: { includePaths: ["app/styles.scss"] } },
    
            // Reads Sass vars from files or inlined in the options property
            { loader: "@epegzz/sass-vars-loader", options: {
              syntax: 'scss',
              files: [
                // Option 2) Load vars from JSON file
                path.resolve(__dirname, 'config/sassVars.json')
              ]
            }
          }]
        }]
      },
      output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
      }
    };

    Option 3: Load Sass vars from JavaScript file

    // config/sassVars.js
    
    module.exports = {
      blueFromJavaScript: 'blue'
    };
    // styles.css:
    
    .some-class {
      background: $blueFromJavaScript;
    }
    // webpack.config.js
    
    var path = require('path');
    
    module.exports = {
      entry: './src/index.js',
      module: {
        rules: [{
          test: /\.scss$/,
          use: [
            // Inserts all imported styles into the html document
            { loader: "style-loader" },
    
            // Translates CSS into CommonJS
            { loader: "css-loader" },
    
            // Compiles Sass to CSS
            { loader: "sass-loader", options: { includePaths: ["app/styles.scss"] } },
    
            // Reads Sass vars from files or inlined in the options property
            { loader: "@epegzz/sass-vars-loader", options: {
              syntax: 'scss',
              files: [
                // Option 3) Load vars from JavaScript file
                path.resolve(__dirname, 'config/sassVars.js')
              ]
            }
          }]
        }]
      },
      output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
      }
    };

    Pro Tip: Using objects as Sass vars!

    Use map_get in order to use objects as Sass vars:

    // config/sassVars.js
    
    module.exports = {
      lightTheme: {
        background: 'white',
        color: 'black'
      },
      darkTheme: {
        background: 'black',
        color: 'gray'
      }
    };
    // styles.css:
    
    $theme: $lightTheme;
    
    .some-class {
      background: map_get($theme, background);
      color: map_get($theme, color);
    }

    Recommend Projects

    • React photo React

      A declarative, efficient, and flexible JavaScript library for building user interfaces.

    • Vue.js photo Vue.js

      🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

    • Typescript photo Typescript

      TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

    • TensorFlow photo TensorFlow

      An Open Source Machine Learning Framework for Everyone

    • Django photo Django

      The Web framework for perfectionists with deadlines.

    • D3 photo D3

      Bring data to life with SVG, Canvas and HTML. 📊📈🎉

    Recommend Topics

    • javascript

      JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

    • web

      Some thing interesting about web. New door for the world.

    • server

      A server is a program made to process requests and deliver data to clients.

    • Machine learning

      Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

    • Game

      Some thing interesting about game, make everyone happy.

    Recommend Org

    • Facebook photo Facebook

      We are working to build community through open source technology. NB: members must have two-factor auth.

    • Microsoft photo Microsoft

      Open source projects and samples from Microsoft.

    • Google photo Google

      Google ❤️ Open Source for everyone.

    • D3 photo D3

      Data-Driven Documents codes.