Skip to content

Commit

Permalink
Adds support for babel-module-resolver (#5)
Browse files Browse the repository at this point in the history
* Adds support for babel-module-resolver

* fixes module resolver and adds a test

Co-authored-by: Moji Izadmehr <[email protected]>
  • Loading branch information
leorossi and M-Izadmehr authored Jun 27, 2020
1 parent b4089c2 commit 46e51dd
Show file tree
Hide file tree
Showing 11 changed files with 5,842 additions and 7 deletions.
3,119 changes: 3,119 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"enhanced-resolve": "^4.1.1",
"express": "^4.17.1",
"lodash": "^4.17.15",
"lodash.get": "^4.4.2",
"moment": "^2.24.0",
"mustache-express": "^1.3.0",
"opn": "^6.0.0",
Expand All @@ -36,6 +37,7 @@
"eslint": "^6.6.0",
"eslint-config-prettier": "^6.5.0",
"eslint-plugin-prettier": "^3.1.1",
"find-babel-config": "^1.2.0",
"husky": "^3.0.9",
"jest": "^26.1.0",
"lint-staged": "^9.4.2",
Expand Down
40 changes: 38 additions & 2 deletions src/handlers/fileResolver.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
const get = require("lodash.get");
const path = require("path");
const enhancedResolve = require("enhanced-resolve");
const { defaultParsableExtensions } = require("../models/Parsables");

const moduleResolver = require("babel-plugin-module-resolver");
const findBabelConfig = require("find-babel-config");
function checkNodeModules(absPath) {
return /node_modules/.test(absPath) || (absPath[0] !== "/" && absPath[1] !== ':');
return (
/node_modules/.test(absPath) || (absPath[0] !== "/" && absPath[1] !== ":")
);
}

function checkBabelModuleResolver(filePath, currentFile) {
// path doesn't start with a '.' or with a '/' which should be local dependencies
if (!filePath.match(/^(\.|\/)/)) {
const loadedConfig = findBabelConfig.sync(currentFile);
if (get(loadedConfig, "config.plugins")) {
// find module-resolver configuration
const moduleResolverConfig = loadedConfig.config.plugins.find(element => {
if (Array.isArray(element)) {
if (element[0] == "module-resolver") {
return element;
}
}
});
const opts = moduleResolverConfig[1];
// convert root to absolute path, otherwise module resolver won't work properly
opts.root = path.join(path.dirname(loadedConfig.file), opts.root[0]);
const currentFileDirectory = path.dirname(currentFile);
const resolved = moduleResolver.resolvePath(filePath, currentFile, opts);
let output = path.join(currentFileDirectory, resolved);
if (!output.match(/\.js$/)) {
output += ".js";
}
return output;
}
}
}
// by default resolve Parsable files even without .ext in import
const resolveSync = enhancedResolve.create.sync({
extensions: defaultParsableExtensions
Expand All @@ -27,6 +57,12 @@ function resolve(relPath = "", currentFile = "") {
return { resolved };
} catch (err) {
if (!err.missing) return;

// Check if it's resolvable by babel-module-resolver
const absPath = checkBabelModuleResolver(relPath, currentFile);
if (absPath) {
return { resolved: { absPath } };
}
const notFound = checkNodeModules(relPath)
? relPath
: path.resolve(currentDir, relPath);
Expand Down
12 changes: 7 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ const WriteReportFile = require("./cli/WriteReportFile");
const ReportServer = require("./reportServer/server");
class DeadFile {
constructor(argv) {
const { _: entry, dir, exclude = [], output } = argv;

const { _: entry, dir, exclude = [], output, report } = argv;
this.baseDir = this.dirToAbs(dir); // if dir is not absolute find based on pwd
this.entry = this.entryToAbs(entry, dir); // if the entry is not absolute find the absolute
this.exclude = exclude; // excluded files/folders
this.output = output;

this.shouldReport = !(report == false);
this.allAssets = AllAssets(dir, { exclude }); // Array

this.results = null;
new UsedAssets(
{ entry: this.entry, dir: this.baseDir, exclude: this.exclude },
this.setUsedAssets.bind(this)
Expand Down Expand Up @@ -75,7 +74,10 @@ class DeadFile {
importedButNotFoundInScope
};

this.reporting(data);
if (this.shouldReport) {
this.reporting(data);
}
this.results = data;
}

reporting(data) {
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/babel-example/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"plugins": [
"@babel/plugin-transform-modules-commonjs",
[
"module-resolver",
{
"root": [
"./src"
]
}
]
]
}
Loading

0 comments on commit 46e51dd

Please sign in to comment.