-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (30 loc) · 1.07 KB
/
index.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
import findBabelConfig from "find-babel-config";
import path from "path";
import { Module } from "module";
import { resolvePath } from "babel-plugin-module-resolver";
const root = path.resolve(".");
const loadLikeNormal = Module._load;
exports.before = config => {
const moduleResolverOpts = getModuleResolverOpts(config);
Module._load = (requireString, _module, isMain) => {
const normalPath =
resolvePath(requireString, _module.filename, moduleResolverOpts) ||
requireString;
return loadLikeNormal(normalPath, _module, isMain);
};
};
const getModuleResolverOpts = ({ alias } = {}) => {
if (alias) return { root, alias };
let babelConfig = findBabelConfig.sync(root).config;
if (babelConfig.env) {
babelConfig =
babelConfig.env[
process.env.BABEL_ENV || process.env.NODE_ENV || "development"
];
}
if (!babelConfig || !babelConfig.plugins) return null;
const moduleResolverConfig = babelConfig.plugins.find(
pluginConfig => pluginConfig[0] === "module-resolver"
);
return moduleResolverConfig && moduleResolverConfig[1];
};