search
HomeWeb Front-endHTML Tutorialgulp-ruby-sass与gulp-sass_html/css_WEB-ITnose

I have been working in js since graduation. I have never written css, and my understanding of css is still at the level of self-study in school. My basic skills are too poor, so I have recently started to study in depth.

I learned that sass and less are more popular, so I decided to choose one. I won’t discuss the pros and cons of the two. I chose sass because it is closer to ruby.

Usually when writing sass, it is necessary to save and compile, but recently I have been using visual studio code to develop. This editor is not mature enough and does not support the function of compiling sass. I can only do it myself. Let's write a script and do it.

There is nothing to say about using gulp to do this work. I searched on npm and found gulp-ruby-sass because the ruby ​​environment is installed on the machine. The next script is that the installation is smooth and the use is very cool.

var gulp = require('gulp');var sass = require('gulp-ruby-sass');var group = require('gulp-group-files');var sassFiles = {    "xxx" : {        src: "./xxx/styles/sass/index.scss",        dest: "./xxx/styles/"    }};gulp.task('sass:compile',function (){    return group(sassFiles,function (key,fileset){        return sass(fileset.src)            .on('error', function (err) {                console.error('compile sass file error: %s', err.message);            })            .pipe(gulp.dest(fileset.dest));    })();});gulp.task('sass:watch',function (){    gulp.watch('**/*.scss',['sass:compile'])});gulp.task('default',['sass:watch']);

The code looks very nice, there is no problem, I am a little complacent. However, I encountered such a strange thing today, which is the reason for writing this article.

A problem occurred when I added an item to the projects that needed to be built. If talk is cheap, just show u the code.

var gulp = require('gulp');var sass = require('gulp-ruby-sass');var group = require('gulp-group-files');var sassFiles = {    "xxx" : {        src: "./xxx/styles/sass/index.scss",        dest: "./xxx/styles/"    },    "yyy" : {        src: "./yyy/styles/sass/index.scss",        dest: "./yyy/styles/"    }};gulp.task('sass:compile',function (){    return group(sassFiles,function (key,fileset){        return sass(fileset.src)            .on('error', function (err) {                console.error('compile sass file error: %s', err.message);            })            .pipe(gulp.dest(fileset.dest));    })();});gulp.task('sass:watch',function (){    gulp.watch('**/*.scss',['sass:compile'])});gulp.task('default',['sass:watch']);

Compared with the above, there is just one more thing that needs to be compiled. It doesn't look like a problem, but... The content of xxx/styles/index.css generated by compilation is actually yyy/styles/index.css, and sometimes the other way around.

I once suspected that there was a problem with my code. I changed it several times, but the problem remained the same. I have no choice but to look at the gulp-ruby-sass source code. I have a general understanding of the principle of this plug-in, as follows:

  • Create a temporary directory to store the css files generated by compilation;

  • Call the sass command to compile and generate The css file is first placed in the temporary directory;

  • reads the content of the css file into a stream and pipes it to gulp.dest defined in the gulp task. This completes the compilation and compilation of the scss file. Generation of css files;

  • Delete temporary directories and temporary files

  • The cause of the problem is the deletion of temporary files and directories in step 4, continuously When compiling multiple files, only a temporary directory is generated. After the first file is compiled successfully, the temporary directory is deleted, and subsequent file readings will cause errors. Depending on how long the operation takes, the following situations may occur:

  • Only one of the two files is compiled successfully

  • The content of the two files is the same (if the file name Same)

  • The file content does not match
    ...

  • After learning about the execution process of gulp-ruby-sass, I decided to abandon it. The reason is For the problems listed above, I don’t want to change my code.

    Continuing to search on npm, I found gulp-sass.

    npm install --save-dev gulp-sass. After waiting, npm reported an error.

    wtf!!! Well, just think of it as a blessing in disguise. As can be seen from npm-debug.log, gulp-sass relies on the node-sass library. When installing the node-sass library, you need to execute a script, and if something goes wrong during the process, you can try installing it as an administrator.

    Install again as administrator, this time it is successful, and then change gulpfile.js

    var gulp = require('gulp');var sass = require('gulp-sass');var group = require('gulp-group-files');var sassFiles = {    "xxx" : {        src: "./xxx/styles/sass/index.scss",        dest: "./xxx/styles/"    },    "yyy" : {        src: "./yyy/styles/sass/index.scss",        dest: "./yyy/styles/"    }};gulp.task('sass:compile',function (){    return group(sassFiles,function (key,fileset){        return gulp.src(fileset.src)            .pipe(sass().on('error', sass.logError))            .pipe(gulp.dest(fileset.dest));    })();});gulp.task('sass:watch',function (){    gulp.watch('**/*.scss',['sass:compile'])});gulp.task('default',['sass:watch']);

    By looking at the dependencies and part of the source code, you can find that gulp-ruby-sass and gulp The difference between -sass lies in the different compilers and different compilation processes.

    • gulp-ruby-sass is a call to sass, so a ruby ​​environment is required, and temporary directories and temporary files need to be generated

    • gulp-sass is a call to For node-sass, it is enough to have a node.js environment. The compilation process does not require temporary directories and files, and is directly converted through the buffer content.

    After fixing this problem, I can use it happily and I can continue to learn css.

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

    The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

    HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

    The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

    The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

    The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

    HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

    The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

    HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

    HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

    HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

    HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

    From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

    HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

    Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

    WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Tools

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft

    EditPlus Chinese cracked version

    EditPlus Chinese cracked version

    Small size, syntax highlighting, does not support code prompt function

    MantisBT

    MantisBT

    Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use