Git Product home page Git Product logo

gulp5-sass-plugin's Introduction

gulp5-sass-plugin

CodeQL Test and coverage codecov

Sass plugin for gulp5.

Compare

We strongly recommend you to use this plugin instead of gulp-sass or gulp-dart-sass.

Why

gulp-sass

gulp-sass is still using node-sass by default, and it has been deprecated for quite a long while.

Also, node-sass will take a long time to built during installation.

gulp-dart-sass

gulp-dart-sass is just forking the above project and changed it's deps, while it:

  • just remove sourcemap and pipe tests
  • still remain the old deps

gulp5-sass-plugin

It's a totally rewrite version in typescript. It has:

  • Uses new compile api
  • Option interface, and will provide autocomplete and validate (with IDE support like VSCode)
  • Code quality test and 100% test coverage

Install

pnpm add -D gulp5-sass-plugin
# or
yarn add -D gulp5-sass-plugin
# or
npm i -D gulp5-sass-plugin

Basic Usage

You should use sass to synchronously transform your sass code in to css:

import { dest, src, watch } from "gulp";
import { sass } from "gulp5-sass-plugin";

export const build = () =>
  src("./styles/**/*.scss")
    .pipe(sass().on("error", sass.logError))
    .pipe(dest("./css"));

export const watch = () => watch("./styles/**/*.scss", build);

You can also compile asynchronously:

import { dest, src, watch } from "gulp";
import { sassAsync } from "gulp5-sass-plugin";

export const build = () =>
  src("./styles/**/*.scss")
    .pipe(sassAsync().on("error", sassAsync.logError))
    .pipe(dest("./css"));

export const watch = () => watch("./styles/**/*.scss", build);

Error logging

Note that we provide a useful function called logError on these 2 transform functions to let you print errors gracefully.

See the demo above for usage.

Performance

Note that synchronous compilation is twice as fast as asynchronous compilation by default, due to the overhead of asynchronous callbacks. To avoid this overhead, you can use the fibers package to call asynchronous importers from the synchronous code path. To enable this, pass the Fiber class to the fiber option:

const { dest, src, watch } = require("gulp");
const { sass } = require("gulp5-sass-plugin");
const fiber = require("fibers");

const build = () =>
  src("./styles/**/*.scss")
    .pipe(sass({ fiber }).on("error", sass.logError))
    .pipe(dest("./css"));

exports.build = build;
exports.watch = () => watch("./styles/**/*.scss", build);

Options

You should pass in options just like you would for Dart Sass compileString api. They will be passed along just as if you were using sass. We also export SassOption and SassAsyncOption interface in declaration files.

For example:

export const build = () =>
  src("./styles/**/*.scss")
    .pipe(sass({ outputStyle: "compressed" }).on("error", sass.logError))
    .pipe(dest("./css"));

Or this for asynchronous code:

export const build = () =>
  src("./styles/**/*.scss")
    .pipe(
      sassAsync({ outputStyle: "compressed" }).on("error", sassAsync.logError),
    )
    .pipe(dest("./css"));

Source Maps

gulp5-sass-plugin can be used together with gulp-sourcemaps to generate source maps for the Sass to CSS compilation. You will need to initialize gulp-sourcemaps prior to running gulp5-sass-plugin and write the source maps after.

const { dest, src, watch } = require("gulp");
const { sassAsync } = require("gulp5-sass-plugin");
const sourcemaps = require("gulp-sourcemaps");

exports.build = () =>
  src("./styles/**/*.scss")
    .pipe(sourcemaps.init())
    .pipe(sassAsync({ outputStyle: "compressed" }).on("error", sass.logError))
    .pipe(sourcemaps.write())
    .pipe(dest("./css"));

By default, gulp-sourcemaps writes the source maps inline in the compiled CSS files. To write them to a separate file, specify a path relative to the gulp.dest() destination in the sourcemaps.write() function.

const { dest, src, watch } = require("gulp");
const { sassAsync } = require("gulp5-sass-plugin");
const sourcemaps = require("gulp-sourcemaps");

exports.build = () =>
  src("./styles/**/*.scss")
    .pipe(sourcemaps.init())
    .pipe(sassAsync({ outputStyle: "compressed" }).on("error", sass.logError))
    .pipe(sourcemaps.write("./maps"))
    .pipe(dest("./css"));

Warning

Currently we only support gulp-sourcemaps with sassAsync function.

Node Support

Since sass only supports Node.js 14 and above, we also only support Node.js 14 and above.

Issues

gulp5-sass-plugin is a very light-weight wrapper around Dart Sass. Because of this, the issue you're having likely isn't a gulp5-sass-plugin issue, but an issue with one those projects or with Sass as a whole.

If you have a feature request/question how Sass works/concerns on how your Sass gets compiled/errors in your compiling, it's likely a Dart Sass issue and you should file your issue with one of those projects.

If you're having problems with the options you're passing in, it's likely a Dart Sass and you should file your issue with one of those projects.

We may, in the course of resolving issues, direct you to one of these other projects. If we do so, please follow up by searching that project's issue queue (both open and closed) for your problem and, if it doesn't exist, filing an issue with them.

gulp5-sass-plugin's People

Contributors

mister-hope avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

gulp5-sass-plugin's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): replace dependency standard-version with commit-and-tag-version 9.5.0
  • chore(deps): update dependency rollup to v4.18.0

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

github-actions
.github/workflows/codeql-analysis.yml
  • actions/checkout v4
  • github/codeql-action v3
  • github/codeql-action v3
  • github/codeql-action v3
.github/workflows/test.yml
  • actions/checkout v4
  • pnpm/action-setup v2
  • actions/setup-node v4
  • codecov/codecov-action v4
npm
package.json
  • picocolors ^1.0.1
  • plugin-error ^2.0.1
  • replace-ext ^2.0.0
  • sass ^1.77.2
  • source-map-js ^1.2.0
  • strip-ansi ^7.1.0
  • vinyl ^3.0.0
  • vinyl-sourcemaps-apply ^0.2.1
  • @commitlint/cli 19.3.0
  • @commitlint/config-conventional 19.2.2
  • @rollup/plugin-node-resolve 15.2.3
  • @types/globule 1.1.9
  • @types/gulp 4.0.17
  • @types/gulp-postcss 8.0.6
  • @types/gulp-sourcemaps 0.0.38
  • @types/gulp-tap 1.0.5
  • @types/node 20.12.12
  • @types/vinyl 2.0.12
  • @vitest/coverage-v8 1.6.0
  • autoprefixer 10.4.19
  • commitizen 4.3.0
  • cz-git 1.9.1
  • del 7.1.0
  • esbuild 0.21.3
  • eslint 8.57.0
  • eslint-config-mister-hope 0.1.0
  • globule 1.3.4
  • gulp 5.0.0
  • gulp-postcss 10.0.0
  • gulp-sourcemaps 3.0.0
  • gulp-tap 2.0.0
  • husky 9.0.11
  • nano-staged 0.8.0
  • postcss 8.4.38
  • prettier 3.2.5
  • rimraf 5.0.7
  • rollup 4.17.2
  • rollup-plugin-dts 6.1.1
  • rollup-plugin-esbuild 6.1.1
  • standard-version 9.5.0
  • typescript 5.4.5
  • vite 5.2.11
  • vitest 1.6.0
  • gulp ^5.0.0
  • node >=14
  • pnpm 9.1.1

  • Check this box to trigger a request for Renovate to run again on this repository

[Bug] Source maps are broken when used with `SrcOptions#base` option

This plugin breaks source maps when used with the base option of gulp.src(), and Chrome DevTools, for example, shows an error in the Sources panel like this:

Could not load content for http://localhost:8080/dist/styles/_partial.scss (HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE)

Also, even without the base option, when using gulp's built-in source maps support, only the source maps for partials will be broken, and the source maps for stylesheets in node_modules will work.

Quick summary:

Using the base option? Source Maps provider Are the source maps for partials valid? Are the source maps for node_modules valid?
No Plugin Yes Yes
Yes Plugin No No
No Built-in No Yes
Yes Built-in No No

Minimal reproduction: https://github.com/chalkygames123/repro-mr-hope__gulp-sass-source-maps

As a workaround, enabling sourceMapIncludeSources option of the Sass compiler fixes this issue.

[Feature Request] Add the ability to use grass instead of dart-sass

Describe the feature and why you wanted
Grass promises 2x performance gains over dart-sass. It would be nice to be able to use it with gulp-sass, perhaps by setting some parameter in the options, or even being able to provide your own SASS implementation. For example

.pipe(sass({ compiler: 'grass' /* default being 'dart-sass' */ }))

or

import * as grass from 'grass';
import * as sass from 'sass';

const useGrass = true;

.pipe(sass({ compiler: useGrass ? grass : sass }))

[Feature Request] Show more details in logError

when I get an error in my SCSS file, I get an error message with little information.
In my opinion, this was more detailed in the past as well.
Maybe it is possible to add a more advanced option for the logError function.

This is my standard output:
original-w

I find this output more helpful:
with-messageOriginal-w

this is my default gulpfile

const { sass } = require("@mr-hope/gulp-sass");
....

function css() {
  return src(paths.scss)
    .pipe(sass({style: 'compressed'}).on("error", sass.logError))
    .pipe(autoprefixer({
      cascade: false
    }))
    .pipe(dest(paths.destinationCss));
}
....

package.json:

...
    "@mr-hope/gulp-sass": "^3.0.2",
    "gulp": "^4.0.2",
...

Node 16 on Linux Mint 20.03

[Bug] Using /**/ glob when watching causes a steady increase in build time

Describe the bug
When the watch glob contains /**/, build times increase each time the build gets triggered by the watcher

Additional context

Here's a watch log when the glob is

const roots = {
    css: './project/wwwroot/css',
}
const watchGlobs = {
    sass: [
        `${roots.css}/**/*.sass`,
        `${roots.css}/**/*.scss`,
    ],
}
[00:20:23] Starting 'watch:css'...
[00:20:28] Starting 'css'...
[00:20:34] Finished 'css' after 5.33 s
[00:20:35] Starting 'css'...
[00:20:40] Finished 'css' after 4.98 s
[00:20:41] Starting 'css'...
[00:20:48] Finished 'css' after 6.54 s
[00:20:49] Starting 'css'...
[00:20:58] Finished 'css' after 8.72 s
[00:21:00] Starting 'css'...
[00:21:11] Finished 'css' after 10 s
[00:22:35] Starting 'css'...
[00:22:48] Finished 'css' after 13 s

And here's when the glob avoids using /**/

const roots = {
    css: './project/wwwroot/css',
}
const watchGlobs = {
    sass: [
        `${roots.css}/*.sass`,
        `${roots.css}/src/*.sass`,
        `${roots.css}/src/elements/*.sass`,
        `${roots.css}/src/admin-elements/*.sass`,
        `${roots.css}/src/mixins/*.sass`,

        `${roots.css}/*.scss`,
        `${roots.css}/src/*.scss`,
        `${roots.css}/src/elements/*.scss`,
        `${roots.css}/src/admin-elements/*.scss`,
        `${roots.css}/src/mixins/*.scss`,
    ],
}
[00:25:48] Starting 'watch:css'...
[00:25:53] Starting 'css'...
[00:25:57] Finished 'css' after 4.06 s
[00:26:02] Starting 'css'...
[00:26:03] Finished 'css' after 1.35 s
[00:26:05] Starting 'css'...
[00:26:06] Finished 'css' after 1.08 s
[00:26:07] Starting 'css'...
[00:26:08] Finished 'css' after 929 ms
[00:26:09] Starting 'css'...
[00:26:09] Finished 'css' after 887 ms
[00:26:10] Starting 'css'...
[00:26:11] Finished 'css' after 889 ms

Here's my CSS task

// CSS tasks
gulp.task('css', () => {
    const processors = [
        autoprefixer,
        discard({removeAll: true}),
        mqpacker,
        nano({preset: 'default'})
    ];

    return pipeline(gulp.src(`${roots.css}/*.sass`),
        sourcemaps.init(),                // Init maps
        sass({ fiber }),                       // Compile SASS
        gulp.dest(roots.css),            // Output the raw CSS
        postcss(processors),           // Postprocess it
        sourcemaps.write(`./`),        // Write maps
        cond('**/*.css',                    // If it's a css file and not a map file
            rename({suffix: '.min'}),   // Add .min suffix
        ),
        gulp.dest(`${roots.css}/dist`),    // Output minified CSS
        errorHandler);
});

gulp.task('watch:css', () => gulp.watch(watchGlobs.sass, gulp.series('css')));

and the packages I use

{
		"@hail2u/css-mqpacker": "github:hail2u/node-css-mqpacker#v8.0.1",
		"@mr-hope/gulp-sass": "^2.0.0",
		"autoprefixer": "^10.2.3",
		"cssnano": "^4.1.10",
		"fibers": "^5.0.0",
		"gulp": "^4.0.2",
		"gulp-if": "^3.0.0",
		"gulp-postcss": "^9.0.0",
		"gulp-rename": "^2.0.0",
		"gulp-sourcemaps": "^3.0.0",
		"postcss": "^8.2.4",
		"postcss-discard-comments": "^4.0.2",
		"sass": "^1.32.5"
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.