This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbabel.resolver.js
72 lines (67 loc) · 1.56 KB
/
babel.resolver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Babel monorepo resolver function
* More details: https://babeljs.io/docs/en/configuration#babelconfigjs
* Make sure to install monorepo project dependencies and current project dependencies
*
* ```
* npm i && (cd ./path-to/monorepo-project && npm i)
* ```
*
* Also you have to install `babel-plugin-module-resolver`
* ```
* npm i -D babel-plugin-module-resolver
* ```
*
* Usage (in babel.config.js):
*
* ```
* const monorepoResolver = require('./babel.resolver');
*
* const { root, alias } = monorepoResolver({
* path: 'path-to/monorepo-project',
* modules: [
* 'path-to/module-1',
* 'path-to/module-2',
* ...
* ],
* });
*
* const moduleResolverConfig = {
* root,
* alias: {
* ...alias,
* // additional aliases
* },
* };
*
* module.exports = function (api) {
* api.cache(true);
*
* const presets = [
* 'module:metro-react-native-babel-preset',
* ];
*
* const plugins = [
* ['module-resolver', moduleResolverConfig],
* ];
*
* return { presets, plugins };
* };
* ```
*/
const path = require('path')
module.exports = (structure) => {
const frameworkPath = path.resolve(__dirname, structure.path)
const alias = structure.modules.reduce((acc, frameworkModule) => {
const frameworkModulePath = path.resolve(frameworkPath, frameworkModule)
const frameworkModuleConfig = path.resolve(
frameworkModulePath,
'package.json'
)
return { ...acc, [frameworkModuleConfig.name]: frameworkModulePath }
}, {})
return {
root: path.resolve('./'),
alias,
}
}