在我安装节点后的 package.json 文件中,我想要一个命令可以使用一个命令自动编译我的任何 sass 文件?
In my package.json file after installing node, I want to have a command that can automatically compile my any of my sass files using one command?
因此,为了概述我想要的内容,我希望在我的脚本中只有一个命令。所以,我希望能够像这样将一个参数传递到我 运行ning 的代码中:
"scripts": {
"compile:'variable'": "node-sass sass/variable.scss css/variable.css -w" }
有没有一种方法可以在 npm 中执行类似的操作,以便我可以将参数传递到我的命令中 运行?在命令行中,我想以一种简单的方式进行编译。如果我想编译一个名为 "home" 的文件,我会说。所以,我希望能够在命令行中输入:
npm run compile:home
然后,这个,我想通过 package.json 和 运行ning node-sass 获取以下命令来编译它:
"node-sass scss/home.scss css/home.css -w"
如果您有任何问题,请随时发表评论。
不幸的是,npm-scripts
没有内置的方法来满足您的要求,运行 可以成功跨平台。
但是,满足您要求的几种方法如下(...您可能需要 解决方案 B):
解决方案 A:
如果您只有几个 .scss
文件要编译为 .css
,那么最简单的解决方案是为每个文件创建一个 npm-script
。例如:
"scripts": {
"compile:home": "node-sass sass/home.scss css/home.css -w",
"compile:quxx": "node-sass sass/quux.scss css/quxx.css -w"
}
这将允许您运行每个脚本使用您需要的命令语法,即
$ npm run compile:home
$ npm run compile:quxx
但是,这 可能 并不理想,因为我猜你有很多 .scss
文件 - 因此你的问题是关于在脚本中使用变量。
解决方案 B:
跨平台工作的解决方案(并使您能够通过 CLI 将文件名定义为参数)是编写 nodejs 实用程序脚本。然后可以通过 package.json
.
的 scripts
部分调用 nodejs 脚本
以下提供了实现此目的的必要步骤:
创建一个nodejs脚本如下:让我们将文件命名为sass-to-css.js
.
sass-到-css.js
const fs = require('fs');
const path = require('path');
const execSync = require('child_process').execSync;
// Error when no filename argument provided.
if (!process.argv[2]) {
console.log('\x1b[31mERR!\x1b[0m Trailing filename arg must be provided.');
process.exit(1);
}
const fileName = process.argv[2];
const srcPath = `${path.join('sass', fileName)}.scss`;
// Check source .scss file exists.
if (!fs.existsSync(srcPath)) {
console.log('\x1b[31mERR!\x1b[0m Path cannot be found: %s', srcPath);
process.exit(1);
}
// Path to node-sass executable in node_modules directory.
const executablePath = path.join('node_modules', '.bin', 'node-sass');
// Destimation path for resultant .css file.
const destPath = `${path.join('css', fileName)}.css`;
// The command to be invoked.
const cmd = `${executablePath} ${srcPath} ${destPath} -w`;
// Execute the command.
execSync(cmd, {
cwd: process.cwd()
});
将 sass-to-css.js
保存在项目目录的根目录中,与存储 package.json
的文件夹相同。例如:
.
├── ...
├── node_modules
│ └── ...
├── package.json
├── sass
│ ├── home.scss
│ └── ...
└── sass-to-css.js <--
然后在 package.json
的 scripts
部分添加以下脚本:
"scripts": {
"compile": "node sass-to-css"
}
要通过 CLI 调用脚本,请输入类似以下命令的内容:
$ npm run compile home
$ npm run compile foo
$ npm run compile bar
注意:提供的最后一个参数是要编译的 .scss
文件的文件名(不带文件扩展名)。即上例中的home
、foo
、bar
。 space 必须存在于 compile
部分和 filename/argument.
之间
运行 命令的语法与您要求的不完全相同,但很接近!
补充说明:
sass-to-css.js
的相关部分是:
- 它利用节点
process.argv
获取通过 CLI 输入的 filename/argument。
- 源 (
.scss
) 和目标 (.css
) 文件的路径是使用节点 path.join()
. 构造的
child_process.execSync()
用于执行适当的 node-sass
命令。
- 它检查是否提供了强制文件名参数,并使用
fs.existsSync(path)
检查 .scss
文件的结果文件路径是否存在。
sass-to-css.js
目前使用 Template literals(ES6 的一个特性)。如果您的 运行 使用不支持此语法的旧版本的 nodejs,请改用下面脚本的 ES5 版本:
ES5版本sass-to-css.js
var fs = require('fs');
var path = require('path');
var execSync = require('child_process').execSync;
// Error when no filename argument provided.
if (!process.argv[2]) {
console.log('\x1b[31mERR!\x1b[0m Trailing filename arg must be provided.');
process.exit(1);
}
var fileName = process.argv[2];
var srcPath = path.join('sass', fileName) + '.scss';
// Check source .scss file exists.
if (!fs.existsSync(srcPath)) {
console.log('\x1b[31mERR!\x1b[0m Path cannot be found: %s', srcPath);
process.exit(1);
}
// Path to node-sass executable in node_modules directory.
var executablePath = path.join('node_modules', '.bin', 'node-sass');
// Destimation path for resultant .css file.
var destPath = path.join('css', fileName) + '.css';
// The command to be invoked.
var cmd = executablePath + ' ' + srcPath + ' ' + destPath + ' -w';
// Execute the command.
execSync(cmd, {
cwd: process.cwd()
});
因此,为了概述我想要的内容,我希望在我的脚本中只有一个命令。所以,我希望能够像这样将一个参数传递到我 运行ning 的代码中:
"scripts": {
"compile:'variable'": "node-sass sass/variable.scss css/variable.css -w" }
有没有一种方法可以在 npm 中执行类似的操作,以便我可以将参数传递到我的命令中 运行?在命令行中,我想以一种简单的方式进行编译。如果我想编译一个名为 "home" 的文件,我会说。所以,我希望能够在命令行中输入:
npm run compile:home
然后,这个,我想通过 package.json 和 运行ning node-sass 获取以下命令来编译它:
"node-sass scss/home.scss css/home.css -w"
如果您有任何问题,请随时发表评论。
不幸的是,npm-scripts
没有内置的方法来满足您的要求,运行 可以成功跨平台。
但是,满足您要求的几种方法如下(...您可能需要 解决方案 B):
解决方案 A:
如果您只有几个 .scss
文件要编译为 .css
,那么最简单的解决方案是为每个文件创建一个 npm-script
。例如:
"scripts": {
"compile:home": "node-sass sass/home.scss css/home.css -w",
"compile:quxx": "node-sass sass/quux.scss css/quxx.css -w"
}
这将允许您运行每个脚本使用您需要的命令语法,即
$ npm run compile:home
$ npm run compile:quxx
但是,这 可能 并不理想,因为我猜你有很多 .scss
文件 - 因此你的问题是关于在脚本中使用变量。
解决方案 B:
跨平台工作的解决方案(并使您能够通过 CLI 将文件名定义为参数)是编写 nodejs 实用程序脚本。然后可以通过 package.json
.
scripts
部分调用 nodejs 脚本
以下提供了实现此目的的必要步骤:
创建一个nodejs脚本如下:让我们将文件命名为
sass-to-css.js
.sass-到-css.js
const fs = require('fs'); const path = require('path'); const execSync = require('child_process').execSync; // Error when no filename argument provided. if (!process.argv[2]) { console.log('\x1b[31mERR!\x1b[0m Trailing filename arg must be provided.'); process.exit(1); } const fileName = process.argv[2]; const srcPath = `${path.join('sass', fileName)}.scss`; // Check source .scss file exists. if (!fs.existsSync(srcPath)) { console.log('\x1b[31mERR!\x1b[0m Path cannot be found: %s', srcPath); process.exit(1); } // Path to node-sass executable in node_modules directory. const executablePath = path.join('node_modules', '.bin', 'node-sass'); // Destimation path for resultant .css file. const destPath = `${path.join('css', fileName)}.css`; // The command to be invoked. const cmd = `${executablePath} ${srcPath} ${destPath} -w`; // Execute the command. execSync(cmd, { cwd: process.cwd() });
将
sass-to-css.js
保存在项目目录的根目录中,与存储package.json
的文件夹相同。例如:. ├── ... ├── node_modules │ └── ... ├── package.json ├── sass │ ├── home.scss │ └── ... └── sass-to-css.js <--
然后在
package.json
的scripts
部分添加以下脚本:"scripts": { "compile": "node sass-to-css" }
要通过 CLI 调用脚本,请输入类似以下命令的内容:
$ npm run compile home $ npm run compile foo $ npm run compile bar
注意:提供的最后一个参数是要编译的
之间.scss
文件的文件名(不带文件扩展名)。即上例中的home
、foo
、bar
。 space 必须存在于compile
部分和 filename/argument.运行 命令的语法与您要求的不完全相同,但很接近!
补充说明:
sass-to-css.js
的相关部分是:- 它利用节点
process.argv
获取通过 CLI 输入的 filename/argument。 - 源 (
.scss
) 和目标 (.css
) 文件的路径是使用节点path.join()
. 构造的
child_process.execSync()
用于执行适当的node-sass
命令。- 它检查是否提供了强制文件名参数,并使用
fs.existsSync(path)
检查.scss
文件的结果文件路径是否存在。
- 它利用节点
sass-to-css.js
目前使用 Template literals(ES6 的一个特性)。如果您的 运行 使用不支持此语法的旧版本的 nodejs,请改用下面脚本的 ES5 版本:ES5版本sass-to-css.js
var fs = require('fs'); var path = require('path'); var execSync = require('child_process').execSync; // Error when no filename argument provided. if (!process.argv[2]) { console.log('\x1b[31mERR!\x1b[0m Trailing filename arg must be provided.'); process.exit(1); } var fileName = process.argv[2]; var srcPath = path.join('sass', fileName) + '.scss'; // Check source .scss file exists. if (!fs.existsSync(srcPath)) { console.log('\x1b[31mERR!\x1b[0m Path cannot be found: %s', srcPath); process.exit(1); } // Path to node-sass executable in node_modules directory. var executablePath = path.join('node_modules', '.bin', 'node-sass'); // Destimation path for resultant .css file. var destPath = path.join('css', fileName) + '.css'; // The command to be invoked. var cmd = executablePath + ' ' + srcPath + ' ' + destPath + ' -w'; // Execute the command. execSync(cmd, { cwd: process.cwd() });