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

@breathelife/prettier-plugin-sort-imports

v3.0.5

Published

A prettier plugins to sort imports in provided RegEx order

Downloads

113

Readme

@breathelife/prettier-plugin-sort-imports

A prettier plugin to sort import declarations by provided RegEx order. Based on @trivago/prettier-plugin-sort-imports

Install

npm

npm install --save-dev @breathelife/prettier-plugin-sort-imports

or, using yarn

yarn add --dev @breathelife/prettier-plugin-sort-imports

Usage

Add an order in prettier config file.

module.exports = {
  "printWidth": 80,
  "tabWidth": 4,
  "trailingComma": "all",
  "singleQuote": true,
  "jsxBracketSameLine": true,
  "semi": true,
  "importOrder": ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
}

APIs

importOrder

A collection of regular expressions in string format. The plugin uses new RegExp to evaluate regular expression. E.g. node.source.value.match(new RegExp(val)) Here, val is the string provided in import order.

strictGrouping

Removes blank lines between import statements of the same group. Defaults to false.

experimentalBabelParserPluginsList

A collection of parser names for babel parser. The plugin passes this list to babel parser so it can understand the syntaxes used in the file being formatted. The plugin uses prettier itself to figure out the parser it needs to use but if that fails, you can use this field to enforce the usage of the plugins babel needs.

module.exports = {
  "printWidth": 80,
  "tabWidth": 4,
  "trailingComma": "all",
  "singleQuote": true,
  "jsxBracketSameLine": true,
  "semi": true,
  "importOrder": ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
  "strictGrouping": true,
  "experimentalBabelParserPluginsList" : ["jsx", "typescript"]
}

How does import sort work ?

The plugin extracts the imports which are defined in importOrder. These imports are local imports. The imports which are not part of the importOrder is considered as 3rd party imports.

After, the plugin sorts the local imports and 3rd party imports using natural sort algorithm. In the end, the plugin returns final imports with 3rd party imports on top and local imports at the end.

FAQ / Troubleshooting

Q. How can I add the RegEx imports in the importOrder list ?

You can define the RegEx in the importOrder. For example if you want to sort the following imports:

import React from 'react';
import classnames from 'classnames';
import z from '@server/z';
import a from '@server/a';
import s from './';
import p from '@ui/p';
import q from '@ui/q';

then the importOrder would be ["^@ui/(.*)$","^@server/(.*)$", '^[./]']. Now, the final output would be as follows:

import classnames from 'classnames';
import React from 'react';
import p from '@ui/p';
import q from '@ui/q';
import a from '@server/a';
import z from '@server/z';
import s from './';

Q. How can I run examples in this project ?

There is a examples directory. The examples file can be formatted by using npm run example command.

npm run example examples/example.tsx

Q. How to use the plugin with *.d.ts files ?

The plugin automatically ignores the *.d.ts files. We encourage you to declare the *.d.ts files in .prettierignore. (Read more here)[https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore].

Q. How does the plugin handle the first comment in the file.

The plugin keeps the first comment as it is in the file. The plugin also removes the new lines in between first comment and the first import. input:

// comment

import a from 'a'

output:

// comment
import a from 'a'

Q. I'm getting error about experimental syntax.

If you are using some experimental syntax and the plugin has trouble parsing your files, you might getting errors similar to this:

SyntaxError: This experimental syntax requires enabling one of the following parser plugin(s): ...

To solve this issue, you can use the new option experimentalBabelParserPluginsList in your .prettierrc and pass an array of plugin names to be used.

Difference between this fork and the upstream package

The upstream package collects every import lines, sort them according to the given order and rewrite the files with the sorted import statements at the top.

While this strategy works well for most use cases, at Breathe Life we sometimes have other types of statements inserted between imports, which won't be preserved with the original strategy. This fork replaces the import statements line-by-line and supports // prettier-ignore comments for statements that need to be preserved between imports.