Skip to content

Commit

Permalink
feat: improve extends behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate Moore committed Mar 15, 2022
1 parent 40d0d6f commit ddce5c6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-tips-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@proload/core": minor
---

Improve `extends` behavior related to dependencies
38 changes: 25 additions & 13 deletions packages/core/lib/esm/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import deepmerge from 'deepmerge';

import { existsSync, readdir, readFile, stat } from "fs";
import { promisify } from "util";
import { createRequire } from "module";
import resolvePkg from "resolve-pkg";
import requireOrImport from "./requireOrImport.mjs";
import { assert, ProloadError } from "../error.cjs";

Expand All @@ -13,7 +13,6 @@ export { ProloadError };
const toStats = promisify(stat);
const toRead = promisify(readdir);
const toReadFile = promisify(readFile);
const require = createRequire(import.meta.url);

let merge = deepmerge;
const defaultExtensions = ['js', 'cjs', 'mjs'];
Expand Down Expand Up @@ -67,24 +66,37 @@ const requireOrImportWithMiddleware = (filePath) => {
*/
async function resolveExtension(namespace, { filePath, extension }) {
let resolvedPath;
if (extname(extension) === "") {
if (extension.startsWith("./") || extension.startsWith("../")) {
resolvedPath = require.resolve(`${extension}${extname(filePath)}`, {
paths: [dirname(filePath)],
});
if (extension.startsWith("./") || extension.startsWith("../")) {
if (extname(extension) === "") {
resolvedPath = resolve(dirname(filePath), `${extension}${extname(filePath)}`);
}
if (!existsSync(resolvedPath)) resolvedPath = null;

if (!resolvedPath) {
resolvedPath = resolve(dirname(filePath), extension);
}
if (!existsSync(resolvedPath)) resolvedPath = null;
}
if (!resolvedPath) {
const pkg = resolvePkg(extension, {
cwd: dirname(filePath),
});
const accepted = validNames(namespace);
for (const config of accepted) {
try {
resolvedPath = require.resolve(`${extension}/${config}`, {
paths: [dirname(filePath)],
});
break;
resolvedPath = `${pkg}/${config}`;
if (resolvedPath && existsSync(resolvedPath)) {
break;
} else {
resolvedPath = null
}
} catch (e) {}
}
}
resolvedPath =
resolvedPath || require.resolve(extension, { paths: [dirname(filePath)] });
if (!resolvedPath) {
resolvedPath = resolvePkg(extension, { cwd: dirname(filePath) });
}
if (!resolvedPath) return
const value = await requireOrImportWithMiddleware(resolvedPath);

return { filePath: resolvedPath, value };
Expand Down
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"author": "Nate Moore <[email protected]>",
"dependencies": {
"deepmerge": "^4.2.2",
"escalade": "^3.1.1"
"escalade": "^3.1.1",
"resolve-pkg": "^2.0.0"
},
"repository": {
"type": "git",
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,13 @@ resolve-from@^5.0.0:
resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-5.0.0.tgz#c35225843df8f776df21c57557bc087e9dfdfc69"
integrity sha512-qYg9KP24dD5qka9J47d0aVky0N+b4fTU89LN9iDnjB5waksiC49rvMB0PrUJQGoTmH50XPiqOvAjDfaijGxYZw==

resolve-pkg@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/resolve-pkg/-/resolve-pkg-2.0.0.tgz#ac06991418a7623edc119084edc98b0e6bf05a41"
integrity sha512-+1lzwXehGCXSeryaISr6WujZzowloigEofRB+dj75y9RRa/obVcYgbHJd53tdYw8pvZj8GojXaaENws8Ktw/hQ==
dependencies:
resolve-from "^5.0.0"

resolve@^1.10.0:
version "1.20.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.20.0.tgz#629a013fb3f70755d6f0b7935cc1c2c5378b1975"
Expand Down

0 comments on commit ddce5c6

Please sign in to comment.