[英]How to compile scss to css with node-sass
I have a master.scss with many imports from other.scss files.我有一个 master.scss,其中包含许多来自 other.scss 文件的导入。 If I change a *.scss file master.css is generated automatically.如果我更改 *.scss 文件,master.css 会自动生成。
I use only the NPM, no Gulp or Grunt.我只使用 NPM,没有 Gulp 或 Grunt。 This should remain so.这应该保持下去。
My current build script:我当前的构建脚本:
"scripts": {
"watch-sass": "sass --watch src/scss/master.scss:dist/css/master.css"
}
That's nice but takes a long time to compile!这很好,但需要很长时间才能编译!
Now I'm trying to use node-sass .现在我正在尝试使用node-sass 。 It should compile very fast.它应该编译得非常快。 Unfortunately, I do not understand it completely... node-sass is installed, via npm install node-sass
不幸的是,我不完全理解......通过npm install node-sass
sass
Where do I use the following (from docs)?我在哪里使用以下内容(来自文档)?
var sass = require('node-sass');
sass.render({
file: scss_filename,
[, options..]
}, function(err, result) { /*...*/ });
// OR
var result = sass.renderSync({
data: scss_content
[, options..]
});
This is not so in the package.json
, right?在package.json
中不是这样,对吧?
Here's a tutorial, what I've read: Using NPM as a Task Runner这是我读过的教程: Using NPM as a Task Runner
The script is good.剧本不错。 How can I use it?我该如何使用它?
"scripts": {
"sass": "node-sass sass/ -o build/css/"
}
This will compile all of the sass files (that don't start with an underscore) to the build/css/ directory.这会将所有 sass 文件(不以下划线开头)编译到 build/css/ 目录。
I hope for your help!我希望得到你的帮助!
Maybe this covers your question: How to compile or convert sass / scss to css with node-sass (no Ruby)?也许这涵盖了您的问题: How to compile or convert sass / scss to css with node-sass (no Ruby)?
If it's an option for you I would recommend using grunt, it will make things a lot simpler and faster.如果它适合您,我会推荐使用 grunt,它会让事情变得更简单、更快。 This grunt plugin is probably the best option: https://www.npmjs.com/package/grunt-contrib-sass这个 grunt 插件可能是最好的选择: https ://www.npmjs.com/package/grunt-contrib-sass
// UPDATE // 更新
I followed the tutorial you sent and it's very straightforward.我遵循了您发送的教程,它非常简单。 You create a file in your root folder called "package.json" containing the following:您在根文件夹中创建一个名为“package.json”的文件,其中包含以下内容:
{
"watches": {
"sass": "sass/**"
},
"scripts": {
"sass": "node-sass sass/ -o build/css/",
"dev": "rerun-script"
}
}
You then open the command line in the root folder and run the following:然后在根文件夹中打开命令行并运行以下命令:
npm install --save-dev node-sass
The above installs node-sass上面安装了node-sass
You then run:然后运行:
npm install --save-dev rerun-script
The above creates a watch task so you don't have to rerun node-sass everytime you make changes上面创建了一个 watch 任务,这样你就不必在每次进行更改时都重新运行 node-sass
And last you run最后你跑
npm run dev
Done!完毕!
If you want automatically compile files then you need to put the flag -w如果你想自动编译文件那么你需要把标志 -w
node-sass -w -r assets/src/scss/ -o assets/dist/css/
My package.json我的包.json
{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"build:js": "watchify assets/src/js/main_1.js -o 'exorcist assets/dist/js/main_1.js.map > assets/dist/js/main_1.js' -d -t [babelify --presets [latest]]",
"build:scss": "node-sass -w -r assets/src/scss/ -o assets/dist/css/",
"build": "npm run build:scss & npm run build:js"
},
"devDependencies": {
"babel-cli": "^6.0.0",
"babel-preset-latest": "^6.16.0",
"babelify": "^7.3.0",
"browserify": "^13.1.1",
"exorcist": "^0.4.0",
"node-sass": "^4.5.0",
"watchify": "^3.7.0"
},
"browserify": {
"transform": [
"babelify"
]
}
}
Actual version of package.json: https://gist.github.com/artamonovdev/5f24aaca504e4d1b299bba0413f0a57d package.json 的实际版本: https ://gist.github.com/artamonovdev/5f24aaca504e4d1b299bba0413f0a57d
The watch
mode in node-sass
doesn't run the first compilation. node-sass
中的watch
模式不会运行第一次编译。 It supposes that you have already ran node-sass
.它假定您已经运行了node-sass
。
Personally, I use something like this:就个人而言,我使用这样的东西:
{
"scripts": {
"sass": "node-sass -o /path/to/dist /path/to/src",
"sass:watch": "npm run sass && npm run sass -- --watch --recursive"
}
}
And you can use it like this: npm run sass:watch
你可以像这样使用它: npm run sass:watch
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.