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

gulp-prettier-plugin

v1.3.0

Published

gulp plugin to format source code with prettier

Downloads

2,045

Readme

gulp-prettier-plugin

Build Status Coverage Status styled with prettier

gulp plugin to format your source code files with prettier.

Install

yarn add --dev gulp-prettier-plugin

prettier is marked as a peer dependency, so if you already have it installed, the plugin will use that version.

Usage

This module exports a single function with this signature:

declare const prettierPlugin: (prettierOptions: any, pluginOptions: IPrettierPluginOptions) => PrettierTransform;

The function receives 2 optional arguments: prettierOptions, the options object to configure prettier, and pluginOptions, options to configure the plugin. The return object is a Transform stream, just like most gulp plugins. The supported plugin options are the following:

Name | Type | Description --- | --- | --- filter | boolean | If true, the plugin will only emit files that need formatting. This is useful when you are piping to gulp.dest and only want to write the files that haven't been formatted yet, otherwise every single file will be rewritten. validate | boolean | If true, after all files are processed, it will throw an error if any input files were not already formatted, detailing the paths of each. You might want to turn this on only in continuous integration environments to make sure all files are formatted before any patches are merged to your repository.

All options are false by default.

Examples

Format in-place only JS files that haven't been already formatted:

const gulp = require('gulp');
const prettierPlugin = require('gulp-prettier-plugin');

gulp.task('prettier', () =>
  gulp
    .src(['./src/**/*.js', './gulpfile.js'])
    .pipe(prettierPlugin(undefined, { filter: true }))
    // passing a function that returns base will write the files in-place
    .pipe(gulp.dest(file => file.base))
);

Format in-place all TypeScript and LESS files that haven't already been formatted:

gulp.task("prettier", () =>
  gulp
    .src(["./src/**/*.ts", "./src/**/*.less"])
    .pipe(prettierPlugin(undefined, { filter: true }))
    // passing a function that returns base will write the files in-place
    .pipe(gulp.dest(file => file.base))
);

Same as the previous example, but written in TypeScript

import * as gulp from 'gulp'
import * as prettierPlugin from 'gulp-prettier-plugin'

gulp.task('prettier', () =>
  gulp
    .src(['./src/**/*.ts', './src/**/*.less'])
    .pipe(prettierPlugin(undefined, { filter: true })
    )
    // passing a function that returns base will write the files in-place
    .pipe(gulp.dest(file => file.base))
);

Format each and every JS file in place, using the trailing-comma and single-quote options, and pipe any other plugin such as eslint before writing:

const gulp = require('gulp');
const prettierPlugin = require('gulp-prettier-plugin');
const eslint = require('gulp-eslint');

gulp.task('prettier', () =>
  gulp
    .src(['./src/**/*.js', './gulpfile.js'])
    .pipe(prettierPlugin())
    .pipe(eslint())
    .pipe(eslint.failAfterError())
    // passing a function that returns base will write the files in-place
    .pipe(gulp.dest(file => file.base))
);

Scan al JS files and when it finds a file that hasn't been formatted yet, format it in-place and save the path so that if it is running in a CI environment, it throws an error detailing the files that were not already formatted.

const gulp = require('gulp');
const prettierPlugin = require('gulp-prettier-plugin');
const isCI = process.env.CI;

gulp.task('prettier', () =>
  gulp
    .src(['./src/**/*.js', './gulpfile.js'])
    .pipe(
      prettierPlugin(
        {
          trailingComma: 'es5',
          singleQuote: true,
        },
        {
          filter: true,
          validate: isCI,
        }
      )
    )
    // passing a function that returns base will write the files in-place
    .pipe(gulp.dest(file => file.base))
);