如何使用Grunt-Sass-Replace替换SASS变量值



我想在sass配置文件中替换几个SASS变量值。例如,我想替换变量" $ file_global" =" new";

的值

我想使用" grunt-sass-replace"软件包来完成这项工作,我尝试了很多,但它给了我各种错误。

我的项目目录结构:

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

my-styles.scss 代码:

$file_global: "old";

gruntfile.js 代码:

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代码:

{
  "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"
  }
}

我更新了"文件" ,但它仍然给我带来了各种错误。以下是我尝试过的选项和产生的错误。

首次尝试

// Option First : 
                  files: {
                           'dest/my-styles.scss': 'src/my-styles.scss'
                  },
                  ERROR : 
                          C:wamp64wwwGREP>grunt
                          >> Tasks directory "C:wamp64wwwGREPnode_modulesgrunt-sass-replacenode_modulesgrunt-string-replacetasks" not found.
                          Running "sass-replace:files" (sass-replace) task
                          Warning: no files passed. Use --force to continue..
                          Aborted due to warnings.

第二次尝试:

// Option Second : 
files: [
        {
           src: 'src/my-styles.scss',
           dest: 'dest/my-styles.scss'
        }
],
ERROR : 
        C:wamp64wwwGREP>grunt
        >> Tasks directory "C:wamp64wwwGREPnode_modulesgrunt-sass-replacenode_modulesgrunt-string-replacetasks" not found.
        Running "sass-replace:files" (sass-replace) task
        Warning: pattern.indexOf is not a function Use --force to continue.
        Aborted due to warnings.

上一次尝试:

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

ERROR : 
        C:wamp64wwwGREP>grunt
        >> Tasks directory "C:wamp64wwwGREPnode_modulesgrunt-sass-replacenode_modulesgrunt-string-replacetasks" 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.

任何人都知道如何解决此错误或任何其他可以从事此类工作的咕unt声软件包。

此软件包是3年前的最后更新,也使用grunt〜0.4.5。我无法为您提供帮助,但是结帐了https://www.npmjs.com/package/grunt-sass-replace-values。该软件包在一年前更新并修补。

npm安装grunt-sass-replace-values -save-dev

在GitHub上查看以下问题:https://github.com/eliranmal/grunt-sass-replace/issues/1


说明:


错误原因:

  • 您错误地定义了SASS变量。变量应定义为" $ variable:valuial;" ,而不是" $ variable = value;"

  • 与此软件包的GitHub问题一样,您需要更新通往" Grunt-runt-string-Replace"的路径" dependenty。


解决方案:

在您的项目根文件夹下,转到目录下方:

node_modules/grunt-sass-replace/tasks

您进入上述目录后,查找文件名" sass-replace.js"

只需使用任何文本编辑器打开文件,然后编辑依赖的路径。

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

在您的情况下,按照以下方式进行编辑:

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

我希望这能解决您的问题。如果不使用其他软件包,或使用较旧的节点和grunt(0.4.5(版本。