Skip to content

Instantly share code, notes, and snippets.

@iRoachie
Created September 4, 2017 13:26
Show Gist options
  • Save iRoachie/423512a0fee9be3d118c5a9cfc6310b8 to your computer and use it in GitHub Desktop.
Save iRoachie/423512a0fee9be3d118c5a9cfc6310b8 to your computer and use it in GitHub Desktop.
Getting starting with module resolver

Babel Module resolver is a great tool we use to speed up development. Since es6 code has to go through babel anyway to compile to normal js, we can take advantage of this and remap the paths to files.

Firstly install the plugin. npm i -D babel-plugin-module-resolver

Add the paths you want to use to your .babelrc. Here's an example of how it could look with react native.

{
  "presets": [
    "react-native"
  ],
  "plugins": [
    ["module-resolver", {
      "root": ["./src"],
      "alias": {
        "@config": "./src/config",
        "@components": "./src/components",
        "@models": "./src/models",
        "@data": "./src/data",
        "@constants": "./src/constants",
        "@actions": "./src/actions"
      }
    }]
  ]
}

Import your files like a king! import { Theme } from '@config'

@teenoh
Copy link

teenoh commented Sep 10, 2017

Nice

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