Code Monkey home page Code Monkey logo

prettier-webpack-plugin's Introduction

Prettier Webpack Plugin

Greenkeeper badge All Contributors

Automatically process your source files with Prettier when bundling via Webpack.

How it works

This plugin reads all file dependencies in your dependency graph. If a file is found with a matching extension, the file is processed by Prettier and overwritten.

You can provide options Prettier by specifying them when creating the plugin.

Looking for a loader?

It's in its early stages, but you can find a loader version of this plugin here: prettier-webpack-loader

Installation

Note, for Webpack 4 support, install prettier-webpack-plugin@1. For Webpack < 4, install prettier-webpack-plugin@0

Simply run npm install --save-dev prettier prettier-webpack-plugin or yarn add --dev prettier prettier-webpack-plugin to install.

Then, in your Webpack config files, add the lines:

var PrettierPlugin = require("prettier-webpack-plugin");

module.exports = {
  // ... config settings here ...
  plugins: [
    new PrettierPlugin()
  ],
};

Why?

Keeping style consistent between programmers new to collaboration can be tricky. Prettier tackles this problem by formatting your code to fit its preferred style, which is admittedly very pretty. Now, your code is automatically formatted when you bundle with Webpack.

Perfect for group projects bundling code on save!

Usage

The API is very simple. The constructor accepts one argument, options, a JavaScript object which you can leverage to override any default behaviors. You can specify any of Prettier's options to override any of the defaults.

For the most basic example, simple add this in your webpack.config.js:

plugins: [
  new PrettierPlugin()
],

Or, you could add options to the plugin in the form of an Object:

plugins: [
  new PrettierPlugin({
    printWidth: 80,               // Specify the length of line that the printer will wrap on.
    tabWidth: 2,                  // Specify the number of spaces per indentation-level.
    useTabs: false,               // Indent lines with tabs instead of spaces.
    semi: true,                   // Print semicolons at the ends of statements.
    encoding: 'utf-8',            // Which encoding scheme to use on files
    extensions: [ ".js", ".ts" ]  // Which file extensions to process
  })
],

Again, see Prettier's options for a complete list of options to specify for Prettier.

Note that you can specify any option for Prettier to use in this object. So, all options are assumed to be for Prettier, and will thus be passed to prettier, with the exception of three for this plugin:

  • encoding (type: String)
    • The encoding scheme to use for the file.
    • Default: utf-8
  • extensions (type: [String])
    • Which file extensions to pass.
    • Default: Either what your version of Prettier supports, or [ ".css", ".graphql", ".js", ".json", ".jsx", ".less", ".sass", ".scss", ".ts", ".tsx", ".vue", ".yaml" ]
  • configFile (type: String)
    • Optional value to supply global config file from your project in order to avoid hardcoding values in multiple places
    • Default: .prettierrc (from your current project directory)

Testing

npm run test or yarn run test will show you how tests are going currently.

These tests can also serve as primitive examples for configuring Prettier Webpack Plugin.

Contributors

Thanks goes to these wonderful people (emoji key):


Josh Hawkins

๐Ÿ’ป ๐Ÿ“– ๐Ÿ’ก โš ๏ธ

Erwann Mest

๐Ÿ“–

Eduardo Sganzerla

๐Ÿ’ป ๐Ÿ“– โš ๏ธ

rkilgore-meta

๐Ÿ’ป ๐Ÿค” โš ๏ธ

Jacob

๐Ÿค”

Jason Salzman

๐Ÿ’ป ๐Ÿ“– ๐Ÿค” โš ๏ธ

pastelInc

๐Ÿ’ป

Nicolas Beauvais

๐Ÿ’ป

Alejandro Sanchez

๐Ÿ› ๐Ÿ’ป

This project follows the all-contributors specification. Contributions of any kind welcome!

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.