节点包 - 是否可以在 package.json 中插入变量?
Node Package - Is it possible to interpolate a variable in package.json?
我有一个使用 node-sass
.
编译 Sass 的静态站点
目前我正在使用 Grunt 来观看文件,但我觉得这太过分了,因为我可以使用他们内置的 CLI。
所以我在 package.json 中添加了这个:
// package.json
...
"scripts": {
"sass": "node-sass -w input/dir -o output-dir/"
}
问题是,我需要在 --include-path
中 require
一个 Sass 框架模块(全局安装)。我可以在 Gruntfile 中做到这一点:
// Gruntfile.js
sass: {
options: {
includePaths: require("the-framework").includePaths()
},
...
},
所以我首先想到的是像这样插入字符串:
// package.json
...
"scripts": {
"sass": "node-sass -w input/dir -o output-dir/ --include-path " + require("the-framework").includePaths()
}
正如预期的那样,它不起作用。脚本运行良好,但插值变量被忽略。
任何解决方案或替代方案?如果可能的话,我宁愿不要创建额外的文件只是为了存储变量。
谢谢
你可以这样:
“scripts”: {
“sass”: “node-sass --include-path scss scss/main.scss public/css/main.css”
},
我不知道这样做是否正确,但我可以解释我将如何解决这个任务。
您不能在 package.json
中插入变量,因为它必须有效 json。你可以在这里写 bash 命令。
1) 您可以编写需要结果的节点命令。如果 includePaths()
不是 return 字符串,你应该小心。
Node options:
-e, --eval script evaluate script
-p, --print evaluate script and print result
所以它会像
node -e "console.log(require('the-framework').includePaths())"
或 --print
的更短版本
node -p "require('the-framework').includePaths()"
2) 将上一个命令的内联输出到 sass 脚本中。注意右转义。
{
"scripts": {
"sass": "node-sass -w input/dir -o output-dir/ --include-path $(node -p \"require('the-framework').includePaths()\")"
}
}
有关执行 bash 命令的更多信息,您可以找到 here。
P.S。 Windows 版本不同
{
"scripts": {
"sass": "FOR /f \"delims=\" %v IN ('node -p \"require('edje').includePaths()[0]\"') DO node-sass -w assets/sass -o assets/css --include-path \"%v\""
}
}
您可以找到更多信息 here。
使用 npm 脚本可以通过多种方式解决此问题。
通过查看您的具体需求,我想到的第一个是,npm 在调用脚本时接受额外的参数。例如:
npm run sass -- path/to/the-framework
这将解析为:
node-sass -w input/dir -o output-dir/ --include-path path/to/the-framework
另一种方法是将代码移动到 .js
可执行文件 (Node) 中,我们称之为 watch-sass.js
.
因此您的脚本将如下所示:
"sass": "node watch-sass.js"
你会 运行:
npm run sass
这样您就可以更自由地在文件中做任何您想做的事情。
可能性真的是无限的,如果你愿意,你可以利用 bash 脚本的力量而不是 JS,它们都可以 read/write 环境变量。但你不一定需要它们。例如,您可以只使用一个 shell 脚本为您编写一个 package.json(或 Python,或 Ruby...),所有变量都已经就位。但最后我认为这只是一个品味问题,使用你觉得更简单或更舒服的东西。
我有一个使用 node-sass
.
目前我正在使用 Grunt 来观看文件,但我觉得这太过分了,因为我可以使用他们内置的 CLI。
所以我在 package.json 中添加了这个:
// package.json
...
"scripts": {
"sass": "node-sass -w input/dir -o output-dir/"
}
问题是,我需要在 --include-path
中 require
一个 Sass 框架模块(全局安装)。我可以在 Gruntfile 中做到这一点:
// Gruntfile.js
sass: {
options: {
includePaths: require("the-framework").includePaths()
},
...
},
所以我首先想到的是像这样插入字符串:
// package.json
...
"scripts": {
"sass": "node-sass -w input/dir -o output-dir/ --include-path " + require("the-framework").includePaths()
}
正如预期的那样,它不起作用。脚本运行良好,但插值变量被忽略。
任何解决方案或替代方案?如果可能的话,我宁愿不要创建额外的文件只是为了存储变量。
谢谢
你可以这样:
“scripts”: {
“sass”: “node-sass --include-path scss scss/main.scss public/css/main.css”
},
我不知道这样做是否正确,但我可以解释我将如何解决这个任务。
您不能在 package.json
中插入变量,因为它必须有效 json。你可以在这里写 bash 命令。
1) 您可以编写需要结果的节点命令。如果 includePaths()
不是 return 字符串,你应该小心。
Node options:
-e, --eval script evaluate script
-p, --print evaluate script and print result
所以它会像
node -e "console.log(require('the-framework').includePaths())"
或 --print
node -p "require('the-framework').includePaths()"
2) 将上一个命令的内联输出到 sass 脚本中。注意右转义。
{
"scripts": {
"sass": "node-sass -w input/dir -o output-dir/ --include-path $(node -p \"require('the-framework').includePaths()\")"
}
}
有关执行 bash 命令的更多信息,您可以找到 here。
P.S。 Windows 版本不同
{
"scripts": {
"sass": "FOR /f \"delims=\" %v IN ('node -p \"require('edje').includePaths()[0]\"') DO node-sass -w assets/sass -o assets/css --include-path \"%v\""
}
}
您可以找到更多信息 here。
使用 npm 脚本可以通过多种方式解决此问题。
通过查看您的具体需求,我想到的第一个是,npm 在调用脚本时接受额外的参数。例如:
npm run sass -- path/to/the-framework
这将解析为:
node-sass -w input/dir -o output-dir/ --include-path path/to/the-framework
另一种方法是将代码移动到 .js
可执行文件 (Node) 中,我们称之为 watch-sass.js
.
因此您的脚本将如下所示:
"sass": "node watch-sass.js"
你会 运行:
npm run sass
这样您就可以更自由地在文件中做任何您想做的事情。
可能性真的是无限的,如果你愿意,你可以利用 bash 脚本的力量而不是 JS,它们都可以 read/write 环境变量。但你不一定需要它们。例如,您可以只使用一个 shell 脚本为您编写一个 package.json(或 Python,或 Ruby...),所有变量都已经就位。但最后我认为这只是一个品味问题,使用你觉得更简单或更舒服的东西。