【问题标题】:How to replace sass variable values using grunt-sass-replace?如何使用 grunt-sass-replace 替换 sass 变量值?
【发布时间】:2019-10-05 22:18:31
【问题描述】:

我想替换 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:\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.

第二次尝试:

// 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.

最后一次尝试:

// 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.

任何人都知道如何解决这个错误,或者任何其他可以做这种工作的 grunt 包。

【问题讨论】:

    标签: node.js sass gruntjs node-modules


    【解决方案1】:

    这个包最后一次更新是在 3 年前,它使用 grunt ~0.4.5。我无法为您提供帮助,但是请从 https://www.npmjs.com/package/grunt-sass-replace-values 签出“grunt-sass-replace-values”。这个包在一年前更新并打了补丁。

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

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


    解释:


    错误原因:

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

    • 由于此软件包的 Github 问题,您需要更新 "grunt-string-replace" 依赖项的路径。


    解决方案:

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

    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) 版本。

    【讨论】:

    • 你能进一步解释一下吗?
    • 添加解决方案。
    猜你喜欢
    • 2014-12-31
    • 2018-11-22
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    相关资源
    最近更新 更多