@epegzz/sass-vars-loader

中文文档
6.1.0发布于 2021/04/05MITCommonJS近 30 天下载 2.8万

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

原始使用文档
文档目录7 节
  1. Import Sass vars from Webpack config or from JS/JSON files
  2. Install
  3. Usage
  4. Option 1: Inline Sass vars in the webpack config
  5. Option 2: Load Sass vars from JSON file
  6. Option 3: Load Sass vars from JavaScript file
  7. Pro Tip: Using objects as Sass vars!

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);
    }
    

    版本与发布记录

    @epegzz/sass-vars-loader 共发布 37 个版本,首次发布于 2016/08/22;过去 12 个月发布了 0 个版本。

    版本发布日期距今
    6.1.0latest2021/04/055 年前
    6.0.02020/01/126 年前
    5.1.02019/12/126 年前
    5.0.02019/07/217 年前
    4.4.02019/07/217 年前
    4.3.12019/03/087 年前

    关于 @epegzz/sass-vars-loader 的常见问题

    @epegzz/sass-vars-loader 是什么?

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

    如何安装 @epegzz/sass-vars-loader?

    在项目目录运行 npm install @epegzz/sass-vars-loader,或使用 pnpm add / yarn add / bun add。

    @epegzz/sass-vars-loader 支持 TypeScript 吗?

    该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/epegzz__sass-vars-loader 是否存在。

    @epegzz/sass-vars-loader 支持 ESM 还是 CommonJS?

    以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

    @epegzz/sass-vars-loader 需要什么版本的 Node.js?

    package.json 的 engines 字段要求 Node.js >=8。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

    @epegzz/sass-vars-loader 可以商用吗?

    @epegzz/sass-vars-loader 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

    @epegzz/sass-vars-loader 的下载量有多少?

    根据 npm 官方下载统计,@epegzz/sass-vars-loader 在 2026/10/14 – 2026/11/12 这 30 天内被下载 28,412 次,日均约 947 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

    @epegzz/sass-vars-loader 还在维护吗?

    最新版本 6.1.0 发布于 2021/04/05(5 年前);至今共发布 37 个版本,其中过去 12 个月发布了 0 个;首次发布于 2016/08/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。