import-sort
import-sort copied to clipboard
Local import path
Hello!
I have a private project in which I need to include custom import formatting rules and I can't / do not want to publish import-sort-style-* module. Is there a way I could import plugin from local project directory?
As a workaround I am using ln -s ../import-sort-style-mystyle ./node_modules, but is there a cleaner way?
EDIT:
I am using this plugin file so it makes no purpose to publish this as an NPM module. It relies too strongly on project specific directory structure:
function isLocalClass(imported) {
return imported.moduleName.startsWith("src/framework/classes");
}
function isLocalComponent(imported) {
return imported.moduleName.startsWith("src/components");
}
function isLocalEffect(imported) {
return imported.moduleName.startsWith("src/effect");
}
function isLocalHelper(imported) {
return imported.moduleName.startsWith("src/framework/helpers");
}
function isLocalInterface(imported) {
return imported.moduleName.startsWith("src/framework/interfaces");
}
function isLocalModule(imported) {
return imported.moduleName.startsWith("src/");
}
function isLocalType(imported) {
return imported.moduleName.startsWith("src/framework/types");
}
function format(styleApi) {
const {
and,
hasDefaultMember,
hasNamedMembers,
hasNamespaceMember,
hasNoMember,
hasOnlyDefaultMember,
hasOnlyNamedMembers,
hasOnlyNamespaceMember,
isAbsoluteModule,
isRelativeModule,
member,
name,
naturally,
not,
or,
startsWithAlphanumeric,
startsWithLowerCase,
startsWithUpperCase,
} = styleApi;
return [
{
match: and(
hasNamespaceMember,
not(isLocalModule)
),
sort: member(naturally),
},
{
match: and(
hasOnlyDefaultMember,
not(isLocalModule)
),
sort: member(naturally),
},
{
match: not(isLocalModule),
sort: member(naturally),
},
{ separator: true },
// import Foo from "src/components/Foo"
{
match: isLocalComponent,
sort: member(naturally),
},
{ separator: true },
// import useFoo from "src/effects/useFoo"
{
match: isLocalEffect,
sort: member(naturally),
},
{ separator: true },
// import Foo from "src/framework/classes/Foo"
// import foo from "src/framework/helpers/foo"
{
match: and(
hasOnlyDefaultMember,
or(
isLocalClass,
isLocalHelper
),
),
sort: member(naturally),
},
{
match: or(
isLocalClass,
isLocalHelper
),
sort: member(naturally),
},
{ separator: true },
// import Foo from "src/framework/interfaces/Foo"
// import Foo from "src/framework/types/Foo"
{
match: and(
hasOnlyDefaultMember,
or(
isLocalInterface,
isLocalType,
),
),
sort: member(naturally),
},
{
match: or(
isLocalInterface,
isLocalType,
),
sort: member(naturally),
},
{ separator: true },
];
}
module.exports = format;
Also, overall I am really grateful for this project. It really saves a lot of time and effort to keep everything in order. :)
Side note: using Yarn portal / link protocols might help here as a workaround also: https://next.yarnpkg.com/features/protocols
This is great and should be added to the README !
Hi @mcharytoniuk we are using it with a scoped package and it works like a charm 👍
just add it like this into package.js
"importSort": {
".js, .jsx, .es6, .es, .mjs, .ts, .tsx": {
"style": "@npmjsAccount/import-sort-style-your-style"
}
}
@naume sorry, but it does not solve the issue. I really do not want to use npm or any other registry and just import the file directly from the project.
@naume ok, I thought this through and you can actually use something like https://github.com/lerna/lerna to keep packages in one repo and use namespaces like you mentioned. Did you have something like that in mind?
@mcharytoniuk Hi, how did you managed to make it work with yarn portal? I've added sth like this to devDependencies "import-sort-style-daniel": "file:./config/import-sort-style",. It is a folder with index.ts and package.json inside. I do call yarn and it is properly moved to node_modules but I still got an error when using it:
▶ ./node_modules/import-sort-cli/lib/index.js ./src/components/Current.js
src/components/Current.js:
Error: Style "daniel" could not be resolve
@dkoprowski Did you config the package.json correctly? Because I'm using the same syntax "import-sort-style-daniel": "file:./config/import-sort-style" and it worked for me (with npm though)