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

[RFC] Enhancing plugin with transform imports functionality #413

Open
serhiipalash opened this issue Oct 23, 2020 · 0 comments
Open

[RFC] Enhancing plugin with transform imports functionality #413

serhiipalash opened this issue Oct 23, 2020 · 0 comments

Comments

@serhiipalash
Copy link

serhiipalash commented Oct 23, 2020

Hi @tleunen ! My name is Serhii Palash.

I would like to commit in your repository a functionality of transforms-imports plugin https://www.npmjs.com/package/babel-plugin-transform-imports. Let me explain why.

In our project we follow the principle One module - one function. We follow this rule to make code more stable, to always be sure that the specific screen depends only on what it really needs. We use named exports only for styled components, in all other places we always use export default.

The code structure looks like this

src/
  app/
    auth/
      actions/
        loginWithEmailAndPassword.js
        loginWithGoogle.js
        loginWithFacebook.js

So, the import statements in the screen component are

import loginWithEmailAndPassword from '@/app/auth/actions/loginWithEmailAndPassword'
import loginWithFacebook from '@/app/auth/actions/loginWithFacebook'

And we use your great plugin for aliasing src/ directory to @ symbol.

Many other projects follow the One module - one function rule (e.g. Material UI).

The main problem with organising code in this way is a long and verbose list of imports, especially on big screens

Screenshot 2020-10-23 at 11 29 59

and the solution is to use a transform Babel plugin transpiling this

import {
  loginWithEmailAndPassword,
  loginWithFacebook
} from '@/app/auth/actions'

into this

import loginWithEmailAndPassword from '@/app/auth/actions/loginWithEmailAndPassword'
import loginWithFacebook from '@/app/auth/actions/loginWithFacebook'

and there is a nice plugin doing it - babel-plugin-transform-imports.

Still it's not enough. Even though aliases are applied and imports are transformed and the app works, the linting is broken.

The goals for linting are

  1. Highlight missing real named exports

Screenshot 2020-10-23 at 11 43 13

  1. Highlight unresolved modules

Screenshot 2020-10-23 at 11 44 36

  1. Apply aliases during imports linting

Screenshot 2020-10-23 at 11 49 02

  1. Highlight named import when it resolves to missing module after transformation

Screenshot 2020-10-23 at 11 54 50

And the problem is with last goal. When it's a transformation of imports, linting looks like this

Screenshot 2020-10-23 at 11 57 39

That is because eslint-import-resolver-babel-module, which eslint-import uses for resolving modules, knows only about babel-plugin-module-resolver and doesn't know anything about babel-plugin-transform-imports. So, it applies alias then checks if src/app/auth/actions.js exists, which doesn't. It doesn't know that it should add named import at the end of path src/app/auth/actions/loginWithEmailAndPassword.js.

There are two ways how I can make it work

  1. Update eslint-import-resolver-babel-module making it aware about transform-imports plugin effect.
  2. Update yours babel-plugin-module-resolver making it support imports transformation and in this way making eslint-import-resolver-babel-module aware about it.

I would personally choose the option 2., making yours babel-plugin-module-resolver to support imports transformation. Your plugin has name module-resolver and not module-aliases, so it seems logical to make it responsible for imports transformation too. Plus your plugin is actually alive and the transforms-imports seems to be dead.

So, can I create a pull request with functionality accepting config like this?

[
  'module-resolver',
  {
    transforms: {
      '@/app/auth/actions': {
        transform: '@/app/auth/actions/${member}',
        preventFullImport: true,
      },
    }
    alias: {
      '@': './src',
    },
    extensions: ['.json', '.js'],
  },
],

I will also add support for regex in transform matchers.

P.S. Material UI is a very popular framework. I think people will thank you for improving its usage.

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

No branches or pull requests

1 participant