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

grunt-sass-replace

v0.1.18

Published

replaces sass values with grunt

Downloads

9

Readme

grunt-sass-replace

replaces sass values with grunt

NPM

Getting Started

This plugin requires Grunt ~0.4.5

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-sass-replace --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-sass-replace');

The 'sass-replace' task

Overview

In your project's Gruntfile, add a section named sass-replace to the data object passed into grunt.initConfig().

grunt.initConfig({
  'sass-replace': {
    files: [
      {
        src: 'my-styles.scss',
        dest: 'my-new-styles.scss'
      }
    ],
    options: {
      variables: [
        {
          name: 'my-var',
          from: '10px',
          to: '3.333em'
        }
      ],
      imports: [
        {
          from: 'http://foo.wat.com',
          to: 'http://bar.wat.com'
        }
      ]
    }
  },
});

Options

options.variables

Type: Array<Object>
Default value: none
Mandatory: either this or options.imports must be set

A collection of VariableInstructions to describe replacements of sass variable values.

VariableInstruction

Type: Object
Default value: none
Mandatory: at least one VariableInstruction must be available on the options.variables collection

An object representing a single instruction in the options.variables collection.
The to field is mandatory, and either from, name or both can be used to filter the lookup.

When both name and from are used, they both apply as filters. As name can be passed as a string or as a regular expression, it presents two common use cases:

  • passing both fields as strings can be used to replace values in duplicate variable declarations, e.g. a variable overriding a variable with the same name in another file.
  • passing a string to from and a regular expression to name can be used to achieve finer filtering, matching variables which have a certain value, by several names.
VariableInstruction.name

Type: String|RegExp
Default value: /\S+/ (match at least one non-whitespace character)
Mandatory: either this or the from field must be set

The variable name for lookup, without the leading $.

When a string is passed, the literal value is searched, e.g. my-var will match $my-var: "foo".

A regular expression can be passed in its literal form or as a RegExp instance.
e.g. /my[-_]?[Vv]ar/ or new RegExp('my[-_]?[Vv]ar') will both match $my-var: "foo", $my_var: "foo" and $myVar: "foo".

When using a RegExp instance, only its source is used for the lookup (flags are ignored).

VariableInstruction.from

Type: String|Number|Boolean
Default value: none
Mandatory: either this or the name field must be set

The variable's current value for lookup. Note that !default statements are not captured for replacement, and are kept as is (post an issue if you want to be able to alter !default statements as well).

See note on using double quotes when replacing string values.

VariableInstruction.to

Type: String|Number|Boolean
Default value: none
Mandatory: yes

A new value for the matched variable.

See note on using double quotes when replacing string values.

Note on replacing string values

When replacing variable values of type string, e.g. $my-var: "foo"$my-var: "bar", the variable value's surrounding double quotes may be omitted from the from/to field's value, as they will be captured and passed on to the replacement.
However, it is advised to prefer the explicit notation and always surround string values with double quotes, for better readability and to avoid confusion or unexpected behavior.

:+1:

  {
    from: '"foo"',
    to: '"bar"'
  }
  ...

:-1:

  {
    from: 'foo',
    to: 'bar'
  }
  ...

options.imports

Type: Array<Object>
Default value: none
Mandatory: either this or options.variables must be set

A collection of ImportInstructions to describe replacements of @import paths.

ImportInstruction

Type: Object
Default value: none
Mandatory: at least one ImportInstruction must be available on the options.imports collection

An object representing a single instruction in the options.imports collection.
Both from and to fields are mandatory.

ImportInstruction.from

Type: String
Default value: none
Mandatory: yes

The import path(s) current value for lookup. Captures only the path contents, i.e. everything between the surrounding quotes, or inside a url().

Capturing of everything after the @import (including quotes or url()s) is currently not supported, post an issue if you find it useful. Also, passing regular expressions was not tested, but probably works.

ImportInstruction.to

Type: String
Default value: none
Mandatory: yes

A new value for the matched import path(s).

Usage Examples

Replacing variables by current value

This example also shows the usage of various types in variable values.

grunt.initConfig({
  'sass-replace': {
    files: {
     'dest/my-styles.scss': 'src/my-styles.scss'
    },
    options: {
      variables: [
        {
          from: '"foo"',
          to: '"bar"'
        },
        {
          from: 10,
          to: '10%'
        },
        {
          from: 3.333,
          to: 6.666
        },
        {
          from: '10px',
          to: '20em'
        }
      ]
    }
  }
});

Replacing variables by name

grunt.initConfig({
  'sass-replace': {
    files: {
      'dest/my-styles.scss': 'src/my-styles.scss'
    },
    options: {
      variables: [
        {
          name: 'my-var',
          to: 'bar'
        }
      ]
    }
  }
});

Replacing variables by name and current value

grunt.initConfig({
  'sass-replace': {
    files: {
      'dest/my-styles.scss': 'src/my-styles.scss'
    },
    options: {
      variables: [
        {
          name: 'my-var',
          from: '"foo"',
          to: '"bar"'
        }
      ]
    }
  }
});

Replacing variables using regular expressions

grunt.initConfig({
  'sass-replace': {
    files: {
      'dest/my-styles.scss': 'src/my-styles.scss'
    },
    options: {
      variables: [
        {
          name: new RegExp('my[-_]?[Vv]ar'),
          from: '"foo"',
          to: 1000000000000
        },
        {
          name: /my[-_]?[Nn]um[-_]?[Vv]ar/,
          to: -1
        }
      ]
    }
  }
});

Replacing imports

grunt.initConfig({
  'sass-replace': {
    files: {
      'dest/my-styles.scss': 'src/my-styles.scss'
    },
    options: {
      imports: [
        {
          from: 'foo',
          to: 'bar'
        },
        {
          from: 'foo.css',
          to: 'bar.css'
        },
        {
          from: 'foo.scss',
          to: 'bar.scss'
        },
        {
          from: 'http://wat.com/foo',
          to: 'http://wat.com/bar'
        },
        {
          from: 'http://wat.tha.fuck.com/foo',
          to: 'http://wat.tha.fuck.com/bar'
        },
        {
          from: 'http://wat.com/foo?family=#{$family}',
          to: 'http://wat.com/bar?family=#{$family}'
        },
        {
          from: 'foo-foo',
          to: 'bar-bar'
        },
        {
          from: 'foo-foo-foo',
          to: 'bar-bar-bar'
        }
      ]
    }
  }
});

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality, lint and test your code.

To allow testing of negative flows, the tests are run via npm, which is running the main task with the --force flag. To run the tests, run:

npm run test

Release History

See the changelog.

License

Copyright (c) 2016 Eliran Malka. Licensed under the WTFPL license.