1.0.0 • Published 5 years ago

@lomray/metro-custom-path-resolver v1.0.0

Weekly downloads
8
License
MIT
Repository
github
Last release
5 years ago

Metro Custom Path Resolver

.
└── src
├── assets
│   └── sample-icon.jpg
├── components
│   └── sample-component.js
├── custom-dir
│   ├── assets
│   │   └── sample-icon.jpg
│   └── components
│   └── sample-component.js
└── index.js

Content index.js:

import image from 'assets/sample-icon.jpg';
import { DemoComponent } from 'components/sample-component.js';

...

This module, resolve original sample-icon.jpg and sample-component.js from custom-dir, if they exist.

PS: For import an original module from resolved, use a relative path instead alias, in other cases, use aliases.

Installation & Usage

First, install the npm module.

npm install --save-dev @lomray/metro-custom-path-resolver

Next, enable hot reloading in your webpack config:
1. Import in metro.config.js:

```js
const path               = require('path')
const fs                 = require('fs')
const CustomPathResolver = require('@lomray/metro-custom-path-resolver')

const appDirectory = fs.realpathSync(process.cwd())
const resolveApp   = relativePath => path.resolve(appDirectory, relativePath)
```
  1. Add the module to the resolve / plugins array:
    module.exports = {
       resolver:    {
           resolveRequest: CustomPathResolver({
               excludePath: /(custom-dir|node_modules)/,
               rootPath:    resolveApp('src/'),
               projectPath: resolveApp(`src/custom-dir/`),
           })
       }
    }

You're all set!