Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@epegzz/sass-vars-loader

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@epegzz/sass-vars-loader

A SASS vars loader for Webpack. Load global SASS vars from JS/JSON files or from Webpack config.

  • 1.2.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.1K
decreased by-62.41%
Maintainers
1
Weekly downloads
 
Created
Source

A SASS vars loader for Webpack

Reads sass vars from Javascript files, from JSON files or from the Webpack config.

Install

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

Usage

The following files can all be found in the example directory, which contains a fully functional example of how to use the sass-vars-loader.

The example projects uses sass-vars-loader to inject vars into sass files. In this case app/styles.scss:

body {

  // Option 1) Read from webpack config
  font-size: $bodyFontSize;

  // Nesting is also possible!
  border: map_get($borders, heavy);

  // Option 2) Read from JSON or Javascript file 
  color: $blueColor; // from colors.json
  background-color: $grayBackgroundColor; // from background.js
}

Take a look at the sass-vars-loader section in webpack.config.js to see how they were loaded:

var path = require('path');

module.exports = {
  entry: './app/index.js',
  module: {
    rules: [{
      test: /\.scss$/,
      use: [{
        loader: "style-loader" // creates style nodes from JS strings
      }, {
        loader: "css-loader" // translates CSS into CommonJS
      }, {
        loader: "sass-loader", // compiles Sass to CSS
        options: {
          includePaths: ["app/styles.scss"],
        },
      }, {
        loader: "@epegzz/sass-vars-loader", // read Sass vars from file or options
        options: {
          // Option 1) Specify vars here
          vars: {
            bodyFontSize: '21px',

            // Nesting is also possible (use map_get to read them in scss)!
            borders: {
              heavy: '5px solid black',
              thin: '1px solid gray',
            },
          },
          // Option 2) Load vars from JSON or Javascript file
          files: [
            path.resolve(__dirname, 'app/colors.json'),
            path.resolve(__dirname, 'app/backgrounds.js'),
          ],
        },
      }],
    }],
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
}

with app/colors.json

{
  "blueColor": "blue",
  "redColor": "red"
}

and app/backgrounds.js

module.exports = {
  grayBackgroundColor: 'gray',
  whiteBackgroundColor: 'white',
};

Usage with Extract Text Plugin

With the Extract Text Plugin the webpack.config.js changes slightly:

const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: './app/index.js',
  module: {
    rules: [{
      test: /\.scss$/,
      use: ExtractTextPlugin.extract({
        fallback: "style-loader",
        use: [{
          loader: "css-loader" // translates CSS into CommonJS
        }, {
          loader: "sass-loader", // compiles Sass to CSS
          options: {
            includePaths: ["app/styles.scss"],
          },
        }, {
          loader: "@epegzz/sass-vars-loader", // read Sass vars from file or options
          options: {
            // Option 1) Specify vars here
            vars: {
              bodyFontSize: '21px',
    
              // Nesting is also possible (use map_get to read them in scss)!
              borders: {
                heavy: '5px solid black',
                thin: '1px solid gray',
              },
            },
            // Option 2) Load vars from JSON or Javascript file
            files: [
              path.resolve(__dirname, 'app/colors.json'),
              path.resolve(__dirname, 'app/backgrounds.js'),
            ],
          },
        }],
      })
    }],
  },
  plugins: [
    new ExtractTextPlugin("styles.css"),
  ],
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
}

Acknowledgments

SASS var generator shamelessly copied from Kasu/jsonToSassVars.js

Keywords

FAQs

Package last updated on 13 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc