Code Monkey home page Code Monkey logo

gulp-prettier-plugin's Introduction

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))
);

gulp-prettier-plugin's People

Contributors

gaumala avatar tdolsen avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

tdolsen

gulp-prettier-plugin's Issues

Not formating my file

Before

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import { AppRegistry } from 'react-native';
import Navigation from './src/components/Navigation';

export default class gamLogin extends Component {
  render() {
    return <Navigation />;
  }
}

AppRegistry.registerComponent("login", () => gamLogin);

Task

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

Expected

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import { AppRegistry } from 'react-native';
import Navigation from './src/components/Navigation';

export default class gamLogin extends Component {
  render() {
    return <Navigation />;
  }
}

AppRegistry.registerComponent('login', () => gamLogin);

Result

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import { AppRegistry } from 'react-native';
import Navigation from './src/components/Navigation';

export default class gamLogin extends Component {
  render() {
    return <Navigation />;
  }
}

AppRegistry.registerComponent("login", () => gamLogin);

Could not find a declaration file for module 'gulp-prettier-plugin'

I'm trying to import gulp-prettier-plugin in my gulpfile.ts, but tslint keeps giving the above error. Here's my code:

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

gulp.task('prettier', () => {
  return gulp.src('{gulpfile,src/**/*}.{ts,tsx}')
    .pipe(prettier({ parser: 'typescript' }))
})

TypeError: this is not a typed array.

I'm trying to run prettier on a couple of files but I'm getting this error:

[11:49:32] Starting 'prettier'...
../node_modules/gulp-prettier-plugin/dist/index.js:74
    outputFile._contents = buffer_1.Buffer.from(transformedCode);

TypeError: this is not a typed array.
    at Function.from (native)
    ...

when running gulp prettier.

My gulp file setup is like this:

var prettier  = require('gulp-prettier-plugin');
gulp.task('prettier', () =>
    gulp
        .src(['.public/library/js/scripts.js', './gulpfile.js'])
        .pipe(
            prettier({
                trailingComma: 'all',
                singleQuote: true,
                bracketSpacing: true,
                useTabs: true,
                printWidth: 100,
            }
        )
    )
    .pipe(gulp.dest(file => file.base))
);

I've updated all my dependencies to be sure that's not it so I'm guessing I'm missing something really obvious!

css support?

Errors out when attempting formatting a Less file. Ever consider adding Less/SCSS support to this plugin? Would be great if it could be handled with this plugin instead of another one specific to styles.

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.