-
Notifications
You must be signed in to change notification settings - Fork 117
/
alias.config.js
32 lines (30 loc) · 1.18 KB
/
alias.config.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
const resolve = require('path').resolve;
const fs = require('fs');
const ROOT = resolve(__dirname, '.');
const MODULES_ROOT = resolve(ROOT, 'modules');
const AliasConfig = modulesDir => {
const mdir = modulesDir || MODULES_ROOT;
// this function looks into all packages under ./src and creates alias for local dev
// {@mlvis/some-package: './modules/some-package/src'}
return () =>
fs.readdirSync(mdir).reduce((aliasMap, pkg) => {
// src/some-package
const packagePath = resolve(mdir, pkg);
if (!fs.lstatSync(packagePath).isDirectory()) {
return aliasMap;
}
// packages/some-package/package.json
const packageJsonPath = resolve(packagePath, 'package.json');
// bypass alias mapping if no package.json exists
if (!fs.existsSync(packageJsonPath)) {
return aliasMap;
}
const packageInfo = require(packageJsonPath);
// @mlvis/some-package => some-package
const packageName = packageInfo.name.replace(/^(@mlvis\/)/, '');
aliasMap[packageInfo.name] = resolve(mdir, packageName, 'src');
return aliasMap;
}, {});
};
module.exports = AliasConfig(MODULES_ROOT);
module.exports.AliasConfig = AliasConfig;