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

prettier-plugin-organize-imports

v4.1.0

Published

Make prettier organize your imports using the TypeScript language service API.

Downloads

3,948,422

Readme

GitHub Workflow Status

Prettier Plugin: Organize Imports

Make sure that your import statements stay consistent no matter who writes them and what their preferences are.

A plugin that makes Prettier organize your imports (i. e. sorts, combines and removes unused ones) using the organizeImports feature of the TypeScript language service API. This is the same as using the "Organize Imports" action in VS Code.

Features

  • 👌 Dependency-free (just peer-dependencies you probably already have).
  • 💪 Supports .js, .jsx, .ts, .tsx and .vue files.
  • 🚀 Zero config.
  • 🤓 No more weird diffs or annoying merge conflicts in PRs caused by import statements.
  • 🤯 If your editor supports auto-imports, you'll stop thinking about your imports so much that you won't even care about their order anymore.

Caveat

This plugin inherits, extends, and then overrides the built-in Prettier parsers for babel, babel-ts, typescript and vue. This means that it is incompatible with other plugins that do the same; only the last loaded plugin that exports one of those parsers will function.

Installation

npm install --save-dev prettier-plugin-organize-imports

Note that prettier and typescript are peer dependencies, so make sure you have those installed in your project.

Usage

Prettier 3

Automatic plugin discovery has been removed. Thus you need to configure Prettier to use the plugin according to the Plugins docs, for example by adding it to the plugins config option:

{
  "plugins": ["prettier-plugin-organize-imports"]
}

Prettier 2

The plugin will be loaded by Prettier automatically. No configuration needed.

Note that automatic plugin discovery is not supported with some package managers, e. g. Yarn PnP (see https://github.com/prettier/prettier/issues/8474). In that case follow the instructions for Prettier 3 above.

Configuration

Skip Files

Files containing the substring // organize-imports-ignore or // tslint:disable:ordered-imports are skipped.

Skip Destructive Code Actions

If you don't want destructive code actions (like removing unused imports), you can enable the option organizeImportsSkipDestructiveCodeActions via your Prettier config.

{
  "organizeImportsSkipDestructiveCodeActions": true
}

Compatibility

ESLint

For compatibility with ESLint or other linters, see "Integrating with Linters" in the Prettier docs. You should have any import order rules/plugins disabled.

React

Depending on your configuration, if you need the React import to stay even if it's "unused" (i.e. only needed for the JSX factory), make sure to have the jsx option set to react in your tsconfig.json. For more details click here.

Vue.js

Make sure that you have the optional peer dependency vue-tsc installed.

npm install --save-dev vue-tsc

If you're using Vue.js with Pug templates, you'll also need to install @vue/language-plugin-pug as a dev dependency, and configure it in vueCompilerOptions (see usage).

Debug Logs

If something doesn't work, you can try to prefix your prettier command with DEBUG=true which will enable this plugin to print some logs.

Rationale/Disclaimer

This plugin acts outside of Prettier's scope because "Prettier only prints code. It does not transform it.", and technically sorting is a code transformation because it changes the AST (this plugin even removes code, i. e. unused imports). In my opinion however, the import statements are not really part of the code, they are merely directives that instruct the module system where to find the code (only true as long as your imports are side-effects free regarding the global scope, i. e. import order doesn't matter), comparable with using directives in C# or #include preprocessing directives in C. Therefore the practical benefits outweigh sticking with the philosophy in this case.

Changelog

See changelog.md.

License

MIT.