npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@marknotton/inject-scss

v1.1.5

Published

Inject Javascript variables into your SASS/SCSS during the Gulp task within pipes.

Downloads

5

Readme

Inject SCSS

Made For NPM Made For Gulp

Inject Javascript variables and imports Globs into your SASS/SCSS during the Gulp task within pipes.

Installation

npm i @marknotton/inject-scss --save-dev
const injectScss = require('@marknotton/inject-scss')

Basically this uses the same principle as Postilabs Inject SCSS project, only this variation works within Gulp pipes. So you can inject variables or imports directly before your SCSS compilation.

You could use this to inject asset paths that differ between development and production environments for example. Or keep unit sizes or other variables consistent between Javascript and CSS. Or maybe you just need to define a special custom property bespoke to your project. You can also refer to Globs when importing files, which means you can eliminate the need to import all your modular css files at the start of your scss and have them automatically inject everything.

Injecting Variables

Pass an associative array where the keys are the css properties and the values are the css values.

gulp.task('sass', () => {

  let settings = {
	  variables = {
	   'images':'"../assets/images/"',
	   'max-height':'100vh',
	   'browser-version':11
	  }
  }

  return gulp.src('*/**.scss')
  .pipe(injectScss(settings))
  .pipe(gulpsass({outputStyle: 'compressed'}))
  .pipe(gulp.dest("cssfile.css"))
});

SCSS input

:root { --browser-version : #{$browser-version}; }
body {
  background-image:url(#{$images} + 'wallpaper.jpg');
  max-height:#{$max-height};
}

CSS output

:root { --browser-version : 11; }
body {
  background-image:url(../assets/images/wallpaper.jpg);
  max-height:100vh;
}

Maps

You can also pass in nested objects, which will resolve to a Sass Map.


let settings = {
  variables = {
    'images':'"../assets/images/"',
    'max-height':'100vh',
    'browser-version':11,
    'themes' : {
      'primary' : '#FFF8DC',
      'secondary' : '#EFF0F1'
    }
  }
}

Virtual SCSS output

Maps won't actually be rendered anywhere in your scss files. It's all done dynamically during a Gulp task. However, this is essentially what a map might function like during the process:

$themes : (
  primary : '#FFF8DC',
  secondary : '#EFF0F1'
);

Injecting Imports

Pass an array of strings (as-apposed to an object like above) to render each string as an css @import. The example below will grab all scss files that are prefixed with an underscore. Paths that have no Glob syntax (like ! or *) will be added to the import array regardless of wether the file exists. This is intended to give you better control of files that aren't matched in the glob. Paths starting with a hat character (^) will be rendered at the end of your file.

Passing 'true' will log out the debug of the imports and variables. Showing you what's actually being included.

gulp.task('sass', () => {

  let setttings = {
    imports = {
      'vendor/marknotton/doggistyle/dist/_doggistyle.scss',
      'settings',
      '^src/sass/**/(_)*.scss'
    }
  }

  return gulp.src('*/**.scss')
  .pipe(injectScss(setttings))
  .pipe(gulpsass({outputStyle: 'compressed'}))
  .pipe(gulp.dest("cssfile.css"))
});

Virtual SCSS output

Imports won't actually be rendered anywhere in your scss files. It's all done dynamically during a Gulp task. However, this is essentially what the imports might function like during the process. Notice how the paths are relative to the sass file, and not the absolute directory tree.

@import 'vendor/marknotton/doggistyle/dist/_doggistyle',
'settings';

/* All your other CSS */

@import 'pages/home',
'pages/news',
'components/gallery/images';

Injecting both Variables and Imports

To use both injection methods, you can simply include an array for imports and an object for variables in any order.

.pipe(injectScss({ variables : {...}, imports : {...} ))

Debug

Passing a true will render out some additional data to help you see what's actually being injected

.pipe(injectScss({ variables : {...}, imports : {...} ), true)

Special Files

The settings object should include a 'variables' and 'imports' key to inject their respective options. These are global for all files that get rendered out. But you can also merge in a special set of options bespoke to a particular file. If the sass file in the stream matches the nested key name, then the additional variables/imports will be merged into the global variables/imports too.

.pipe(injectScss({
  variables : {...},
  imports : {...},
  emailer : { variables : {
    'max-width' : '600px'
  }}
 })