简体   繁体   English

VS Code SCSS 自动编译成 CSS

[英]VS Code SCSS auto compiling to CSS

I am total beginner in programming and just started to learn HTML/CSS.我是编程的初学者,刚刚开始学习 HTML/CSS。 For coding I started to use VS Code.对于编码,我开始使用 VS Code。 And I really like it.我真的很喜欢它。

Only problem so far, what I got, is auto compiling of SCSS to CSS.到目前为止,我得到的唯一问题是将 SCSS 自动编译为 CSS。 I have searched and read many solutions, and the best what I found was with ruby + sass + code in VS Code terminal sass --watch .我搜索并阅读了许多解决方案,我发现最好的方法是在 VS Code 终端sass --watch .使用 ruby​​ + sass + 代码sass --watch . It is watching my project and creating new CSS when new SCSS is created.它正在监视我的项目并在创建新的 SCSS 时创建新的 CSS。 And it is watching for changes in SCSS.它正在关注 SCSS 的变化。 But problem is that this code must be entered each time I am starting VS Code.但问题是每次启动 VS Code 时都必须输入此代码。

Tried also solution with Gulp file and package.json, but also could not make it start automatically.也尝试过使用 Gulp 文件和 package.json 的解决方案,但也无法使其自动启动。 And it has to be made for each project separately.并且必须为每个项目单独制作。 I tried also Atom, and it has sass-autocompile package , and it works perfectly.我也试过 Atom,它有sass-autocompile 包,它工作得很好。 So, simplest way for me would be to use Atom and forget.所以,对我来说最简单的方法是使用 Atom 并忘记。 But I would like to use VS Code though.但是我想使用 VS Code。

So, generally question is if there would be possibility to create extension for VS Code to automate SCSS compilation to CSS (similar to Atom's package, which would be the best IMO).因此,一般的问题是是否有可能为 VS Code 创建扩展以自动将 SCSS 编译为 CSS(类似于 Atom 的包,这将是最好的 IMO)。 Or maybe somebody could explain me other way how to solve this problem.或者也许有人可以用其他方式向我解释如何解决这个问题。

You will need two things: 您将需要两件事:

  • tasks.json file task.json文件
  • Blade Runner extension for VS CODE VS CODE的Blade Runner扩展

Start by creating .vscode folder in your project. 首先在项目中创建.vscode文件夹。

Then in it create tasks.json file with the following content: 然后在其中创建具有以下内容的tasks.json文件:

{
    "version": "0.1.0",
    "command": "sass",
    "isShellCommand": true,
    "args": ["--watch", "."],
    "showOutput": "always"
}

Now, after opening the project you can run the task by clicking Ctrl+Shift+B . 现在,在打开项目后,您可以通过单击Ctrl + Shift + B来运行任务。

To automate the process use Blade Runner extension - https://marketplace.visualstudio.com/items?itemName=yukidoi.blade-runner 要自动执行该过程,请使用Blade Runner扩展程序-https://marketplace.visualstudio.com/items ? itemName = yukidoi.blade-runner

Blade Runner will run the task automatically after opening the project :) 打开项目后,Blade Runner将自动运行任务:)

A solution without additional extensions无需额外扩展的解决方案

With sass带着骚

Assuming you have sass installed globally with for instance:假设您在全球范围内安装了 sass,例如:

npm install -g sass

Open the folder and create a task.json file under .vscode containing打开文件夹并在 .vscode 下创建一个 task.json 文件,其中包含

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
    {
        "label": "Watch Sass",
        "type": "shell",
        "command": "sass --watch src/style.sass styles/style.css --style=compressed",
        "problemMatcher": [],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "runOptions": {
            "runOn": "folderOpen"
        }
    }]
}

With node-sass使用 node-sass

Replace sass with node-sass in the above.将上面的 sass 替换为 node-sass 。

In both cases make sure the source/destination filename, location and extension are correct (in my case src/style.scss and style/style.css)在这两种情况下,请确保源/目标文件名、位置扩展名正确(在我的情况下为 src/style.scss 和 style/style.css)

Or copy the section in your .vscode-worskpace to avoid clutter.或者复制 .vscode-workspace 中的部分以避免混乱。 Make sure to change the sass source and destination files to your personal needs.确保根据您的个人需要更改 sass 源文件和目标文件。

Now allow automatic run tasks by现在允许自动运行任务

  1. Ctrl+Alt+P Ctrl+Alt+P
  2. select Manage automatic Tasks and选择管理自动任务
  3. select Allow Automatic Tasks in Folder and选择允许文件夹中的自动任务
  4. close and reopen your folder (or Workspace)关闭并重新打开您的文件夹(或工作区)

The sass compiler will be called and starts watching all your edits with a reassuring: sass 编译器将被调用并开始以令人放心的方式查看您的所有编辑:

Compiled css\src\style.sass to css\style.css.
Sass is watching for changes. Press Ctrl-C to stop.

or with error messages when compilation failed.:或编译失败时出现错误消息。:

Error: semicolons aren't allowed in the indented syntax.
  ╷
7 │     padding: 0;
  │               ^
  ╵
  css\src\_base.sass 7:12  @import
  css\src\style.sass 1:9   root stylesheet

Or use Easy Compile - it will auto compile on save. 或使用简易编译-保存时会自动编译。

https://marketplace.visualstudio.com/items?itemName=refgd.easy-compile https://marketplace.visualstudio.com/items?itemName=refgd.easy-compile

Easy Compile or Live SASS Compiler extensions for Visual Studio Code. 适用于Visual Studio Code的Easy Compile或Live SASS Compiler扩展。 The Live SASS Compiler can recompile all sources, whereas Easy Compile just compiles a single file. Live SASS编译器可以重新编译所有源代码,而Easy Compile仅编译一个文件。

Easy Compile compiles when you save a file, whereas Live SASS Compiler can be made to watch your code and compile when it sees a change. 保存文件时,Easy Compile会进行编译,而Live SASS Compiler可以用来观察代码并在看到更改时进行编译。 You must manually start it every time, whereas Easy Compile runs out of the box. 您必须每次都手动启动它,而Easy Compile则是开箱即用的。

There already is an official document out there https://code.visualstudio.com/docs/languages/css#_step-3-create-tasksjson已经有一个官方文件https://code.visualstudio.com/docs/languages/css#_step-3-create-tasksjson

Only tip we can consider here is put an argument of --watch just not to build manually by hitting ctrl+shift+b every time.我们在这里可以考虑的唯一提示是放置一个--watch参数,只是不要每次都按ctrl+shift+b手动构建。

// Sass configuration
{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Sass Compile",
      "type": "shell",
      "command": "sass --watch styles.scss styles.css",
      "group": "build"
    }
  ]
}

Without any plugins, you can create .vscode folder in your project and just write some tasks.json没有任何插件,你可以在你的项目中创建 .vscode 文件夹,只需要写一些 tasks.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM