我想替换 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"
}
}
我更新了"file",但它仍然给我带来各种错误。 以下是我尝试过的选项以及生成的错误。
第一次尝试
// 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 包。
最佳答案
这个包最后一次更新是在 3 年前,它也使用 grunt ~0.4.5。我无法帮助你,但是请从 https://www.npmjs.com/package/grunt-sass-replace-values 中查看“grunt-sass-replace-values” 。该软件包在一年前更新并修补过。
npm 安装 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'));
我希望这能解决您的问题。如果不使用其他包,或者使用旧的 Node 和 grunt(0.4.5) 版本。
关于node.js - 如何使用 grunt-sass-replace 替换 sass 变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56205785/