npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bdchauvette/gulp-prettier

v2.0.0

Published

Gulp plugin to format code with prettier

Downloads

346

Readme

gulp-prettier

Gulp plugin to format code with prettier

styled with prettier codecov Build Status dependencies Status

Installation

npm install --save-dev @bdchauvette/gulp-prettier

prettier is a peer dependency, so make sure to install it if it's not already in your package.json:

npm install --save-dev prettier

Usage

const gulp = require("gulp");
const prettier = require("@bdchauvette/gulp-prettier");

gulp.task("prettify", () =>
  gulp
    .src("src/**/*.js")
    .pipe(
      prettier({
        // Normal prettier options, e.g.:
        singleQuote: true,
        trailingComma: "all"
      })
    )
    .pipe(gulp.dest(file => file.base))
);

The plugin adds a boolean didPrettierFormat property to the Vinyl file object. You can use this property to only output changed files, or fail CI builds if any files were not changed (see recipes below).

Options

For the available options, see the prettier documentation.

The only option that is not passed directly through to prettier is filepath. This plugin overrides the filepath property to match the path property of the Vinyl File object being formatted. This allows prettier to infer which parser to use if you do not explicitly set the parser option.

For example, if you use gulp.src('**/*.css'), prettier will automatically infer that it needs to use the postcss parser to format your stylesheets.

Recipes

Only output changed files

If you only want to write files that have actually been changed, you can use gulp-filter to filter out files that were already correctly formatted.

const gulp = require("gulp");
const filter = require("gulp-filter");
const prettier = require("@bdchauvette/gulp-prettier");

gulp.task("prettify", () =>
  gulp
    .src("src/**/*.js")
    .pipe(prettier())
    .pipe(filter(file => file.didPrettierFormat))
    .pipe(gulp.dest(file => file.base))
);

Fail CI builds on unformatted files

In a CI environment, you might want to fail the build if you detect any files that haven't been formatted by prettier. You can do this by using gulp-if to pipe to gulp-error if any files are freshly formatted (i.e. they have the didPrettierFormat property).

const gulp = require("gulp");
const gulpError = require("gulp-error");
const gulpIf = require("gulp-if");
const isCI = require("is-ci");
const prettier = require("@bdchauvette/gulp-prettier");

gulp.task("prettify", () =>
  gulp
    .src("src/**/*.js")
    .pipe(prettier())
    .pipe(gulpIf(file => isCI && file.didPrettierFormat, gulpError()))
    .pipe(gulp.dest(file => file.base))
);

Custom prettier builds

If you would like to use a custom build of prettier, e.g. prettier-miscellaneous, you can require @bdchauvette/gulp-prettier/factory, then pass it your own version of prettier:

const gulp = require("gulp");
const customPrettier = require("prettier-miscellaneous");
const createGulpPrettier = require("@bdchauvette/gulp-prettier/factory");

const gulpPrettier = createGulpPrettier(customPrettier);

gulp.task("prettify", () =>
  gulp
    .src("src/**/*.js")
    .pipe(gulpPrettier({ singleQuote: true }))
    .pipe(gulp.dest(file => file.base))
);

Sourcemaps

prettier does not currently support sourcemaps, so neither does this plugin.

See #445 and #2073 for discussion.


Related projects

There are a few different projects that integrate prettier and gulp:


Development

Testing

npm test

Linting

npm run lint
# With fixes
npm run lint:fix