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

Module resolver support? #3

Open
pedro-lb opened this issue Nov 25, 2019 · 3 comments
Open

Module resolver support? #3

pedro-lb opened this issue Nov 25, 2019 · 3 comments

Comments

@pedro-lb
Copy link

pedro-lb commented Nov 25, 2019

Hey,

First of all thanks for this awesome lib! It's really good =)

I wonder if we could add babel.js module-resolver support - in order to support absolute imports.

For example I have this setup in my project:

import FadeIn from "components/Loading/FadeIn";

Thanks to module-resolver I don't have to do ../../../components/Loading/FadeIn, since it simplifies that by allowing me to directly target components folder.

But due to module-resolver, deadfile doesn't recognise that the component is being imported:

image

Any ideas? I'm willing to contribute to this project. 😃

@M-Izadmehr
Copy link
Owner

Nice suggestion, if you are interested to help, I would be extremely glad if you are interested to add this feature to the repo.
There are three ways to add absolute import that I can think of now, but we can start by supporting babel:

  1. package.json:
    "components": "file:./app/src/components",
  1. Webpack resolve
  resolve: {
    alias: {
    "components": "file:./app/src/components",
    }
  }
  1. Babel
{
  "plugins": [
    ["babel-plugin-module-resolver", {
      "alias": {
         "components": "file:./app/src/components",
      }
    }]
  ]
}

Reminder for future reference
I am sure that in 99% of cases there is not going to be a naming conflict between these three, but I think we should initially check their priority (we can go with babel for now).

How to do it
Initially, we can just go for babel.

  1. We should search for all valid babel config files (based on their priority if there is more than one babel config file .babelrc vs babel.config.js (in the main folder), import it, find the alias section.

This should be done in src/index as a method.
This file uses two important methods:

  • UsedAssets which finds all files which are imported in the project (this class should get the module aliases as input to be able to use them)
  • AllAssets which finds all files existing in the project
  1. UsedAssets:
    In order to handle imports aliases for used assets the main changes should be done in /handlers/fileResolver.js, this file gets import path as (relPath) and currentFile, and decides if this is a node_modules import or not. So before resolving file, and checking if it is node_modules or not, we need to handle aliases.

@matt-dalton
Copy link

Do we have to pass in any options to get this to work on v1.2.2? It seems to not be working by default for me with deadfile ./App.tsx --exclude ios android

It could be because of other things I have going on: e.g. Typescript, React Native etc

@ridvanaltun
Copy link

Do we have to pass in any options to get this to work on v1.2.2? It seems to not be working by default for me with deadfile ./App.tsx --exclude ios android

It could be because of other things I have going on: e.g. Typescript, React Native etc

Same issue here, i'm using babel-plugin-module-resolver with a react-native project. There is no error, just not recognize resolved paths.

My babel configuration:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      'module-resolver',
      {
        extensions: [
          '.ios.ts',
          '.android.ts',
          '.ts',
          '.ios.tsx',
          '.android.tsx',
          '.tsx',
          '.jsx',
          '.js',
          '.json',
        ],
        alias: {
          '@components': './src/components',
          '@constants': './src/constants',
          '@screens': './src/screens',
          '@contexts': './src/contexts',
          '@reducers': './src/reducers',
          '@locales': './src/locales',
          '@helpers': './src/helpers',
          '@api': './src/services/ApiService.js',
        },
      },
    ],
  ],
};

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

4 participants