Kim
Kim

Reputation: 275

How to replace sass variable values using grunt-sass-replace?

I want to replace few sass variable values inside a sass config file. For example, I want to replace the value of variable "$file_global" = "new";

I want to use "grunt-sass-replace" package to do the work, i tried alot but its giving me various errors.

My Project Directory Structure:

grep/
     /node_modules/
     package.json
     Gruntfile.js
     src/
         my-styles.scss

my-styles.scss Code:

$file_global: "old";

Gruntfile.js Code:

module.exports = function(grunt){
pkg: grunt.file.readJSON('package.json'),
grunt.initConfig({ 

'sass-replace': {

                  files: { // File Options
                            src: 'src/my-styles.scss',
                            dest: 'dest/my-styles.scss'
                  },
                  options: {
                    variables: [
                      {
                        name: 'file_global',
                        to: 'new'
                      }
                    ]
                  }
}

});

grunt.loadNpmTasks('grunt-sass-replace');
grunt.registerTask('default', ['sass-replace']);
};

package.json Code:

{
  "name": "grep",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "KJ",
  "license": "ISC",
  "devDependencies": {
    "grunt": "^1.0.4",
    "grunt-sass-replace": "^0.1.18",
    "npm-check-updates": "^3.1.9"
  }
}

I updated the "files" but its still giving me various errors. Below are the options that i tried and the errors generated.

First Try

// Option First : 
                  files: {
                           'dest/my-styles.scss': 'src/my-styles.scss'
                  },

                  ERROR : 
                          C:\wamp64\www\GREP>grunt
                          >> Tasks directory "C:\wamp64\www\GREP\node_modules\grunt-sass-replace\node_modules\grunt-string-replace\tasks" not found.
                          Running "sass-replace:files" (sass-replace) task
                          Warning: no files passed. Use --force to continue..
                          Aborted due to warnings.

Second Try:

// Option Second : 
files: [
        {
           src: 'src/my-styles.scss',
           dest: 'dest/my-styles.scss'
        }

],

ERROR : 
        C:\wamp64\www\GREP>grunt
        >> Tasks directory "C:\wamp64\www\GREP\node_modules\grunt-sass-replace\node_modules\grunt-string-replace\tasks" not found.
        Running "sass-replace:files" (sass-replace) task
        Warning: pattern.indexOf is not a function Use --force to continue.
        Aborted due to warnings.

Last Try:

// Option Third : 
files: {
           src: 'src/my-styles.scss',
           dest: 'dest/my-styles.scss'
        },


ERROR : 
        C:\wamp64\www\GREP>grunt
        >> Tasks directory "C:\wamp64\www\GREP\node_modules\grunt-sass-replace\node_modules\grunt-string-replace\tasks" not found.
        Running "sass-replace:files" (sass-replace) task
        >> [1] scss files found in [1] passed files.
        >> replacements resolved successfully.
        running string-replace task.
        Warning: Task "string-replace:sass" not found. Use --force to continue.
        Aborted due to warnings.

Anyone know how to solve this error, or any other grunt package which can do this kind of work.

Upvotes: 1

Views: 640

Answers (1)

ximewatch
ximewatch

Reputation: 73

This package was last updated 3 years ago, also it uses grunt ~0.4.5. I can't help you with this, However checkout "grunt-sass-replace-values" from https://www.npmjs.com/package/grunt-sass-replace-values. This package is updated a year ago and patched.

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

Check out following issue on Github: https://github.com/eliranmal/grunt-sass-replace/issues/1


Explanation :


Cause of errors :

  • You defined sass variable incorrectly. Variables should be defined as "$variable: value;" and not like "$variable = value;"

  • As of the Github issue with this package, you need to update the path to your "grunt-string-replace" dependency.


Solution :

Under your project root folder, Go to below directory:

node_modules/grunt-sass-replace/tasks

Once you're in the above directory, look for a file name "sass-replace.js"

Just open the file with any Text Editor, and Edit the path to dependency.

grunt.task.loadTasks(path.resolve(__dirname, '../node_modules/grunt-string-replace/tasks'));

In your case edit this like as below :

grunt.task.loadTasks(path.resolve(__dirname, '../../../node_modules/grunt-string-replace/tasks'));

I hope this solves your problem. If not use another package, or use older node and grunt(0.4.5) versions.

Upvotes: 1

Related Questions