Skip to content

Commit

Permalink
fixes module resolver and adds a test
Browse files Browse the repository at this point in the history
  • Loading branch information
leorossi committed Feb 19, 2020
1 parent 5daaae9 commit 6e7b3b3
Show file tree
Hide file tree
Showing 11 changed files with 5,830 additions and 22 deletions.
3,119 changes: 3,119 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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 Down
44 changes: 27 additions & 17 deletions src/handlers/fileResolver.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const get = require("lodash.get");
const path = require("path");
const enhancedResolve = require("enhanced-resolve");
const { defaultParsableExtensions } = require("../models/Parsables");
Expand All @@ -10,21 +11,29 @@ function checkNodeModules(absPath) {
}

function checkBabelModuleResolver(filePath, currentFile) {
const loadedConfig = findBabelConfig.sync(currentFile);
if (loadedConfig) {
// find module-resolver configuration
const moduleResolverConfig = loadedConfig.config.plugins.find(element => {
if (Array.isArray(element)) {
if (element[0] == "module-resolver") {
return element;
// 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";
}
});
const opts = moduleResolverConfig[1];
const currentFileDirectory = path.dirname(currentFile);
const resolved = moduleResolver.resolvePath(filePath, currentFile, opts);
const output = path.join(currentFileDirectory, resolved);
return output;
return output;
}
}
}
// by default resolve Parsable files even without .ext in import
Expand All @@ -39,10 +48,6 @@ const resolveSync = enhancedResolve.create.sync({
*/
function resolve(relPath = "", currentFile = "") {
const currentDir = path.dirname(currentFile);
if (!relPath.match(/^(\.|\/)/)) {
// path doesn't start with a '.' or with a '/' which should be local dependencies
relPath = checkBabelModuleResolver(relPath, currentFile);
}

try {
const absPath = resolveSync(currentDir, relPath);
Expand All @@ -53,6 +58,11 @@ function resolve(relPath = "", currentFile = "") {
} 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 6e7b3b3

Please sign in to comment.