import-sort icon indicating copy to clipboard operation
import-sort copied to clipboard

Local import path

Open mcharytoniuk opened this issue 5 years ago • 7 comments

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. :)

mcharytoniuk avatar Jan 17 '20 13:01 mcharytoniuk

Side note: using Yarn portal / link protocols might help here as a workaround also: https://next.yarnpkg.com/features/protocols

mcharytoniuk avatar Feb 04 '20 19:02 mcharytoniuk

This is great and should be added to the README !

Floriferous avatar Apr 02 '20 12:04 Floriferous

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 avatar May 18 '20 18:05 naume

@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.

mcharytoniuk avatar May 26 '20 07:05 mcharytoniuk

@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 avatar May 26 '20 11:05 mcharytoniuk

@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 avatar Jun 03 '20 16:06 dkoprowski

@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)

grumd avatar Jun 26 '20 16:06 grumd