Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: add posibility to pass regular expressions to the 'import-order' rules #1015

Open
ghost opened this issue Feb 5, 2018 · 12 comments

Comments

@ghost
Copy link

ghost commented Feb 5, 2018

Hi everyone,

Now we have the default groups, such as ["builtin", "external", "parent", "sibling", "index"], but would be better to add possibility to pass regular expressions to these groups definitions. For example, like this:

["buildin", "external", ".constants.js$", "index"]

It will improve code readability by adding customization of import groupings.
I just want to get approve from maintainers of this repository. Its not a problem to implement it and create PR by myself.

@ljharb
Copy link
Member

ljharb commented Feb 5, 2018

The current purpose of the order rule is to group by kind - what you're asking for would be to group by path. I'm not sure if that's a particularly useful enhancement.

@ghost
Copy link
Author

ghost commented Feb 6, 2018

@ljharb Thanks for your response.
So, if you think that this proposal is unnecessary, lets close this issue.

@coreyfarrell
Copy link

What if we had the ability to define custom groups using regex?

{
    "groups": ["pify", "builtin", "external", "parent", "sibling", "index"],
    "custom-groups": {
        "pify": "^pify$"
    }
}

This would help me with the following code:

const pify = require('pify');
const fs = pify(require('fs'));

Currently my only option is to either import 'fs' to an alternate variable before importing 'pify', or allow 'builtin' and 'external' to be combined even though (for my use) pify is the only exception to the builtin requires first rule. I think an option for custom groups would allow both of our issues to be resolved without changing the meaning of values in the groups array. @AndrewShapel and @ljharb do you have thoughts on this?

@ghost
Copy link
Author

ghost commented May 12, 2018

@ljharb @coreyfarrell I agree with this idea. In that case, we group imports by kind and definition of these groups will be paths.

@atty-stripe
Copy link

Regex-defined import groups would be very helpful, especially in codebases that use path aliases (such as in Webpack.)

I would love to require that all imports from a particular path alias are grouped together, as follows:

import moment from 'moment';

import a from 'foo-files/path/to/a.js';
import b from 'foo-files/path/to/b.js';

import x from 'bar-files/path/to/x.js';
import y from 'bar-files/path/to/y.js';

@gpbl
Copy link

gpbl commented Oct 18, 2018

I also would like to have more granular grouping since we use aliases via the module-resolver babel plugin. 🙏

@munkyjunky
Copy link

A custom group would be very helpful - currently I'm working in a monorepo that has several packages included. When importing one package into another we want to import by builtin, external, custom-packages, parent, sibling, index

These custom packages are installed using link-local so they're currently seen as external modules. Being able to capture these as a custom group via regex would be a huge help!

@soryy708
Copy link
Contributor

Related: #674

@oliverdolgener
Copy link

I'd really like to see this merged. Such a good idea

@ykanavalik
Copy link

+1
would be really nice to have such feature

@bschlenk
Copy link

I'd like to be able to define a css group, anything ending with /\.s?css$/ should be sorted last.

@ljharb
Copy link
Member

ljharb commented Aug 16, 2021

Between #1570 and #1386 this seems like maybe it's already possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

9 participants