gulp-prettier

raw JSON →
6.0.0 verified Sat Apr 25 auth: no javascript

A Gulp plugin to format files with Prettier. Current stable version is 6.0.0, released November 2023. This plugin integrates Prettier into Gulp build pipelines, allowing automatic code formatting during development or as a CI check via `prettier.check()`. Key differentiators: simple API mirroring Prettier's options, supports EditorConfig, and the `check` method for CI validation. Since v6.0.0, it is pure ESM and requires Node 18. Prior v5.0.0 upgraded Prettier to v3.0.0. It is a direct replacement for manually calling Prettier in Gulp tasks.

error Error [ERR_REQUIRE_ESM]: require() of ES Module ... from ... not supported.
cause Using CommonJS require() on a package that is pure ESM (v6+).
fix
Switch to import statement: import prettier from 'gulp-prettier'; or use dynamic import().
error Error: Cannot find module 'gulp-prettier'
cause gulp-prettier not installed or not in node_modules.
fix
Run: npm install gulp-prettier --save-dev
error TypeError: prettier is not a function
cause Incorrect import pattern; importing default incorrectly.
fix
Use: import prettier from 'gulp-prettier';
error Error: No files matched pattern: ... (or similar Gulp error)
cause glob pattern in gulp.src() matches no files.
fix
Ensure src pattern is correct and files exist.
breaking v6.0.0 moved to pure ESM and requires Node >=18. CommonJS require() no longer works.
fix Switch to ESM imports (import prettier from 'gulp-prettier') and ensure Node >=18.
breaking v5.0.0 upgraded Prettier to v3.0.0, which has breaking changes in its API and options.
fix Review Prettier v3 changelog for option changes; update your Prettier options accordingly.
breaking v4.0.0 dropped Node 10 support.
fix Upgrade Node to >=12 (or later).
breaking v3.0.0 dropped Node 4,6,8 and upgraded Prettier to v2.0.0.
fix Use Node >=10 and update Prettier options for v2 changes.
gotcha The 'check' method throws an error if files are not formatted, which can break Gulp watch tasks. Use it only in CI or as a final validation step.
fix Wrap in try/catch or use separate CI-only task.
deprecated Options like 'singleQuote' and 'trailingComma' are part of Prettier's options and may change in future Prettier versions.
fix Consult Prettier documentation for valid options; use .prettierrc file to share config.
npm install gulp-prettier
yarn add gulp-prettier
pnpm add gulp-prettier

Defines two Gulp tasks: 'format' to format JavaScript files using Prettier with options, and 'check' to validate formatting in CI.

import gulp from 'gulp';
import prettier from 'gulp-prettier';

export function format() {
  return gulp.src('src/**/*.js')
    .pipe(prettier({ singleQuote: true, trailingComma: 'all' }))
    .pipe(gulp.dest('dist'));
}

export function check() {
  return gulp.src('dist/**/*.js')
    .pipe(prettier.check({ singleQuote: true }));
}