Skip to content

Commit

Permalink
extract node resolver to common module in lib
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Jun 2, 2018
1 parent ce5b35d commit 36611f1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"use strict";

const Path = require("path");
const requireAt = require("require-at");
const optionalRequire = require("optional-require");
const nodeResolver = require("../../../lib/node-resolver");

/*
* This is a webpack module resolver plugin to ensure that modules are resolved from
Expand All @@ -16,70 +15,25 @@ module.exports = class ModuleResolver {
this.target = target;
}

isModuleRequest(request) {
//
// If request is not an absolute/relative path then it's requiring a
// nodejs module.
//
if (Path.isAbsolute(request)) return false;

if (request === "." || request === "..") {
return false;
}

if (request.startsWith("../") || request.startsWith("./")) {
return false;
}

if (Path.sep !== "/") {
return !request.startsWith("." + Path.sep) || !request.startsWith(".." + Path.sep);
}

return true;
}

apply(resolver) {
const archetypeDir = Path.join(__dirname, "..", "..", "..");

resolver.plugin(this.source, (req, callback) => {
//
// Let webpack's internal resolver deal with requiring non-modules
//
if (!this.isModuleRequest(req.request)) return callback();

//
// If require is NOT originated from a dir within node_modules, then
// call require.resolve from within the archetype's directory
// to make sure the instance the archetype depends on is resolved.
//
const atPath = req.path.indexOf(this.path) < 0 ? archetypeDir : req.path;

const result = nodeResolver.resolve(req.request, atPath);

//
// If request is requiring for a module, then first try to resolve the
// module's real path by looking for its package.json, and then use that
// with request converted to a relative path without the module name part.
//
const splits = req.request.split("/");
const nameX = splits[0].startsWith("@") ? 2 : 1; // check for scoped module
const name = splits.slice(0, nameX).join("/");
//
// All modules must have package.json
// Let webpack's internal resolver deal with requiring non-modules
//
const resolved = optionalRequire.resolve(
requireAt(atPath),
// ensure require request paths are POSIX
Path.posix.join(name, "package.json"),
false
);

if (!resolved) return callback();

splits.splice(0, nameX, ".");
if (result === undefined) return callback();

const obj = Object.assign({}, req, {
path: Path.dirname(resolved),
request: splits.join("/")
});
const obj = Object.assign({}, req, result);

return resolver.doResolve(
this.target,
Expand Down
61 changes: 61 additions & 0 deletions packages/electrode-archetype-react-app-dev/lib/node-resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"use strict";

/* eslint-disable prefer-template */

const Path = require("path");
const requireAt = require("require-at");
const optionalRequire = require("optional-require");

function isModuleRequest(request) {
//
// If request is not an absolute/relative path then it's requiring a
// nodejs module.
//
if (Path.isAbsolute(request)) return false;

if (request === "." || request === "..") {
return false;
}

if (request.startsWith("../") || request.startsWith("./")) {
return false;
}

if (Path.sep !== "/") {
return !request.startsWith("." + Path.sep) || !request.startsWith(".." + Path.sep);
}

return true;
}

function resolve(req, atPath) {
if (!isModuleRequest(req)) return undefined;

const splits = req.split("/");
const nameX = splits[0].startsWith("@") ? 2 : 1; // check for scoped module
const name = splits.slice(0, nameX).join("/");

//
// All modules must have package.json
//
const resolved = optionalRequire.resolve(
requireAt(atPath),
// ensure require request paths are POSIX
Path.posix.join(name, "package.json"),
false
);

if (!resolved) return undefined;

splits.splice(0, nameX, ".");

return {
path: Path.dirname(resolved),
request: splits.join("/")
};
}

module.exports = {
isModuleRequest,
resolve
};

0 comments on commit 36611f1

Please sign in to comment.